fix(core): Put getId into a closure
This commit is contained in:
parent
1a25a9e379
commit
3b53f4c883
2 changed files with 33 additions and 17 deletions
|
@ -77,18 +77,6 @@ Part.prototype.attr = function (name, value, overwrite = false) {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns on unused ID (unused in this part)
|
|
||||||
*
|
|
||||||
* @param {string} prefix - An optional prefix to apply to the ID
|
|
||||||
* @return {string} id - The id
|
|
||||||
*/
|
|
||||||
Part.prototype.getId = function (prefix = '') {
|
|
||||||
this.freeId += 1
|
|
||||||
|
|
||||||
return prefix + this.freeId
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hide the part
|
* Hide the part
|
||||||
*
|
*
|
||||||
|
@ -125,7 +113,7 @@ Part.prototype.shorthand = function () {
|
||||||
const shorthand = {
|
const shorthand = {
|
||||||
complete,
|
complete,
|
||||||
context: this.context,
|
context: this.context,
|
||||||
getId: this.getId,
|
getId: this.__getIdClosure(),
|
||||||
hide: this.hide,
|
hide: this.hide,
|
||||||
log: this.context.store.log,
|
log: this.context.store.log,
|
||||||
paperless,
|
paperless,
|
||||||
|
@ -289,6 +277,22 @@ Part.prototype.__boundary = function () {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a closure holding a getId method (returns an ID unused in this part)
|
||||||
|
*
|
||||||
|
* @return {function} getId - The getId function
|
||||||
|
*/
|
||||||
|
Part.prototype.__getIdClosure = function () {
|
||||||
|
const self = this
|
||||||
|
const method = function (prefix = '') {
|
||||||
|
self.freeId += 1
|
||||||
|
|
||||||
|
return prefix + self.freeId
|
||||||
|
}
|
||||||
|
|
||||||
|
return method
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies point/path/snippet data from part orig into this
|
* Copies point/path/snippet data from part orig into this
|
||||||
*
|
*
|
||||||
|
|
|
@ -58,10 +58,22 @@ describe('Part', () => {
|
||||||
expect(pattern.parts[0].test.points.macro.y).to.equal(456)
|
expect(pattern.parts[0].test.points.macro.y).to.equal(456)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return a free ID', () => {
|
it('Should return a free ID in draft method', () => {
|
||||||
const part = new Part()
|
let id = null
|
||||||
const free = part.getId()
|
const part = {
|
||||||
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
|
name: 'test',
|
||||||
|
draft: ({ getId, part }) => {
|
||||||
|
console.log(getId)
|
||||||
|
id = getId()
|
||||||
|
id = getId()
|
||||||
|
id = getId()
|
||||||
|
|
||||||
|
return part
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const design = new Design({ parts: [part] })
|
||||||
|
new design().draft()
|
||||||
|
expect(id).to.equal('3')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return a function from __unitsClosure', () => {
|
it('Should return a function from __unitsClosure', () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue