From ade70da54b9bdf7c37d53782426212ac7add5eb6 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Mon, 3 Sep 2018 12:20:51 +0200 Subject: [PATCH] :sparkles: Added support for different option types --- src/pattern.js | 9 +++++---- tests/pattern.test.js | 44 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/pattern.js b/src/pattern.js index d4c97413cd1..7e14c16eb38 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -39,12 +39,13 @@ export default function Pattern(config = false) { for (let i in config.options) { let option = config.options[i]; if (typeof option === "object") { - if (typeof option.val !== "undefined") this.options[i] = option.val / 100; + if (typeof option.pct !== "undefined") this.options[i] = option.pct / 100; else if (typeof option.mm !== "undefined") this.options[i] = option.mm; - else if (typeof option.angle !== "undefined") - this.options[i] = option.angle; + else if (typeof option.deg !== "undefined") this.options[i] = option.deg; + else if (typeof option.dflt !== "undefined") + this.options[i] = option.dflt; else throw "Unknown option type"; - } else if (typeof option === "number") { + } else { this.options[i] = option; } } diff --git a/tests/pattern.test.js b/tests/pattern.test.js index 87d7cdbc0c4..7c89abdec1e 100644 --- a/tests/pattern.test.js +++ b/tests/pattern.test.js @@ -6,7 +6,7 @@ it("Pattern constructor should initialize object", () => { foo: "bar", options: { constant: 2, - percentage: { val: 30, min: 0, max: 100 } + percentage: { pct: 30, min: 0, max: 100 } } }); expect(pattern.width).to.equal(false); @@ -28,16 +28,54 @@ it("Should throw exception upon draft", () => { it("Should load percentage options", () => { let pattern = new freesewing.Pattern({ options: { - test: { val: 30 } + test: { pct: 30 } } }); expect(pattern.options.test).to.equal(0.3); }); +it("Should load millimeter options", () => { + let pattern = new freesewing.Pattern({ + options: { + test: { mm: 30 } + } + }); + expect(pattern.options.test).to.equal(30); +}); + +it("Should load degree options", () => { + let pattern = new freesewing.Pattern({ + options: { + test: { deg: 15 } + } + }); + expect(pattern.options.test).to.equal(15); +}); + +it("Should load an array option", () => { + let pattern = new freesewing.Pattern({ + options: { + test: { dflt: "foo" } + } + }); + expect(pattern.options.test).to.equal("foo"); +}); + +it("Should throw an error for an unknown option", () => { + expect( + () => + new freesewing.Pattern({ + options: { + test: { foo: "bar" } + } + }) + ).to.throw(); +}); + it("Should sample an option", () => { let pattern = new freesewing.Pattern({ options: { - len: { val: 30, min: 0, max: 100 }, + len: { pct: 30, min: 0, max: 100 }, bonus: 10 } });