1
0

Type/what consistency for shorthands and cases

This commit is contained in:
Bán Dénes 2022-12-18 12:37:44 +01:00
parent 83b7dc1bb8
commit da1417ce2f
3 changed files with 8 additions and 8 deletions

View File

@ -57,15 +57,15 @@ exports.parse = (config, outlines, units) => {
}
const part_qname = `cases.${case_name}.${part_name}`
const part_var = `${case_name}__part_${part_name}`
a.unexpected(part, part_qname, ['type', 'name', 'extrude', 'shift', 'rotate', 'operation'])
const type = a.in(part.type || 'outline', `${part_qname}.type`, ['outline', 'case'])
a.unexpected(part, part_qname, ['what', 'name', 'extrude', 'shift', 'rotate', 'operation'])
const what = a.in(part.what || 'outline', `${part_qname}.what`, ['outline', 'case'])
const name = a.sane(part.name, `${part_qname}.name`, 'string')()
const shift = a.numarr(part.shift || [0, 0, 0], `${part_qname}.shift`, 3)(units)
const rotate = a.numarr(part.rotate || [0, 0, 0], `${part_qname}.rotate`, 3)(units)
const operation = a.in(part.operation || 'add', `${part_qname}.operation`, ['add', 'subtract', 'intersect'])
let base
if (type == 'outline') {
if (what == 'outline') {
const extrude = a.sane(part.extrude || 1, `${part_qname}.extrude`, 'number')(units)
const outline = outlines[name]
a.assert(outline, `Field "${part_qname}.name" does not name a valid outline!`)
@ -79,7 +79,7 @@ exports.parse = (config, outlines, units) => {
outline_dependencies.push(name)
base = `${name}_outline_fn()`
} else {
a.assert(part.extrude === undefined, `Field "${part_qname}.extrude" should not be used when type=case!`)
a.assert(part.extrude === undefined, `Field "${part_qname}.extrude" should not be used when what=case!`)
a.in(name, `${part_qname}.name`, Object.keys(cases))
case_dependencies.push(name)
base = `${name}_case_fn()`

View File

@ -17,7 +17,7 @@ exports.operation = (str, choices={}, order=Object.keys(choices)) => {
let res = op_prefix(str)
for (const key of order) {
if (choices[key].includes(res.name)) {
res.type = key
res.what = key
break
}
}

View File

@ -13,10 +13,10 @@ describe('Operation', function() {
it('operation', function() {
// without choices, it's the same as op_prefix
o.operation('arst').should.deep.equal({name: 'arst', operation: 'add'})
// with choices, it propagates type where it found the name
o.operation('arst', {bad: [], good: ['arst']}).should.deep.equal({name: 'arst', operation: 'add', type: 'good'})
// with choices, it propagates the "what" from where it found the name
o.operation('arst', {bad: [], good: ['arst']}).should.deep.equal({name: 'arst', operation: 'add', what: 'good'})
// it also respects order when overridden
o.operation('arst', {first: ['arst'], second: ['arst']}, ['second', 'first']).should.deep.equal({name: 'arst', operation: 'add', type: 'second'})
o.operation('arst', {first: ['arst'], second: ['arst']}, ['second', 'first']).should.deep.equal({name: 'arst', operation: 'add', what: 'second'})
})
})