diff --git a/packages/core/src/pattern.mjs b/packages/core/src/pattern.mjs index 3bd340deff1..76064df35fa 100644 --- a/packages/core/src/pattern.mjs +++ b/packages/core/src/pattern.mjs @@ -80,7 +80,7 @@ Pattern.prototype.addPart = function (part) { * @return {object} this - The Pattern instance */ Pattern.prototype.draft = function () { - this.init() + this.__init() this.__runHooks('preDraft') // Iterate over the provided sets of settings (typically just one) @@ -152,7 +152,7 @@ Pattern.prototype.draft = function () { * @return {object} config - The initialized config */ Pattern.prototype.getConfig = function () { - return this.init().config + return this.__init().config } /** Returns props required to render this pattern through @@ -206,47 +206,13 @@ Pattern.prototype.getRenderProps = function () { return props } -/** - * Initializes the pattern coniguration and settings - * - * @return {object} this - The Pattern instance - */ -Pattern.prototype.init = function () { - this.__runHooks('preInit') - /* - * We allow late-stage updating of the design config (adding parts for example) - * so we need to do the things we used to do in the contructor at a later stage. - * This methods does that, and resolves the design config + user settings - */ - this.__resolveParts() // Resolves parts - .__resolveDependencies() // Resolves dependencies - .__resolveDraftOrder() // Resolves draft order - .__loadPlugins() // Loads plugins - .__filterOptionalMeasurements() // Removes required m's from optional list - .__loadConfigData() // Makes config data available in store - .__loadOptionDefaults() // Merges default options with user provided ones - - // Say hello - this.stores[0].log.info( - `New \`${this.stores[0].get('data.name', 'No Name')}:` + - `${this.stores[0].get( - 'data.version', - 'No version' - )}\` pattern using \`@freesewing/core:${version}\`` - ) - this.stores[0].log.info(`Pattern initialized. Draft order is: ${this.__draftOrder.join(', ')}`) - this.__runHooks('postInit') - - return this -} - /** * Handles pattern sampling * * @return {object} this - The Pattern instance */ Pattern.prototype.sample = function () { - this.init() + this.__init() if (this.settings[0].sample.type === 'option') { return this.sampleOption(this.settings[0].sample.option) } else if (this.settings[0].sample.type === 'measurement') { @@ -265,7 +231,7 @@ Pattern.prototype.sampleMeasurement = function (measurementName) { this.stores[0].log.debug(`Sampling measurement \`${measurementName}\``) this.__runHooks('preSample') this.__applySettings(this.__measurementSets(measurementName)) - this.init() + this.__init() this.__runHooks('postSample') return this.draft() @@ -280,7 +246,7 @@ Pattern.prototype.sampleModels = function (models, focus = false) { this.stores[0].log.debug(`Sampling models \`${Object.keys(models).join(', ')}\``) this.__runHooks('preSample') this.__applySettings(this.__modelSets(models, focus)) - this.init() + this.__init() this.__runHooks('postSample') return this.draft() @@ -295,7 +261,7 @@ Pattern.prototype.sampleOption = function (optionName) { this.stores[0].log.debug(`Sampling option \`${optionName}\``) this.__runHooks('preSample') this.__applySettings(this.__optionSets(optionName)) - this.init() + this.__init() this.__runHooks('postSample') return this.draft() @@ -460,6 +426,40 @@ Pattern.prototype.__filterOptionalMeasurements = function () { return this } +/** + * Initializes the pattern coniguration and settings + * + * @return {object} this - The Pattern instance + */ +Pattern.prototype.__init = function () { + this.__runHooks('preInit') + /* + * We allow late-stage updating of the design config (adding parts for example) + * so we need to do the things we used to do in the contructor at a later stage. + * This methods does that, and resolves the design config + user settings + */ + this.__resolveParts() // Resolves parts + .__resolveDependencies() // Resolves dependencies + .__resolveDraftOrder() // Resolves draft order + .__loadPlugins() // Loads plugins + .__filterOptionalMeasurements() // Removes required m's from optional list + .__loadConfigData() // Makes config data available in store + .__loadOptionDefaults() // Merges default options with user provided ones + + // Say hello + this.stores[0].log.info( + `New \`${this.stores[0].get('data.name', 'No Name')}:` + + `${this.stores[0].get( + 'data.version', + 'No version' + )}\` pattern using \`@freesewing/core:${version}\`` + ) + this.stores[0].log.info(`Pattern initialized. Draft order is: ${this.__draftOrder.join(', ')}`) + this.__runHooks('postInit') + + return this +} + /** * Checks whether a part is hidden in the config * diff --git a/packages/core/tests/pattern-draft.test.mjs b/packages/core/tests/pattern-draft.test.mjs index d7257a96acf..0b549d032ac 100644 --- a/packages/core/tests/pattern-draft.test.mjs +++ b/packages/core/tests/pattern-draft.test.mjs @@ -77,7 +77,7 @@ describe('Pattern', () => { const pattern = new Pattern({ only: ['test.partB'], }) - pattern.init() + pattern.__init() expect(pattern.__needs('test.partA')).to.equal(true) expect(pattern.__needs('test.partB')).to.equal(true) expect(pattern.__needs('test.partC')).to.equal(false) @@ -126,7 +126,7 @@ describe('Pattern', () => { const pattern = new Pattern({ only: ['test.partB'], }) - pattern.init() + pattern.__init() expect(pattern.__wants('test.partA')).to.equal(false) expect(pattern.__wants('test.partB')).to.equal(true) expect(pattern.__wants('test.partC')).to.equal(false) diff --git a/packages/core/tests/pattern-init.test.mjs b/packages/core/tests/pattern-init.test.mjs index 4da857b2497..5d6e0fdb586 100644 --- a/packages/core/tests/pattern-init.test.mjs +++ b/packages/core/tests/pattern-init.test.mjs @@ -65,7 +65,7 @@ describe('Pattern', () => { }) }) - describe('Pattern.init()', () => { + describe('Pattern.__init()', () => { const partA = { name: 'test.partA', measurements: ['head', 'knee'], @@ -112,27 +112,27 @@ describe('Pattern', () => { parts: [partC], }) const pattern = new Pattern() - pattern.init() + pattern.__init() - it('Pattern.init() should resolve all measurements', () => { + it('Pattern.__init() should resolve all measurements', () => { expect( [...pattern.config.measurements, ...pattern.config.optionalMeasurements].length ).to.equal(4) }) - it('Pattern.init() should resolve required measurements', () => { + it('Pattern.__init() should resolve required measurements', () => { expect(pattern.config.measurements.length).to.equal(2) expect(pattern.config.measurements[0]).to.equal('head') expect(pattern.config.measurements[1]).to.equal('knee') }) - it('Pattern.init() should resolve optional measurements', () => { + it('Pattern.__init() should resolve optional measurements', () => { expect(pattern.config.optionalMeasurements.length).to.equal(2) expect(pattern.config.optionalMeasurements[0]).to.equal('chest') expect(pattern.config.optionalMeasurements[1]).to.equal('waist') }) - it('Pattern.init() should resolve options', () => { + it('Pattern.__init() should resolve options', () => { expect(Object.keys(pattern.config.options).length).to.equal(3) for (const [key, value] of Object.entries(partA.options.optA)) { expect(pattern.config.options.optA[key]).to.equal(value) @@ -145,20 +145,20 @@ describe('Pattern', () => { } }) - it('Pattern.init() should resolve parts', () => { + it('Pattern.__init() should resolve parts', () => { expect(pattern.config.parts.length).to.equal(3) }) - it('Pattern.init() should resolve plugins', () => { + it('Pattern.__init() should resolve plugins', () => { expect(pattern.config.plugins.length).to.equal(1) }) - it('Pattern.init() should set config data in the store', () => { + it('Pattern.__init() should set config data in the store', () => { expect(pattern.stores[0].get('data.name')).to.equal('test') expect(pattern.stores[0].get('data.version')).to.equal('1.2.3') }) - it('Pattern.init() should resolve dependencies', () => { + it('Pattern.__init() should resolve dependencies', () => { expect(typeof pattern.config.resolvedDependencies).to.equal('object') expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true) expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0) @@ -175,7 +175,7 @@ describe('Pattern', () => { ).to.equal(true) }) - it('Pattern.init() should resolve the draft order', () => { + it('Pattern.__init() should resolve the draft order', () => { expect(Array.isArray(pattern.config.draftOrder)).to.equal(true) expect(pattern.config.draftOrder[0]).to.equal('test.partA') expect(pattern.config.draftOrder[1]).to.equal('test.partB') @@ -184,7 +184,7 @@ describe('Pattern', () => { // I am aware this does too much for one unit test, but this is to simplify TDD // we can split it up later - it('Pattern.init() should resolve nested injections', () => { + it('Pattern.__init() should resolve nested injections', () => { const partA = { name: 'partA', options: { optionA: { bool: true } }, @@ -336,7 +336,7 @@ describe('Pattern', () => { expect(pattern.parts[0].partR.paths.r.ops[1].to.y).to.equal(44) }) - it('Pattern.init() should resolve nested dependencies', () => { + it('Pattern.__init() should resolve nested dependencies', () => { const partA = { name: 'partA', options: { optionA: { bool: true } }, @@ -478,7 +478,7 @@ describe('Pattern', () => { expect(pattern.parts[0].partD.paths.d.ops[1].to.y).to.equal(44) }) - it('Pattern.init() should load a single plugin', () => { + it('Pattern.__init() should load a single plugin', () => { const plugin = { name: 'example', version: 1, @@ -495,11 +495,11 @@ describe('Pattern', () => { } const design = new Design({ parts: [part] }) const pattern = new design() - pattern.init() + pattern.__init() expect(pattern.hooks.preRender.length).to.equal(1) }) - it('Pattern.init() should load array of plugins', () => { + it('Pattern.__init() should load array of plugins', () => { const plugin1 = { name: 'example1', version: 1, @@ -521,10 +521,10 @@ describe('Pattern', () => { const design = new Design({ plugins: [plugin1, plugin2] }) const pattern = new design() - pattern.init() + pattern.__init() expect(pattern.hooks.preRender.length).to.equal(2) }) - it('Pattern.init() should load conditional plugin', () => { + it('Pattern.__init() should load conditional plugin', () => { const plugin = { name: 'example', version: 1, @@ -537,11 +537,11 @@ describe('Pattern', () => { const condition = () => true const design = new Design({ plugins: [{ plugin, condition }] }) const pattern = new design() - pattern.init() + pattern.__init() expect(pattern.hooks.preRender.length).to.equal(1) }) - it('Pattern.init() should not load conditional plugin', () => { + it('Pattern.__init() should not load conditional plugin', () => { const plugin = { name: 'example', version: 1, @@ -557,7 +557,7 @@ describe('Pattern', () => { expect(pattern.hooks.preRender.length).to.equal(0) }) - it('Pattern.init() should load multiple conditional plugins', () => { + it('Pattern.__init() should load multiple conditional plugins', () => { const plugin = { name: 'example', version: 1, @@ -576,7 +576,7 @@ describe('Pattern', () => { ], }) const pattern = new design() - pattern.init() + pattern.__init() expect(pattern.hooks.preRender.length).to.equal(1) }) @@ -621,11 +621,11 @@ describe('Pattern', () => { parts: [ part1, part2 ] }) const pattern = new design() - pattern.init() + pattern.__init() expect(pattern.hooks.preRender.length).to.equal(2) }) - it('Pattern.init() should register a hook via on', () => { + it('Pattern.__init() should register a hook via on', () => { const Pattern = new Design() const pattern = new Pattern() let count = 0 @@ -636,7 +636,7 @@ describe('Pattern', () => { expect(count).to.equal(1) }) - it('Pattern.init() should register a hook from a plugin', () => { + it('Pattern.__init() should register a hook from a plugin', () => { const Pattern = new Design() const pattern = new Pattern() let count = 0 @@ -655,7 +655,7 @@ describe('Pattern', () => { 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 = { name: 'test', version: '0.1-test', @@ -737,7 +737,7 @@ describe('Pattern', () => { parts: [part], }) const pattern = new Pattern() - pattern.init() + pattern.__init() it('Pattern settings should contain percentage options', () => { expect(pattern.settings[0].options.pct).to.equal(0.3) @@ -774,7 +774,7 @@ describe('Pattern', () => { parts: [part], }) const pattern = new Pattern() - expect(() => pattern.init()).to.throw() + expect(() => pattern.__init()).to.throw() }) }) }) diff --git a/packages/core/tests/pattern-other.test.mjs b/packages/core/tests/pattern-other.test.mjs index c289b298480..e2cbe8e12da 100644 --- a/packages/core/tests/pattern-other.test.mjs +++ b/packages/core/tests/pattern-other.test.mjs @@ -104,7 +104,7 @@ describe('Pattern', () => { } const design = new Design() const pattern = new design({ only: ['test']}) - pattern.init() + pattern.__init() expect(pattern.__isPartHidden('test')).to.equal(false) }) diff --git a/packages/core/tests/stacks.test.mjs b/packages/core/tests/stacks.test.mjs index cc3871ef54d..d81c76d84e2 100644 --- a/packages/core/tests/stacks.test.mjs +++ b/packages/core/tests/stacks.test.mjs @@ -5,7 +5,7 @@ const expect = chai.expect describe('Stacks', () => { - describe('Pattern.init()', () => { + describe('Pattern.__init()', () => { const partA = { name: 'test.partA', measurements: ['head'], @@ -69,7 +69,7 @@ describe('Stacks', () => { }) pattern.draft() - it('Pattern.init() should resolve dependencies', () => { + it('Pattern.__init() should resolve dependencies', () => { expect(typeof pattern.config.resolvedDependencies).to.equal('object') expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true) expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0)