Better Collection#_resolveItem().
This commit is contained in:
parent
ef2eec4c4a
commit
964e461597
@ -123,22 +123,31 @@ export default class Collection extends EventEmitter {
|
|||||||
return Object.hasOwnProperty.call(this._items, key)
|
return Object.hasOwnProperty.call(this._items, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
_resolveItem (keyOrObjectWithId, valueIfKey = null) {
|
_isValidKey (key) {
|
||||||
let value
|
return typeof key === 'number' || typeof key === 'string'
|
||||||
let key = this.getId(keyOrObjectWithId)
|
}
|
||||||
|
|
||||||
if (undefined === key) {
|
_assertValidKey (key) {
|
||||||
if (arguments.length < 2) {
|
if (!this._isValidKey(key)) {
|
||||||
throw new IllegalAdd('Missing value, or object value does not provide id/key')
|
throw new InvalidKey(key)
|
||||||
} else {
|
}
|
||||||
key = keyOrObjectWithId
|
}
|
||||||
value = valueIfKey
|
|
||||||
}
|
_resolveItem (keyOrObjectWithId, valueIfKey = undefined) {
|
||||||
} else {
|
if (valueIfKey !== undefined) {
|
||||||
value = keyOrObjectWithId
|
this._assertValidKey(keyOrObjectWithId)
|
||||||
|
|
||||||
|
return [keyOrObjectWithId, valueIfKey]
|
||||||
}
|
}
|
||||||
|
|
||||||
return [key, value]
|
if (this._isValidKey(keyOrObjectWithId)) {
|
||||||
|
return [keyOrObjectWithId]
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = this.getId(keyOrObjectWithId)
|
||||||
|
this._assertValidKey(key)
|
||||||
|
|
||||||
|
return [key, keyOrObjectWithId]
|
||||||
}
|
}
|
||||||
|
|
||||||
_assertHas (key) {
|
_assertHas (key) {
|
||||||
@ -153,7 +162,7 @@ export default class Collection extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
add (keyOrObjectWithId, valueIfKey = null) {
|
add (keyOrObjectWithId, valueIfKey = undefined) {
|
||||||
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
||||||
this._assertHasNot(key)
|
this._assertHasNot(key)
|
||||||
|
|
||||||
@ -162,7 +171,7 @@ export default class Collection extends EventEmitter {
|
|||||||
this._touch('add', key)
|
this._touch('add', key)
|
||||||
}
|
}
|
||||||
|
|
||||||
set (keyOrObjectWithId, valueIfKey = null) {
|
set (keyOrObjectWithId, valueIfKey = undefined) {
|
||||||
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
||||||
|
|
||||||
const action = this.has(key) ? 'update' : 'add'
|
const action = this.has(key) ? 'update' : 'add'
|
||||||
@ -186,7 +195,7 @@ export default class Collection extends EventEmitter {
|
|||||||
this._assertHas(key)
|
this._assertHas(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
update (keyOrObjectWithId, valueIfKey = null) {
|
update (keyOrObjectWithId, valueIfKey = undefined) {
|
||||||
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
||||||
this._assertHas(key)
|
this._assertHas(key)
|
||||||
|
|
||||||
@ -195,7 +204,7 @@ export default class Collection extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
touch (keyOrObjectWithId) {
|
touch (keyOrObjectWithId) {
|
||||||
const [key] = this._resolveItem(keyOrObjectWithId, null)
|
const [key] = this._resolveItem(keyOrObjectWithId)
|
||||||
this._assertHas(key)
|
this._assertHas(key)
|
||||||
const value = this.get(key)
|
const value = this.get(key)
|
||||||
if (typeof value !== 'object' || value === null) {
|
if (typeof value !== 'object' || value === null) {
|
||||||
@ -208,7 +217,7 @@ export default class Collection extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
remove (keyOrObjectWithId) {
|
remove (keyOrObjectWithId) {
|
||||||
const [key] = this._resolveItem(keyOrObjectWithId, null)
|
const [key] = this._resolveItem(keyOrObjectWithId)
|
||||||
this._assertHas(key)
|
this._assertHas(key)
|
||||||
|
|
||||||
delete this._items[key]
|
delete this._items[key]
|
||||||
|
Loading…
Reference in New Issue
Block a user