1
0
Fork 0

chore(core): Tweaks to new methods after review

This commit is contained in:
joostdecock 2022-07-31 09:40:40 +02:00
parent bc567874c1
commit 4de192835e
2 changed files with 19 additions and 32 deletions

View file

@ -361,16 +361,16 @@ Part.prototype.generateTransform = function(transforms) {
Part.prototype.addCut = function (cut=2, material='fabric', identical=false) { Part.prototype.addCut = function (cut=2, material='fabric', identical=false) {
if (cut === false) { if (cut === false) {
if (this.cut.materials[material]) delete this.cut.materials[material] if (this.cut.materials[material]) delete this.cut.materials[material]
else this.context.raise.error(`Tried to remove a material that is not set`) else this.context.raise.warning(`Tried to remove a material that is not set`)
return this return this
} }
if (typeof this.cut.materials[material] === 'undefined') this.cut.materials[material] = {} if (typeof this.cut.materials[material] === 'undefined') this.cut.materials[material] = {}
if (typeof cut !== 'number') { if (!(Number.isInteger(cut) && cut > -1)) {
this.context.raise.error(`Tried to set cut to a value that is not a number`) this.context.raise.error(`Tried to set cut to a value that is not a positive integer`)
return this return this
} }
if (typeof material !== 'string') { if (typeof material !== 'string') {
this.context.raise.error(`Tried to set material to a value that is not a string`) this.context.raise.warning(`Tried to set material to a value that is not a string`)
return this return this
} }
this.cut.materials[material].cut = cut this.cut.materials[material].cut = cut
@ -381,24 +381,13 @@ Part.prototype.addCut = function (cut=2, material='fabric', identical=false) {
/** Chainable way to remove (some) cut info */ /** Chainable way to remove (some) cut info */
Part.prototype.removeCut = function (material=false) { Part.prototype.removeCut = function (material=false) {
if (!material) { return this.addCut(false, material)
this.context.raise.warning('Called part.removeCut() without any parameters. Not removing anything')
return this
}
if (typeof material !== 'string') {
this.context.raise.error(`Tried to set material to a value that is not a string`)
return this
}
if (this.cut.materials[material]) delete this.cut.materials[material]
else this.context.raise.error(`Tried to remove a material that is not set`)
return this
} }
/** Chainable way to add the grain info */ /** Chainable way to add the grain info */
Part.prototype.setGrain = function (grain=false) { Part.prototype.setGrain = function (grain=false) {
if (grain === false) { if (grain === false) {
this.context.raise.warning('Called part.setGrain() without any parameters. Not changing anything') this.cut.grain = false
return this return this
} }
if (typeof grain !== 'number') { if (typeof grain !== 'number') {

View file

@ -289,15 +289,15 @@ it("Should generate an error if cut is not a number", () => {
let part = new pattern.Part(); let part = new pattern.Part();
part.addCut('a', 'fabric', true) part.addCut('a', 'fabric', true)
expect(pattern.events.error.length).to.equal(1) expect(pattern.events.error.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to set cut to a value that is not a number') expect(pattern.events.error[0]).to.equal('Tried to set cut to a value that is not a positive integer')
}); });
it("Should generate an error if material is not a string", () => { it("Should generate an warning if material is not a string", () => {
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
let part = new pattern.Part(); let part = new pattern.Part();
part.addCut(3, 4) part.addCut(3, 4)
expect(pattern.events.error.length).to.equal(1) expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to set material to a value that is not a string') expect(pattern.events.warning[0]).to.equal('Tried to set material to a value that is not a string')
}); });
it("Should generate an error when removing a material that is not set (through addCut)", () => { it("Should generate an error when removing a material that is not set (through addCut)", () => {
@ -305,8 +305,8 @@ it("Should generate an error when removing a material that is not set (through a
let part = new pattern.Part(); let part = new pattern.Part();
part.addCut(4, 'fabric', true) part.addCut(4, 'fabric', true)
part.addCut(false, 'lining') part.addCut(false, 'lining')
expect(pattern.events.error.length).to.equal(1) expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to remove a material that is not set') expect(pattern.events.warning[0]).to.equal('Tried to remove a material that is not set')
}); });
it("Should remove the part cut through addCut", () => { it("Should remove the part cut through addCut", () => {
@ -330,8 +330,8 @@ it("Should generate an error when removing a material that is not set (through r
let part = new pattern.Part(); let part = new pattern.Part();
part.addCut(4, 'fabric', true) part.addCut(4, 'fabric', true)
part.removeCut('lining') part.removeCut('lining')
expect(pattern.events.error.length).to.equal(1) expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to remove a material that is not set') expect(pattern.events.warning[0]).to.equal('Tried to remove a material that is not set')
}); });
it("Should generate an error when removing a material that is not set (through removeCut)", () => { it("Should generate an error when removing a material that is not set (through removeCut)", () => {
@ -339,8 +339,8 @@ it("Should generate an error when removing a material that is not set (through r
let part = new pattern.Part(); let part = new pattern.Part();
part.addCut(4, 'fabric', true) part.addCut(4, 'fabric', true)
part.removeCut(23) part.removeCut(23)
expect(pattern.events.error.length).to.equal(1) expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.error[0]).to.equal('Tried to set material to a value that is not a string') expect(pattern.events.warning[0]).to.equal('Tried to remove a material that is not set')
}); });
it("Should generate a warning when calling removeCut without parameters", () => { it("Should generate a warning when calling removeCut without parameters", () => {
@ -349,7 +349,7 @@ it("Should generate a warning when calling removeCut without parameters", () =>
part.addCut(4, 'fabric', true) part.addCut(4, 'fabric', true)
part.removeCut() part.removeCut()
expect(pattern.events.warning.length).to.equal(1) expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Called part.removeCut() without any parameters. Not removing anything') expect(pattern.events.warning[0]).to.equal('Tried to remove a material that is not set')
}); });
it("Should generate a warning when calling removeCut without parameters", () => { it("Should generate a warning when calling removeCut without parameters", () => {
@ -358,7 +358,7 @@ it("Should generate a warning when calling removeCut without parameters", () =>
part.addCut(4, 'fabric', true) part.addCut(4, 'fabric', true)
part.removeCut() part.removeCut()
expect(pattern.events.warning.length).to.equal(1) expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Called part.removeCut() without any parameters. Not removing anything') expect(pattern.events.warning[0]).to.equal('Tried to remove a material that is not set')
}); });
it("Should set the part grainline", () => { it("Should set the part grainline", () => {
@ -373,9 +373,7 @@ it("Should raise a warning when calling part.setGrain() without any parameters",
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
let part = new pattern.Part(); let part = new pattern.Part();
part.setGrain() part.setGrain()
expect(part.cut.grain).to.equal(90) expect(part.cut.grain).to.equal(false)
expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Called part.setGrain() without any parameters. Not changing anything')
}); });
it("Should raise an error when calling part.setGrain() with a value that is not a number", () => { it("Should raise an error when calling part.setGrain() with a value that is not a number", () => {