-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
223 lines (207 loc) · 5.89 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
var Structure = require('./src/structure');
/**
* Creates a new instance of Immstruct, having it's own list
* of Structure instances.
*
* ### Examples:
* ```js
* var ImmstructInstance = require('immstruct').Immstruct;
* var immstruct = new ImmstructInstance();
* var structure = immstruct.get({ data: });
* ```
*
* @property {Array.<Structure>} instances Array of `Structure` instances.
*
* @class {Immstruct}
* @constructor
* @returns {Immstruct}
* @api public
*/
function Immstruct () {
if (!(this instanceof Immstruct)) {
return new Immstruct();
}
this._instances = {};
}
/**
*
* Gets or creates a new instance of {Structure}. Provide optional
* key to be able to retrieve it from list of instances. If no key
* is provided, a random key will be generated.
*
* ### Examples:
* ```js
* var immstruct = require('immstruct');
* var structure = immstruct.get('myStruct', { foo: 'Hello' });
* ```
* @param {string} [key] - defaults to random string
* @param {Object|Immutable} [data] - defaults to empty data
*
* @returns {Structure}
* @module immstruct.get
* @api public
*/
Immstruct.prototype.get = function (key, data) {
return getInstance(this, {
key: key,
data: data
});
};
/**
*
* Get list of all instances created.
*
* @param {string} [name] - Name of the instance to get. If undefined get all instances
*
* @returns {(Structure|Object.<String, Structure>)}
* @module immstruct.instance
* @api public
*/
Immstruct.prototype.instance = function (name) {
if (name) return this._instances[name];
return this._instances;
};
/**
* Clear the entire list of `Structure` instances from the Immstruct
* instance. You would do this to start from scratch, freeing up memory.
*
* ### Examples:
* ```js
* var immstruct = require('immstruct');
* immstruct.clear();
* ```
* @module immstruct.clear
* @api public
*/
Immstruct.prototype.clear = function () {
this._instances = {};
};
/**
* Remove one `Structure` instance from the Immstruct instances list.
* Provided by key
*
* ### Examples:
* ```js
* var immstruct = require('immstruct');
* immstruct('myKey', { foo: 'hello' });
* immstruct.remove('myKey');
* ```
* @param {string} key
*
* @module immstruct.remove
* @api public
* @returns {boolean}
*/
Immstruct.prototype.remove = function (key) {
return delete this._instances[key];
};
/**
* Gets or creates a new instance of `Structure` with history (undo/redo)
* activated per default. Same usage and signature as regular `Immstruct.get`.
* Provide optional key to be able to retrieve it from list of instances.
* If no key is provided, a random key will be generated.
*
* Provide optional limit to cap the last number of history references
* that will be kept. Once limit is reached, a new history record
* shifts off the oldest record. The default if omitted is Infinity.
* Setting to 0 is the as not having history enabled in the first place.
*
* ### Examples:
* ```js
* var immstruct = require('immstruct');
* var structure = immstruct.withHistory('myStruct', 10, { foo: 'Hello' });
* var structure = immstruct.withHistory(10, { foo: 'Hello' });
* var structure = immstruct.withHistory('myStruct', { foo: 'Hello' });
* var structure = immstruct.withHistory({ foo: 'Hello' });
* ```
*
* @param {string} [key] - defaults to random string
* @param {number} [limit] - defaults to Infinity
* @param {Object|Immutable} [data] - defaults to empty data
*
* @module immstruct.withHistory
* @api public
* @returns {Structure}
*/
Immstruct.prototype.withHistory = function (key, limit, data) {
return getInstance(this, {
key: key,
data: data,
history: true,
historyLimit: limit
});
};
var inst = new Immstruct();
/**
* This is a default instance of `Immstruct` as well as a shortcut for
* creating `Structure` instances (See `Immstruct.get` and `Immstruct`).
* This is what is returned from `require('immstruct')`.
*
* From `Immstruct.get`:
* Gets or creates a new instance of {Structure} in the default Immstruct
* instance. A link to `immstruct.get()`. Provide optional
* key to be able to retrieve it from list of instances. If no key
* is provided, a random key will be generated.
*
* ### Examples:
* ```js
* var immstruct = require('immstruct');
* var structure = immstruct('myStruct', { foo: 'Hello' });
* var structure2 = immstruct.withHistory({ bar: 'Bye' });
* immstruct.remove('myStruct');
* // ...
* ```
*
* @param {string} [key] - defaults to random string
* @param {Object|Immutable} [data] - defaults to empty data
*
* @api public
* @see {@link Immstruct}
* @see {Immstruct.prototype.get}
* @module immstruct
* @class {Immstruct}
* @returns {Structure|Function}
*/
module.exports = function (key, data) {
return getInstance(inst, {
key: key,
data: data
});
};
module.exports.withHistory = function (key, limit, data) {
return getInstance(inst, {
key: key,
data: data,
history: true,
historyLimit: limit
});
};
module.exports.Structure = Structure;
module.exports.Immstruct = Immstruct;
module.exports.clear = inst.clear.bind(inst);
module.exports.remove = inst.remove.bind(inst);
module.exports.get = inst.get.bind(inst);
module.exports.instance = function (name) {
if (name) return inst._instances[name];
return inst._instances;
};
function getInstance (obj, options) {
if (typeof options.key === 'object') {
options.data = options.key;
options.key = void 0;
} else if (typeof options.key === 'number') {
options.data = options.historyLimit;
options.historyLimit = options.key;
options.key = void 0;
} else if (typeof options.historyLimit === 'object') {
options.data = options.historyLimit;
options.historyLimit = void 0;
}
if (options.key && obj._instances[options.key]) {
return obj._instances[options.key];
}
var newInstance = new Structure(options);
obj._instances[newInstance.key] = newInstance;
return newInstance;
}