Prefer the term of item over entry.

This commit is contained in:
Julien Fontanet 2015-04-08 11:49:25 +02:00
parent b70e0e3e2b
commit 0303558ae1
3 changed files with 46 additions and 46 deletions

View File

@ -25,19 +25,19 @@ var col = new Collection()
### Manipulation
**Inserting a new entry**
**Inserting a new item**
```javascript
col.add('foo', true)
```
**Updating an existing entry**
**Updating an existing item**
```javascript
col.update('foo', false)
```
**Inserting or updating an entry**
**Inserting or updating an item**
```javascript
col.set('bar', true)
@ -45,7 +45,7 @@ col.set('bar', true)
**Notifying an external update**
> If an entry is an object, it can be updated directly without using
> If an item is an object, it can be updated directly without using
> the `set`/`update` methods.
>
> To make sure the collection stays in sync and the correct events are
@ -60,20 +60,20 @@ baz.prop = true
col.touch('baz')
```
> Because this is a much used pattern, `touch` returns the entry to
> Because this is a much used pattern, `touch` returns the item to
> allow its direct modification.
```javascript
col.touch('baz').prop = false
```
**Removing an existing entry**
**Removing an existing item**
```javascript
col.remove('bar')
```
**Removing all entries**
**Removing all items**
```javascript
col.clear()
@ -81,19 +81,19 @@ col.clear()
### Query
**Checking the existence of an entry**
**Checking the existence of an item**
```javascript
var hasBar = col.has('bar')
```
**Getting an existing entry**
**Getting an existing item**
```javascript
var foo = col.get('foo')
// The second parameter can be used to specify a fallback in case the
// entry does not exist.
// item does not exist.
var bar = col.get('bar', 6.28)
```
@ -106,17 +106,17 @@ var bar = col.get('bar', 6.28)
```javascript
var _ = require('lodash')
// Prints all the entries.
// Prints all the items.
_.forEach(col.all, function (value, key) {
console.log('- %s: %j', key, value)
})
// Finds all the entries which are objects and have a property
// Finds all the items which are objects and have a property
// `active` which equals `true`.
var results = _.where(col.all, { active: true })
```
**Getting the number of entries**
**Getting the number of items**
```javascript
var size = col.size
@ -129,7 +129,7 @@ var size = col.size
> addition followed by an update will result only in a single
> addition.
**New entries**
**New items**
```javascript
col.on('add', (added) => {
@ -139,7 +139,7 @@ col.on('add', (added) => {
})
```
**Updated entries**
**Updated items**
```javascript
col.on('update', (updated) => {
@ -149,13 +149,13 @@ col.on('update', (updated) => {
})
```
**Removed entries**
**Removed items**
```javascript
col.on('remove', (removed) => {
// For consistency, `removed` is also a map but contrary to `added`
// and `updated`, the values associated to the keys are not
// significant since the entries have already be removed.
// significant since the items have already be removed.
forEach(removed, (value, key) => {
console.log('- %s', key)

View File

@ -2,10 +2,10 @@ import {EventEmitter} from 'events'
import makeError from 'make-error'
export const BufferAlreadyFlushed = makeError('BufferAlreadyFlushed')
export const DuplicateEntry = makeError('DuplicateEntry')
export const DuplicateItem = makeError('DuplicateItem')
export const IllegalAdd = makeError('IllegalAdd')
export const IllegalTouch = makeError('IllegalTouch')
export const NoSuchEntry = makeError('NoSuchEntry')
export const NoSuchItem = makeError('NoSuchItem')
function isNotEmpty (map) {
/* eslint no-unused-vars: 0*/
@ -22,7 +22,7 @@ export default class Collection extends EventEmitter {
this._buffer = Object.create(null)
this._buffering = 0
this._map = Object.create(null)
this._items = Object.create(null)
this._size = 0
}
@ -47,14 +47,14 @@ export default class Collection extends EventEmitter {
}
for (let key in this._buffer) {
data[this._buffer[key]][key] = this._map[key]
data[this._buffer[key]][key] = this._items[key]
}
['add', 'update', 'remove'].forEach(action => {
const entries = data[action]
const items = data[action]
if (isNotEmpty(entries)) {
this.emit(action, entries)
if (isNotEmpty(items)) {
this.emit(action, items)
}
})
@ -92,10 +92,10 @@ export default class Collection extends EventEmitter {
}
has (key) {
return Object.hasOwnProperty.call(this._map, key)
return Object.hasOwnProperty.call(this._items, key)
}
_resolveEntry (keyOrObjectWithId, valueIfKey = null) {
_resolveItem (keyOrObjectWithId, valueIfKey = null) {
let value
let key = (undefined !== keyOrObjectWithId) ?
this.getId(keyOrObjectWithId) :
@ -117,21 +117,21 @@ export default class Collection extends EventEmitter {
_assertHas (key) {
if (!this.has(key)) {
throw new NoSuchEntry('No ' + key + ' entry')
throw new NoSuchItem('No ' + key + ' item')
}
}
_assertHasNot (key) {
if (this.has(key)) {
throw new DuplicateEntry('Attempt to duplicate ' + key + ' entry')
throw new DuplicateItem('Attempt to duplicate ' + key + ' item')
}
}
add (keyOrObjectWithId, valueIfKey = null) {
const [key, value] = this._resolveEntry(keyOrObjectWithId, valueIfKey)
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
this._assertHasNot(key)
this._map[key] = value
this._items[key] = value
this._size++
this._touch('add', key)
@ -139,10 +139,10 @@ export default class Collection extends EventEmitter {
}
set (keyOrObjectWithId, valueIfKey = null) {
const [key, value] = this._resolveEntry(keyOrObjectWithId, valueIfKey)
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
const action = this.has(key) ? 'update' : 'add'
this._map[key] = value
this._items[key] = value
if (action === 'add') {
this._size++
}
@ -153,29 +153,29 @@ export default class Collection extends EventEmitter {
get (key, defaultValue) {
if (this.has(key)) {
return this._map[key]
return this._items[key]
}
if (arguments.length > 1) {
return defaultValue
}
// Throws a NoSuchEntry.
// Throws a NoSuchItem.
this._assertHas(key)
}
update (keyOrObjectWithId, valueIfKey = null) {
const [key, value] = this._resolveEntry(keyOrObjectWithId, valueIfKey)
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
this._assertHas(key)
this._map[key] = value
this._items[key] = value
this._touch('update', key)
return this
}
touch (keyOrObjectWithId) {
const [key] = this._resolveEntry(keyOrObjectWithId, null)
const [key] = this._resolveItem(keyOrObjectWithId, null)
this._assertHas(key)
const value = this.get(key)
if (typeof value !== 'object' || value === null) {
@ -188,10 +188,10 @@ export default class Collection extends EventEmitter {
}
remove (keyOrObjectWithId) {
const [key] = this._resolveEntry(keyOrObjectWithId, null)
const [key] = this._resolveItem(keyOrObjectWithId, null)
this._assertHas(key)
delete this._map[key]
delete this._items[key]
this._size--
this._touch('remove', key)
@ -199,8 +199,8 @@ export default class Collection extends EventEmitter {
}
clear () {
for (let key in this._map) {
delete this._map[key]
for (let key in this._items) {
delete this._items[key]
this._size--
this._touch('remove', key)
}
@ -212,6 +212,6 @@ export default class Collection extends EventEmitter {
}
get all () {
return this._map
return this._items
}
}

View File

@ -1,6 +1,6 @@
/* eslint-env mocha */
import Collection, {DuplicateEntry, NoSuchEntry} from './index'
import Collection, {DuplicateItem, NoSuchItem} from './index'
import eventToPromise from 'event-to-promise'
import sinon from 'sinon'
@ -37,7 +37,7 @@ describe('Collection', function () {
})
it('throws an exception if the item already exists', function () {
expect(() => this.col.add('bar', true)).to.throw(DuplicateEntry)
expect(() => this.col.add('bar', true)).to.throw(DuplicateItem)
})
it('accepts an object with an id property', function () {
@ -70,7 +70,7 @@ describe('Collection', function () {
})
it('throws an exception if the item does not exist', function () {
expect(() => this.col.update('baz', true)).to.throw(NoSuchEntry)
expect(() => this.col.update('baz', true)).to.throw(NoSuchItem)
})
it('accepts an object with an id property', function () {
@ -102,7 +102,7 @@ describe('Collection', function () {
})
it('throws an exception if the item does not exist', function () {
expect(() => this.col.remove('baz', true)).to.throw(NoSuchEntry)
expect(() => this.col.remove('baz', true)).to.throw(NoSuchItem)
})
it('accepts an object with an id property', function () {