1
0
Fork 0

fix(core): Issue with hiding dependencies

This commit is contained in:
joostdecock 2022-09-11 18:00:41 +02:00
parent 32890819bf
commit 48b366c57e
10 changed files with 165 additions and 167 deletions

View file

@ -21,6 +21,7 @@ export function Part() {
// Enumerable properties // Enumerable properties
this.render = true // FIXME: Replace render with hide this.render = true // FIXME: Replace render with hide
this.hide = false // FIXME: Replace render with hide
this.attributes = new Attributes() this.attributes = new Attributes()
this.points = {} this.points = {}
this.paths = {} this.paths = {}
@ -323,7 +324,9 @@ Part.prototype.shorthand = function () {
const optionsProxy = { const optionsProxy = {
get: function (options, name) { get: function (options, name) {
if (typeof options[name] === 'undefined') if (typeof options[name] === 'undefined')
self.context.store.log.warning(`Tried to access \`options.${name}\` but it is \`undefined\``) self.context.store.log.warning(
`Tried to access \`options.${name}\` but it is \`undefined\``
)
return Reflect.get(...arguments) return Reflect.get(...arguments)
}, },
set: (options, name, value) => (self.context.settings.options[name] = value), set: (options, name, value) => (self.context.settings.options[name] = value),

View file

@ -13,9 +13,7 @@ import {
} from './utils.mjs' } from './utils.mjs'
export function Path(debug = false) { export function Path(debug = false) {
// Non-enumerable properties // Non-enumerable properties
addNonEnumProp(this, 'render', true) // FIXME: replace with hide
//addNonEnumProp(this, 'topLeft', false) //addNonEnumProp(this, 'topLeft', false)
//addNonEnumProp(this, 'bottomRight', false) //addNonEnumProp(this, 'bottomRight', false)
//addNonEnumProp(this, 'attributes', new Attributes()) //addNonEnumProp(this, 'attributes', new Attributes())
@ -28,6 +26,7 @@ export function Path(debug = false) {
this.attributes = new Attributes() this.attributes = new Attributes()
this.topLeft = false this.topLeft = false
this.bottomRight = false this.bottomRight = false
this.render = true
} }
/** Adds the log method for a path not created through the proxy **/ /** Adds the log method for a path not created through the proxy **/
@ -632,9 +631,7 @@ Path.prototype.divide = function () {
if (!op.to.sitsRoughlyOn(current)) if (!op.to.sitsRoughlyOn(current))
paths.push(new Path(this.debug).withLog(this.log).move(current).line(op.to)) paths.push(new Path(this.debug).withLog(this.log).move(current).line(op.to))
} else if (op.type === 'curve') { } else if (op.type === 'curve') {
paths.push( paths.push(new Path(this.debug).withLog(this.log).move(current).curve(op.cp1, op.cp2, op.to))
new Path(this.debug).withLog(this.log).move(current).curve(op.cp1, op.cp2, op.to)
)
} else if (op.type === 'close') { } else if (op.type === 'close') {
paths.push(new Path(this.debug).withLog(this.log).move(current).line(start)) paths.push(new Path(this.debug).withLog(this.log).move(current).line(start))
} }
@ -646,15 +643,13 @@ Path.prototype.divide = function () {
/** Finds intersections between this path and an X value */ /** Finds intersections between this path and an X value */
Path.prototype.intersectsX = function (x) { Path.prototype.intersectsX = function (x) {
if (typeof x !== 'number') if (typeof x !== 'number') this.log.error('Called `Path.intersectsX(x)` but `x` is not a number')
this.log.error('Called `Path.intersectsX(x)` but `x` is not a number')
return this.intersectsAxis(x, 'x') return this.intersectsAxis(x, 'x')
} }
/** Finds intersections between this path and an Y value */ /** Finds intersections between this path and an Y value */
Path.prototype.intersectsY = function (y) { Path.prototype.intersectsY = function (y) {
if (typeof y !== 'number') if (typeof y !== 'number') this.log.error('Called `Path.intersectsX(y)` but `y` is not a number')
this.log.error('Called `Path.intersectsX(y)` but `y` is not a number')
return this.intersectsAxis(y, 'y') return this.intersectsAxis(y, 'y')
} }
@ -781,9 +776,7 @@ Path.prototype.split = function (point) {
firstHalf.push(new Path(this.debug).withLog(this.log).move(path.ops[0].to).line(point)) firstHalf.push(new Path(this.debug).withLog(this.log).move(path.ops[0].to).line(point))
pi++ pi++
secondHalf = divided.slice(pi) secondHalf = divided.slice(pi)
secondHalf.unshift( secondHalf.unshift(new Path(this.debug).withLog(this.log).move(point).line(path.ops[1].to))
new Path(this.debug).withLog(this.log).move(point).line(path.ops[1].to)
)
} }
} else if (path.ops[1].type === 'curve') { } else if (path.ops[1].type === 'curve') {
if (path.ops[0].to.sitsRoughlyOn(point)) { if (path.ops[0].to.sitsRoughlyOn(point)) {

View file

@ -239,7 +239,9 @@ Pattern.prototype.draft = function () {
try { try {
const result = this.__parts[partName].draft(this.parts[partName].shorthand()) const result = this.__parts[partName].draft(this.parts[partName].shorthand())
if (typeof result === 'undefined') { if (typeof result === 'undefined') {
this.store.log.error(`Result of drafting part ${partName} was undefined. Did you forget to return the part?`) this.store.log.error(
`Result of drafting part ${partName} was undefined. Did you forget to return the part?`
)
} else this.parts[partName] = result } else this.parts[partName] = result
} catch (err) { } catch (err) {
this.store.log.error([`Unable to draft part \`${partName}\``, err]) this.store.log.error([`Unable to draft part \`${partName}\``, err])
@ -657,26 +659,27 @@ Pattern.prototype.__resolveParts = function (count = 0) {
} }
} }
for (const [name, part] of Object.entries(this.__parts)) { for (const [name, part] of Object.entries(this.__parts)) {
// Hide when hideAll is set
if (part.hideAll) part.hide = true
// Inject (from) // Inject (from)
if (part.from) { if (part.from) {
if (part.hideDependencies || part.hideAll) {
part.from.hide = true
part.from.hideAll = true
}
this.__parts[part.from.name] = part.from this.__parts[part.from.name] = part.from
this.__inject[name] = part.from.name this.__inject[name] = part.from.name
if (typeof this.__parts[part.from.name] === 'undefined') {
if (part.hideDependencies || part.hideAll) {
this.__parts[part.from.name].hide = true
this.__parts[part.from.name].hideAll = true
}
//this.config = addPartConfig(this.__parts[part.from.name], this.config, this.store)
}
} }
// Simple dependency (after) // Simple dependency (after)
if (part.after) { if (part.after) {
if (Array.isArray(part.after)) { if (Array.isArray(part.after)) {
for (const dep of part.after) { for (const dep of part.after) {
if (part.hideDependencies) dep.hide = true
this.__parts[dep.name] = dep this.__parts[dep.name] = dep
this.__addDependency(name, part, dep) this.__addDependency(name, part, dep)
} }
} else { } else {
if (part.hideDependencies) part.after.hide = true
this.__parts[part.after.name] = part.after this.__parts[part.after.name] = part.after
this.__addDependency(name, part, part.after) this.__addDependency(name, part, part.after)
} }
@ -729,18 +732,16 @@ Pattern.prototype.__resolveDependencies = function (graph = false) {
* configured dependencies. * configured dependencies.
*/ */
Pattern.prototype.needs = function (partName) { Pattern.prototype.needs = function (partName) {
// If only is unset, all parts are needed // If only is unset, all parts are needed
if ( if (
typeof this.settings.only === 'undefined' || typeof this.settings.only === 'undefined' ||
this.settings.only === false || this.settings.only === false ||
( Array.isArray(this.settings.only) && this.settings.only.length === 0 ) (Array.isArray(this.settings.only) && this.settings.only.length === 0)
) return true )
return true
// Make only to always be an array // Make only to always be an array
const only = typeof this.settings.only === 'string' const only = typeof this.settings.only === 'string' ? [this.settings.only] : this.settings.only
? [ this.settings.only ]
: this.settings.only
// Walk the only parts, checking each one for a match in its dependencies // Walk the only parts, checking each one for a match in its dependencies
for (const part of only) { for (const part of only) {
@ -777,7 +778,6 @@ Pattern.prototype.isHidden = function (partName) {
if (Array.isArray(this.settings.only)) { if (Array.isArray(this.settings.only)) {
if (this.settings.only.includes(partName)) return false if (this.settings.only.includes(partName)) return false
} }
if (this.__parts?.[partName]?.hide) return true if (this.__parts?.[partName]?.hide) return true
if (this.__parts?.[partName]?.hideAll) return true if (this.__parts?.[partName]?.hideAll) return true

View file

@ -548,7 +548,6 @@ export const addPartConfig = (part, config, store) => {
config = addPartOptions(part, config, store) config = addPartOptions(part, config, store)
config = addPartMeasurements(part, config, store) config = addPartMeasurements(part, config, store)
config = addPartOptionalMeasurements(part, config, store) config = addPartOptionalMeasurements(part, config, store)
//config = addPartDependencies(part, config, store)
config = addPartPlugins(part, config, store) config = addPartPlugins(part, config, store)
return config return config

View file

@ -4,7 +4,6 @@ import { Design, Pattern, Path } from '../src/index.mjs'
const expect = chai.expect const expect = chai.expect
describe('Part', () => { describe('Part', () => {
it('Shorthand should contain the part itself', () => { it('Shorthand should contain the part itself', () => {
let dp let dp
const part = { const part = {
@ -12,9 +11,9 @@ describe('Part', () => {
draft: ({ part }) => { draft: ({ part }) => {
dp = part dp = part
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ]})
const pattern = new design() const pattern = new design()
pattern.draft() pattern.draft()
expect(typeof dp).to.equal('object') expect(typeof dp).to.equal('object')
@ -181,7 +180,7 @@ describe('Part', () => {
expect(boundary.width).to.equal(124) expect(boundary.width).to.equal(124)
expect(boundary.height).to.equal(400) expect(boundary.height).to.equal(400)
}) })
/* /*
it('Should stack a part', () => { it('Should stack a part', () => {
const part = { const part = {
name: 'test', name: 'test',

View file

@ -7,13 +7,13 @@ describe('Path', () => {
it('Should offset a line', () => { it('Should offset a line', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, part}) => { draft: ({ paths, Path, Point, part }) => {
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)) paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
paths.offset = paths.line.offset(10) paths.offset = paths.line.offset(10)
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(pattern.parts.test.paths.offset.bottomRight.x).to.equal(-10) expect(pattern.parts.test.paths.offset.bottomRight.x).to.equal(-10)
@ -29,9 +29,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4)) .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
paths.offset = paths.curve.offset(10) paths.offset = paths.curve.offset(10)
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.18) expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.18)
@ -42,14 +42,12 @@ describe('Path', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, part }) => { draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path() paths.curve = new Path().move(new Point(0, 0))._curve(new Point(123, 34), new Point(23, 4))
.move(new Point(0, 0))
._curve(new Point(123, 34), new Point(23, 4))
paths.offset = paths.curve.offset(10) paths.offset = paths.curve.offset(10)
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.63) expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.63)
@ -60,14 +58,12 @@ describe('Path', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, part }) => { draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path() paths.curve = new Path().move(new Point(0, 0)).curve_(new Point(40, 0), new Point(123, 34))
.move(new Point(0, 0))
.curve_(new Point(40, 0), new Point(123, 34))
paths.offset = paths.curve.offset(10) paths.offset = paths.curve.offset(10)
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(119.26) expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(119.26)
@ -98,13 +94,11 @@ describe('Path', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, part }) => { draft: ({ paths, Path, Point, part }) => {
paths.line = new Path() paths.line = new Path().move(new Point(0, 0)).line(new Point(40, 0))
.move(new Point(0, 0))
.line(new Point(40, 0))
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(pattern.parts.test.paths.line.length()).to.equal(40) expect(pattern.parts.test.paths.line.length()).to.equal(40)
@ -119,9 +113,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4)) .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close() .close()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(round(pattern.parts.test.paths.curve.length())).to.equal(145.11) expect(round(pattern.parts.test.paths.curve.length())).to.equal(145.11)
@ -136,9 +130,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4)) .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close() .close()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(pattern.parts.test.paths.curve.start().x).to.equal(123) expect(pattern.parts.test.paths.curve.start().x).to.equal(123)
@ -154,9 +148,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4)) .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close() .close()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
expect(pattern.parts.test.paths.curve.end().x).to.equal(123) expect(pattern.parts.test.paths.curve.end().x).to.equal(123)
@ -825,9 +819,9 @@ describe('Path', () => {
.attr('class', 'bar') .attr('class', 'bar')
.attr('class', 'overwritten', true) .attr('class', 'overwritten', true)
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft().render() pattern.draft().render()
@ -1080,16 +1074,13 @@ describe('Path', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, points }) => { draft: ({ paths, Path, Point, points }) => {
paths.line = new Path() paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).attr('class', 'foo')
.move(new Point(0, 0))
.line(new Point(0, 40))
.attr('class', 'foo')
paths.a = new Path().move(points.a).line(points.b) paths.a = new Path().move(points.a).line(points.b)
paths.b = paths.a.offset() paths.b = paths.a.offset()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft() pattern.draft()
expect(pattern.store.logs.error.length).to.equal(2) expect(pattern.store.logs.error.length).to.equal(2)
@ -1102,15 +1093,12 @@ describe('Path', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, points }) => { draft: ({ paths, Path, Point, points }) => {
paths.line = new Path() paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).attr('class', 'foo')
.move(new Point(0, 0))
.line(new Point(0, 40))
.attr('class', 'foo')
paths.a = new Path().move(points.a).line(points.b).join() paths.a = new Path().move(points.a).line(points.b).join()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft() pattern.draft()
expect(pattern.store.logs.error.length).to.equal(2) expect(pattern.store.logs.error.length).to.equal(2)
@ -1155,34 +1143,31 @@ describe('Path', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, points }) => { draft: ({ paths, Path, Point, points }) => {
points.a = new Path() points.a = new Path().move(new Point(0, 0)).line(new Point(0, 40)).shiftFractionAlong()
.move(new Point(0, 0))
.line(new Point(0, 40))
.shiftFractionAlong()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft() pattern.draft()
expect(pattern.store.logs.error[0]).to.equal('Called `Path.shiftFractionAlong(fraction)` but `fraction` is not a number') expect(pattern.store.logs.error[0]).to.equal(
'Called `Path.shiftFractionAlong(fraction)` but `fraction` is not a number'
)
}) })
it('Should log a warning when splitting a path on a non-point', () => { it('Should log a warning when splitting a path on a non-point', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ paths, Path, Point, points }) => { draft: ({ paths, Path, Point, points }) => {
points.a = new Path() points.a = new Path().move(new Point(0, 0)).line(new Point(0, 40)).split()
.move(new Point(0, 0))
.line(new Point(0, 40))
.split()
return part return part
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const pattern = new design() const pattern = new design()
pattern.draft() pattern.draft()
expect(pattern.store.logs.error[0]).to.equal('Called `Path.split(point)` but `point` is not a `Point` object') expect(pattern.store.logs.error[0]).to.equal(
'Called `Path.split(point)` but `point` is not a `Point` object'
)
}) })
}) })

View file

@ -5,41 +5,42 @@ const expect = chai.expect
describe('Pattern', () => { describe('Pattern', () => {
describe('Pattern.draft()', () => { describe('Pattern.draft()', () => {
const partA = { const partA = {
name: 'test.partA', name: 'test.partA',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'chest', 'waist'], optionalMeasurements: ['chest', 'waist'],
options: { options: {
optA: { pct: 40, min: 20, max: 80 } optA: { pct: 40, min: 20, max: 80 },
}, },
draft: () => { } draft: () => {},
} }
const partB = { const partB = {
name: 'test.partB', name: 'test.partB',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'], optionalMeasurements: ['knee'],
after: partA, after: partA,
plugins: [{ plugins: [
{
name: 'testPlugin', name: 'testPlugin',
hooks: { hooks: {
preRender: () => {} preRender: () => {},
}
}],
options: {
optB: { deg: 40, min: 20, max: 80 }
}, },
draft: () => { } },
],
options: {
optB: { deg: 40, min: 20, max: 80 },
},
draft: () => {},
} }
const partC = { const partC = {
name: 'test.partC', name: 'test.partC',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'], optionalMeasurements: ['knee'],
from: partB, from: partB,
options: { options: {
optC: { pct: 20, min: 10, max: 30 } optC: { pct: 20, min: 10, max: 30 },
}, },
draft: () => { } draft: () => {},
} }
const Pattern = new Design({ const Pattern = new Design({
@ -47,7 +48,7 @@ describe('Pattern', () => {
name: 'test', name: 'test',
version: '1.2.3', version: '1.2.3',
}, },
parts: [ partC ] parts: [partC],
}) })
const pattern = new Pattern() const pattern = new Pattern()
@ -84,42 +85,44 @@ describe('Pattern', () => {
const partA = { const partA = {
name: 'test.partA', name: 'test.partA',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'chest', 'waist'], optionalMeasurements: ['chest', 'waist'],
options: { options: {
optA: { pct: 40, min: 20, max: 80 } optA: { pct: 40, min: 20, max: 80 },
}, },
draft: () => { } draft: () => {},
} }
const partB = { const partB = {
name: 'test.partB', name: 'test.partB',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'], optionalMeasurements: ['knee'],
after: partA, after: partA,
plugins: [{ plugins: [
{
name: 'testPlugin', name: 'testPlugin',
hooks: { hooks: {
preRender: () => {} preRender: () => {},
}
}],
options: {
optB: { deg: 40, min: 20, max: 80 }
}, },
draft: () => { } },
],
options: {
optB: { deg: 40, min: 20, max: 80 },
},
draft: () => {},
} }
const partC = { const partC = {
name: 'test.partC', name: 'test.partC',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'], optionalMeasurements: ['knee'],
options: { options: {
optC: { pct: 20, min: 10, max: 30 } optC: { pct: 20, min: 10, max: 30 },
}, },
draft: () => { } draft: () => {},
} }
const Pattern = new Design({ const Pattern = new Design({
parts: [ partA, partB, partC ] parts: [partA, partB, partC],
}) })
const pattern = new Pattern({ const pattern = new Pattern({
only: [ 'test.partB' ] only: ['test.partB'],
}) })
pattern.init() pattern.init()
expect(pattern.needs('test.partA')).to.equal(true) expect(pattern.needs('test.partA')).to.equal(true)
@ -131,42 +134,44 @@ describe('Pattern', () => {
const partA = { const partA = {
name: 'test.partA', name: 'test.partA',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'chest', 'waist'], optionalMeasurements: ['chest', 'waist'],
options: { options: {
optA: { pct: 40, min: 20, max: 80 } optA: { pct: 40, min: 20, max: 80 },
}, },
draft: () => { } draft: () => {},
} }
const partB = { const partB = {
name: 'test.partB', name: 'test.partB',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'], optionalMeasurements: ['knee'],
after: partA, after: partA,
plugins: [{ plugins: [
{
name: 'testPlugin', name: 'testPlugin',
hooks: { hooks: {
preRender: () => {} preRender: () => {},
}
}],
options: {
optB: { deg: 40, min: 20, max: 80 }
}, },
draft: () => { } },
],
options: {
optB: { deg: 40, min: 20, max: 80 },
},
draft: () => {},
} }
const partC = { const partC = {
name: 'test.partC', name: 'test.partC',
measurements: ['head', 'knee'], measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'], optionalMeasurements: ['knee'],
options: { options: {
optC: { pct: 20, min: 10, max: 30 } optC: { pct: 20, min: 10, max: 30 },
}, },
draft: () => { } draft: () => {},
} }
const Pattern = new Design({ const Pattern = new Design({
parts: [ partA, partB, partC ] parts: [partA, partB, partC],
}) })
const pattern = new Pattern({ const pattern = new Pattern({
only: [ 'test.partB' ] only: ['test.partB'],
}) })
pattern.init() pattern.init()
expect(pattern.wants('test.partA')).to.equal(false) expect(pattern.wants('test.partA')).to.equal(false)
@ -337,5 +342,4 @@ describe('Pattern', () => {
*/ */
}) })

View file

@ -610,29 +610,29 @@ describe('Pattern', () => {
expect(count).to.equal(1) expect(count).to.equal(1)
}) })
it("Pattern.init() should register multiple methods on a single hook", () => { it('Pattern.init() should register multiple methods on a single hook', () => {
const plugin = { const plugin = {
name: "test", name: 'test',
version: "0.1-test", version: '0.1-test',
hooks: { hooks: {
preDraft: [ preDraft: [
function(pattern) { function (pattern) {
count++; count++
},
function (pattern) {
count++
},
],
}, },
function(pattern) {
count++;
} }
]
}
};
const Pattern = new Design() const Pattern = new Design()
const pattern = new Pattern() const pattern = new Pattern()
let count = 0 let count = 0
pattern._draft = () => {} pattern._draft = () => {}
pattern.use(plugin) pattern.use(plugin)
pattern.draft() pattern.draft()
expect(count).to.equal(2); expect(count).to.equal(2)
}); })
it('Should check whether created parts get the pattern context', () => { it('Should check whether created parts get the pattern context', () => {
const Pattern = new Design() const Pattern = new Design()
@ -660,7 +660,6 @@ describe('Pattern', () => {
expect(Array.isArray(part.context.store.logs.warning)).to.equal(true) expect(Array.isArray(part.context.store.logs.warning)).to.equal(true)
expect(Array.isArray(part.context.store.logs.error)).to.equal(true) expect(Array.isArray(part.context.store.logs.error)).to.equal(true)
}) })
}) })
describe('Pattern.settings', () => { describe('Pattern.settings', () => {

View file

@ -12,9 +12,9 @@ describe('Snapped options', () => {
name: 'test', name: 'test',
options: { options: {
test: { pct: 30, min: 0, max: 100, snap: 12, toAbs }, test: { pct: 30, min: 0, max: 100, snap: 12, toAbs },
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements }).draft() const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements }).draft() const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(60) expect(patternA.settings.absoluteOptions.test).to.equal(60)
@ -32,9 +32,9 @@ describe('Snapped options', () => {
toAbs, toAbs,
snap: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144], snap: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144],
}, },
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements }).draft() const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements }).draft() const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements }).draft() const patternC = new design({ options: { test: 0.97 }, measurements }).draft()
@ -57,9 +57,9 @@ describe('Snapped options', () => {
imperial: [25.4, 50.8, 76.2, 101.6], imperial: [25.4, 50.8, 76.2, 101.6],
}, },
}, },
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'metric' }).draft() const patternA = new design({ options: { test: 0.13 }, measurements, units: 'metric' }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'metric' }).draft() const patternB = new design({ options: { test: 0.27 }, measurements, units: 'metric' }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'metric' }).draft() const patternC = new design({ options: { test: 0.97 }, measurements, units: 'metric' }).draft()
@ -84,13 +84,29 @@ describe('Snapped options', () => {
imperial: [25.4, 50.8, 76.2, 101.6], imperial: [25.4, 50.8, 76.2, 101.6],
}, },
}, },
},
} }
} const design = new Design({ parts: [part] })
const design = new Design({ parts: [ part ] }) const patternA = new design({
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'imperial' }).draft() options: { test: 0.13 },
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'imperial' }).draft() measurements,
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'imperial' }).draft() units: 'imperial',
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'imperial' }).draft() }).draft()
const patternB = new design({
options: { test: 0.27 },
measurements,
units: 'imperial',
}).draft()
const patternC = new design({
options: { test: 0.97 },
measurements,
units: 'imperial',
}).draft()
const patternD = new design({
options: { test: 0.01 },
measurements,
units: 'imperial',
}).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(50.8) expect(patternA.settings.absoluteOptions.test).to.equal(50.8)
expect(patternB.settings.absoluteOptions.test).to.equal(101.6) expect(patternB.settings.absoluteOptions.test).to.equal(101.6)
expect(patternC.settings.absoluteOptions.test).to.equal(388) expect(patternC.settings.absoluteOptions.test).to.equal(388)

View file

@ -514,7 +514,7 @@ describe('Utils', () => {
expect(result.toAbs(0.0123, { measurements })).to.equal(12.3) expect(result.toAbs(0.0123, { measurements })).to.equal(12.3)
expect(result.fromAbs(12.3, { measurements })).to.equal(0.0123) expect(result.fromAbs(12.3, { measurements })).to.equal(0.0123)
}) })
/* /*
it('Should generate a part transform', () => { it('Should generate a part transform', () => {
const part = { const part = {
name: 'test', name: 'test',