Units separated to their own block, and properly tested
This commit is contained in:
parent
71efdbe020
commit
452d7c155b
7 changed files with 100 additions and 19 deletions
|
@ -1,4 +1,5 @@
|
||||||
const prepare = require('./prepare')
|
const prepare = require('./prepare')
|
||||||
|
const units_lib = require('./units')
|
||||||
const points_lib = require('./points')
|
const points_lib = require('./points')
|
||||||
const outlines_lib = require('./outlines')
|
const outlines_lib = require('./outlines')
|
||||||
const cases_lib = require('./cases')
|
const cases_lib = require('./cases')
|
||||||
|
@ -15,13 +16,17 @@ module.exports = {
|
||||||
config = prepare.inherit(config)
|
config = prepare.inherit(config)
|
||||||
const results = {}
|
const results = {}
|
||||||
|
|
||||||
logger('Parsing points...')
|
// parsing units
|
||||||
const [points, units] = points_lib.parse(config.points)
|
logger('Calculating variables...')
|
||||||
|
const units = units_lib.parse(config)
|
||||||
if (debug) {
|
if (debug) {
|
||||||
results.points = {
|
results.units = units
|
||||||
data: points,
|
}
|
||||||
units: units
|
|
||||||
}
|
logger('Parsing points...')
|
||||||
|
const points = points_lib.parse(config.points, units)
|
||||||
|
if (debug) {
|
||||||
|
results.points = points
|
||||||
}
|
}
|
||||||
|
|
||||||
logger('Generating outlines...')
|
logger('Generating outlines...')
|
||||||
|
|
|
@ -215,18 +215,7 @@ const perform_mirror = exports._perform_mirror = (point, axis) => {
|
||||||
return ['', null]
|
return ['', null]
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.parse = (config = {}) => {
|
exports.parse = (config, units) => {
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// config sanitization
|
// config sanitization
|
||||||
a.unexpected(config, 'points', ['units', 'zones', 'key', 'rotate', 'mirror'])
|
a.unexpected(config, 'points', ['units', 'zones', 'key', 'rotate', 'mirror'])
|
||||||
|
@ -317,7 +306,7 @@ exports.parse = (config = {}) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// done
|
// done
|
||||||
return [filtered, units]
|
return filtered
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.visualize = (points) => {
|
exports.visualize = (points) => {
|
||||||
|
|
21
src/units.js
Normal file
21
src/units.js
Normal 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
|
||||||
|
}
|
11
test/points/000_units.yaml
Normal file
11
test/points/000_units.yaml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
units:
|
||||||
|
a: cy - 7
|
||||||
|
variables:
|
||||||
|
b: a * 1.5
|
||||||
|
points:
|
||||||
|
zones:
|
||||||
|
matrix:
|
||||||
|
columns:
|
||||||
|
col:
|
||||||
|
rows:
|
||||||
|
row:
|
7
test/points/000_units___units.json
Normal file
7
test/points/000_units___units.json
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"u": 19,
|
||||||
|
"cx": 18,
|
||||||
|
"cy": 17,
|
||||||
|
"a": 10,
|
||||||
|
"b": 15
|
||||||
|
}
|
48
test/unit/units.js
Normal file
48
test/unit/units.js
Normal 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)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue