1
0

Units separated to their own block, and properly tested

This commit is contained in:
Bán Dénes 2021-07-11 13:45:53 +02:00
parent 71efdbe020
commit 452d7c155b
7 changed files with 100 additions and 19 deletions

View File

@ -1,4 +1,5 @@
const prepare = require('./prepare')
const units_lib = require('./units')
const points_lib = require('./points')
const outlines_lib = require('./outlines')
const cases_lib = require('./cases')
@ -15,13 +16,17 @@ module.exports = {
config = prepare.inherit(config)
const results = {}
logger('Parsing points...')
const [points, units] = points_lib.parse(config.points)
// parsing units
logger('Calculating variables...')
const units = units_lib.parse(config)
if (debug) {
results.points = {
data: points,
units: units
}
results.units = units
}
logger('Parsing points...')
const points = points_lib.parse(config.points, units)
if (debug) {
results.points = points
}
logger('Generating outlines...')

View File

@ -215,18 +215,7 @@ const perform_mirror = exports._perform_mirror = (point, axis) => {
return ['', null]
}
exports.parse = (config = {}) => {
// parsing units
const raw_units = prep.extend({
u: 19,
cx: 18,
cy: 17
}, a.sane(config.units || {}, 'points.units', 'object')())
const units = {}
for (const [key, val] of Object.entries(raw_units)) {
units[key] = a.mathnum(val)(units)
}
exports.parse = (config, units) => {
// config sanitization
a.unexpected(config, 'points', ['units', 'zones', 'key', 'rotate', 'mirror'])
@ -317,7 +306,7 @@ exports.parse = (config = {}) => {
}
// done
return [filtered, units]
return filtered
}
exports.visualize = (points) => {

21
src/units.js Normal file
View File

@ -0,0 +1,21 @@
const a = require('./assert')
const prep = require('./prepare')
const default_units = {
u: 19,
cx: 18,
cy: 17
}
exports.parse = (config = {}) => {
const raw_units = prep.extend(
default_units,
a.sane(config.units || {}, 'units', 'object')(),
a.sane(config.variables || {}, 'variables', 'object')()
)
const units = {}
for (const [key, val] of Object.entries(raw_units)) {
units[key] = a.mathnum(val)(units)
}
return units
}

View File

@ -0,0 +1,11 @@
units:
a: cy - 7
variables:
b: a * 1.5
points:
zones:
matrix:
columns:
col:
rows:
row:

View File

@ -0,0 +1,7 @@
{
"u": 19,
"cx": 18,
"cy": 17,
"a": 10,
"b": 15
}

48
test/unit/units.js Normal file
View File

@ -0,0 +1,48 @@
const u = require('../../src/units')
describe('Units', function() {
it('defaults', function() {
// check that an empty config has the default units (and nothing more)
const def = u.parse({})
Object.keys(def).length.should.equal(3)
def.u.should.equal(19)
def.cx.should.equal(18)
def.cy.should.equal(17)
})
it('units', function() {
// check that units can contain formulas, and reference each other
const res = u.parse({
units: {
a: 'cx / 2',
b: 'a + 1'
}
})
Object.keys(res).length.should.equal(5)
res.a.should.equal(9)
res.b.should.equal(10)
// also check that order matters, which it should
u.parse.bind(this, {
units: {
a: 'b + 1',
b: 'cx / 2'
}
}).should.throw()
})
it('variables', function() {
// check that variables work, and can override units
const res = u.parse({
units: {
a: 'cx / 2',
},
variables: {
a: 'u + 1'
}
})
Object.keys(res).length.should.equal(4)
res.a.should.equal(20)
})
})