Templating bugfix (#86)
This commit is contained in:
parent
f0d22328cd
commit
9ad080d24c
5 changed files with 34 additions and 7 deletions
16
src/pcbs.js
16
src/pcbs.js
|
@ -1,4 +1,6 @@
|
|||
const m = require('makerjs')
|
||||
const yaml = require('js-yaml')
|
||||
|
||||
const u = require('./utils')
|
||||
const a = require('./assert')
|
||||
const prep = require('./prepare')
|
||||
|
@ -202,22 +204,24 @@ const footprint = exports._footprint = (points, net_indexer, component_indexer,
|
|||
// combine default value with potential user override
|
||||
let value = params[param_name] !== undefined ? params[param_name] : parsed_def.value
|
||||
const type = parsed_def.type
|
||||
a.in(type, `${name}.params.${param_name}.type`, [
|
||||
'string', 'number', 'boolean', 'array', 'object', 'net', 'anchor'
|
||||
])
|
||||
|
||||
// templating support, with conversion back to raw datatypes
|
||||
const converters = {
|
||||
string: v => v,
|
||||
number: v => a.sane(v, `${name}.params.${param_name}`, 'number')(units),
|
||||
boolean: v => v === 'true'
|
||||
boolean: v => v === 'true',
|
||||
array: v => yaml.load(v),
|
||||
object: v => yaml.load(v),
|
||||
net: v => v,
|
||||
anchor: v => yaml.load(v)
|
||||
}
|
||||
if (a.type(value)() == 'string' && Object.keys(converters).includes(type)) {
|
||||
a.in(type, `${name}.params.${param_name}.type`, Object.keys(converters))
|
||||
if (a.type(value)() == 'string') {
|
||||
value = u.template(value, point.meta)
|
||||
value = converters[type](value)
|
||||
}
|
||||
|
||||
// type-specific processing
|
||||
// type-specific postprocessing
|
||||
if (['string', 'number', 'boolean', 'array', 'object'].includes(type)) {
|
||||
parsed_params[param_name] = value
|
||||
} else if (type == 'net') {
|
||||
|
|
|
@ -23,7 +23,7 @@ exports.template = (str, vals={}) => {
|
|||
let res = str
|
||||
let shift = 0
|
||||
for (const match of str.matchAll(regex)) {
|
||||
const replacement = deep(vals, match[1]) || ''
|
||||
const replacement = (deep(vals, match[1]) || '') + ''
|
||||
res = res.substring(0, match.index + shift)
|
||||
+ replacement
|
||||
+ res.substring(match.index + shift + match[0].length)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
points.zones.matrix:
|
||||
mirror: 10
|
||||
key:
|
||||
magic_value: 5
|
||||
outlines:
|
||||
edge:
|
||||
- what: rectangle
|
||||
|
@ -44,3 +46,10 @@ pcbs:
|
|||
params:
|
||||
start: {x: 5, y: 5}
|
||||
end: [[6, 6], [7, 7]]
|
||||
arrobj_templated:
|
||||
what: arrobj_test
|
||||
where:
|
||||
ref: matrix
|
||||
params:
|
||||
start: '{x: {{magic_value}}, y: {{magic_value}}}'
|
||||
end: '[[6, 6], [7, {{magic_value}}]]'
|
||||
|
|
|
@ -221,6 +221,19 @@
|
|||
)
|
||||
|
||||
|
||||
|
||||
|
||||
(module arrobj_test (layer F.Cu) (tedit 5CF31DEF)
|
||||
|
||||
(at 0 0 0)
|
||||
|
||||
(fp_line (start 5 5) (end 6 6) (layer Dwgs.User) (width 0.05))
|
||||
(fp_line (start 5 5) (end 7 5) (layer Dwgs.User) (width 0.05))
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
(gr_line (start -9.5 9.5) (end 9.5 9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
|
||||
(gr_line (start 9.5 9.5) (end 9.5 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
|
||||
(gr_line (start 9.5 -9.5) (end -9.5 -9.5) (angle 90) (layer Edge.Cuts) (width 0.15))
|
||||
|
|
|
@ -33,6 +33,7 @@ describe('Utils', function() {
|
|||
{longlonglong: 'long', short: 'shortshortshort'}
|
||||
).should.equal('long_shortshortshort')
|
||||
u.template('{{a.b.c}}', {a: {b: {c: 'deep'}}}).should.equal('deep')
|
||||
u.template('{x: {{number}}, y: {{number}}}', {number: 5}).should.equal('{x: 5, y: 5}')
|
||||
})
|
||||
|
||||
it('eq', function() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue