-
Notifications
You must be signed in to change notification settings - Fork 330
/
collection.js
91 lines (81 loc) · 2.13 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {isString} from 'lodash';
import {MEETINGS} from '../constants';
/**
* @class Collection
*/
export default class Collection {
namespace = MEETINGS;
/**
* @param {String} property
* @memberof Collection
*/
constructor(property) {
if (!property || !isString(property)) {
throw new Error('Collection expects a string to use as a property name.');
}
/**
* The property name for what collection will be stored, i.e., this.meetingInfos, this.meetings, etc.
* @instance
* @type {String}
* @public
* @memberof Meetings
*/
this.propertyName = property;
/**
* The actual object for the collection
* @instance
* @type {Object}
* @public
* @memberof Meetings
*/
this[this.propertyName] = {};
}
/**
* @param {String} id ID of the thing stuffed into the collection at id location
* @returns {Any} returns whatever is being stuffed into the collection
* @public
* @memberof Collection
*/
get(id) {
return this[this.propertyName] && this[this.propertyName][id] ? this[this.propertyName][id] : null;
}
/**
* @param {String} id the id of the meeting info instance to add to the collection
* @param {Any} value the thing to set in the collection
* @returns {Any} returns the thing just put in the collection
* @public
* @memberof Collection
*/
set(id, value) {
this[this.propertyName][id] = value;
return this.get(id);
}
/**
* remove the thing at the id
* @param {String} id ID of the thing you wish to delete from the collection
* @returns {undefined}
* @public
* @memberof Collection
*/
delete(id) {
delete this[this.propertyName][id];
}
/**
* @returns {Object} returns an object map of things stuffed into the collection
* @public
* @memberof Collection
*/
getAll() {
return this[this.propertyName];
}
/**
* @param {Object} set the replacement object
* @returns {Object} returns an object map of things stuffed into the collection
* @public
* @memberof Collection
*/
setAll(set) {
this[this.propertyName] = set;
return this.getAll();
}
}