Skip to content

Commit

Permalink
ref #5 Use native Map. Store id in the decoration object
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu1kn committed Feb 16, 2018
1 parent f79e825 commit 044db6a
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions lib/text-decoration-collection.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@

const _ = require('lodash');

class TextDecorationCollection {

constructor({generateUuid}) {
this._generateUuid = generateUuid;

this._map = Object.create(null);
this._map = new Map();
}

add({pattern, colour, decorationType}) {
const id = this._generateUuid();
const decoration = {pattern, colour, decorationType};
this._map[id] = decoration;
return Object.assign({id}, decoration);
const decoration = {id, pattern, colour, decorationType};
this._map.set(id, decoration);
return decoration;
}

get(id) {
const value = this._map[id];
return value ? Object.assign({id}, value) : null;
return this._map.get(id);
}

remove(id) {
this._map[id] = null;
this._map.delete(id);
}

find(predicate) {
const id = _.findKey(this._map, decoration => decoration && predicate(decoration));
return id ? Object.assign({id}, this._map[id]) : null;
return Array.from(this._map.values())
.find(decoration => predicate(decoration)) || null;
}

toList() {
return _.reduce(this._map,
(result, value, key) => value ? [...result, Object.assign({id: key}, value)] : result,
[]
);
return Array.from(this._map.values())
.reduce((result, decoration) => [...result, decoration], []);
}

}
Expand Down

0 comments on commit 044db6a

Please sign in to comment.