hmpo-model 4.0.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/local-model.js +38 -42
- package/lib/remote-model.js +7 -1
- package/package.json +2 -3
- package/test/lib/spec.local-model.js +36 -18
- package/test/lib/spec.remote-model.js +7 -3
package/lib/local-model.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const _ = require('underscore');
|
|
4
3
|
const EventEmitter = require('events').EventEmitter;
|
|
5
4
|
|
|
6
5
|
class LocalModel extends EventEmitter {
|
|
@@ -9,8 +8,8 @@ class LocalModel extends EventEmitter {
|
|
|
9
8
|
super();
|
|
10
9
|
|
|
11
10
|
this.options = options || {};
|
|
12
|
-
this.attributes =
|
|
13
|
-
this.set(attributes, {silent: true});
|
|
11
|
+
this.attributes = Object.create(null);
|
|
12
|
+
if (attributes) this.set(attributes, {silent: true});
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
get(key) {
|
|
@@ -19,32 +18,35 @@ class LocalModel extends EventEmitter {
|
|
|
19
18
|
|
|
20
19
|
set(key, value, options) {
|
|
21
20
|
|
|
22
|
-
let attrs =
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
let attrs = Object.create(null);
|
|
22
|
+
if (key instanceof Map) {
|
|
23
|
+
Object.assign(attrs, Object.fromEntries(key));
|
|
24
|
+
options = value;
|
|
25
|
+
} else if (typeof key === 'string') {
|
|
25
26
|
attrs[key] = value;
|
|
26
27
|
} else {
|
|
27
|
-
attrs
|
|
28
|
+
Object.assign(attrs, key);
|
|
28
29
|
options = value;
|
|
29
30
|
}
|
|
30
|
-
options = options || {};
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
// silent set
|
|
33
|
+
if (options && options.silent) {
|
|
34
|
+
Object.assign(this.attributes, attrs);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
const changes = [];
|
|
39
|
+
for (let key in attrs) {
|
|
40
|
+
const value = attrs[key];
|
|
41
|
+
const old = this.attributes[key];
|
|
42
|
+
if (value !== old) changes.push([key, value, old]);
|
|
43
|
+
}
|
|
40
44
|
|
|
41
|
-
|
|
45
|
+
Object.assign(this.attributes, attrs);
|
|
42
46
|
|
|
43
|
-
if (
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
});
|
|
47
|
-
this.emit('change', changed);
|
|
47
|
+
if (changes.length) {
|
|
48
|
+
changes.forEach(([key, value, old]) => this.emit('change:' + key, value, old));
|
|
49
|
+
if (this.listenerCount('change')) this.emit('change', Object.fromEntries(changes));
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
return this;
|
|
@@ -52,38 +54,32 @@ class LocalModel extends EventEmitter {
|
|
|
52
54
|
|
|
53
55
|
|
|
54
56
|
unset(fields, options) {
|
|
55
|
-
options = options || {};
|
|
56
57
|
if (typeof fields === 'string') {
|
|
57
58
|
fields = [fields];
|
|
58
59
|
}
|
|
59
|
-
let old = this.toJSON(),
|
|
60
|
-
changed = {};
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
const changes = [];
|
|
62
|
+
for (let key of fields) {
|
|
63
|
+
const old = this.attributes[key];
|
|
64
|
+
if (old !== undefined) {
|
|
65
|
+
changes.push([key, undefined, old]);
|
|
65
66
|
delete this.attributes[key];
|
|
66
67
|
}
|
|
67
|
-
}
|
|
68
|
+
}
|
|
68
69
|
|
|
69
|
-
if (!options.silent &&
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
73
|
-
this.emit('change', changed);
|
|
70
|
+
if ((!options || !options.silent) && changes.length) {
|
|
71
|
+
changes.forEach(([key, value, old]) => this.emit('change:' + key, value, old));
|
|
72
|
+
if (this.listenerCount('change')) this.emit('change', Object.fromEntries(changes));
|
|
74
73
|
}
|
|
75
74
|
|
|
76
75
|
return this;
|
|
77
76
|
}
|
|
78
77
|
|
|
79
78
|
reset(options) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
_.each(keys, (key) => {
|
|
85
|
-
this.emit('change:' + key, undefined);
|
|
86
|
-
});
|
|
79
|
+
const old = this.attributes;
|
|
80
|
+
this.attributes = Object.create(null);
|
|
81
|
+
if (!options || !options.silent) {
|
|
82
|
+
Object.keys(old).forEach(key => this.emit('change:' + key, undefined, old[key]));
|
|
87
83
|
this.emit('reset');
|
|
88
84
|
}
|
|
89
85
|
}
|
|
@@ -97,8 +93,8 @@ class LocalModel extends EventEmitter {
|
|
|
97
93
|
this.set(property, val + amount);
|
|
98
94
|
}
|
|
99
95
|
|
|
100
|
-
toJSON() {
|
|
101
|
-
return Object.assign({}, this.attributes);
|
|
96
|
+
toJSON(bare = false) {
|
|
97
|
+
return Object.assign(bare ? Object.create(null) : {}, this.attributes);
|
|
102
98
|
}
|
|
103
99
|
}
|
|
104
100
|
|
package/lib/remote-model.js
CHANGED
|
@@ -223,7 +223,13 @@ class RemoteModel extends LocalModel {
|
|
|
223
223
|
|
|
224
224
|
parse(data) {
|
|
225
225
|
debug('parse', data);
|
|
226
|
-
if (data && typeof data === 'object')
|
|
226
|
+
if (data && typeof data === 'object') {
|
|
227
|
+
if (Array.isArray(data)) {
|
|
228
|
+
this.set('data', data);
|
|
229
|
+
} else {
|
|
230
|
+
this.set(data);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
227
233
|
return data;
|
|
228
234
|
}
|
|
229
235
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hmpo-model",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Simple model for interacting with http/rest apis.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,8 +28,7 @@
|
|
|
28
28
|
"debug": "^4.3.3",
|
|
29
29
|
"got": "^11.8.3",
|
|
30
30
|
"hpagent": "^0.1.2",
|
|
31
|
-
"lodash.kebabcase": "^4.1.1"
|
|
32
|
-
"underscore": "^1.13.1"
|
|
31
|
+
"lodash.kebabcase": "^4.1.1"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"chai": "^4.3.4",
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const Model = require('../../lib/local-model');
|
|
4
4
|
|
|
5
|
+
describe('Local Model', () => {
|
|
5
6
|
let model;
|
|
6
7
|
|
|
7
8
|
beforeEach(() => {
|
|
8
|
-
|
|
9
|
-
let Model = require('../../lib/local-model');
|
|
10
|
-
|
|
11
9
|
model = new Model();
|
|
12
10
|
});
|
|
13
11
|
|
|
@@ -18,7 +16,7 @@ describe('Local Model', () => {
|
|
|
18
16
|
});
|
|
19
17
|
|
|
20
18
|
it('has an attributes property of type object', () => {
|
|
21
|
-
model.attributes.
|
|
19
|
+
expect(model.attributes).to.be.an('object');
|
|
22
20
|
});
|
|
23
21
|
|
|
24
22
|
describe('constructor', () => {
|
|
@@ -54,20 +52,28 @@ describe('Local Model', () => {
|
|
|
54
52
|
describe('set', () => {
|
|
55
53
|
|
|
56
54
|
beforeEach(() => {
|
|
57
|
-
model
|
|
58
|
-
name: 'Test name'
|
|
59
|
-
};
|
|
55
|
+
model = new Model({ name: 'Test name' });
|
|
60
56
|
});
|
|
61
57
|
|
|
62
58
|
it('adds a key to the model attributes if the key is a string', () => {
|
|
63
|
-
model.set('age', 20)
|
|
59
|
+
model.set('age', 20);
|
|
60
|
+
expect(model.attributes).to.eql({
|
|
64
61
|
name: 'Test name',
|
|
65
62
|
age: 20
|
|
66
63
|
});
|
|
67
64
|
});
|
|
68
65
|
|
|
69
66
|
it('accepts an object as the key', () => {
|
|
70
|
-
model.set( { placeOfBirth: 'London' } )
|
|
67
|
+
model.set( { placeOfBirth: 'London' } );
|
|
68
|
+
expect(model.attributes).to.eql({
|
|
69
|
+
name: 'Test name',
|
|
70
|
+
placeOfBirth: 'London'
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('accepts a Map as the key', () => {
|
|
75
|
+
model.set( new Map([['placeOfBirth', 'London']]));
|
|
76
|
+
expect(model.attributes).to.eql({
|
|
71
77
|
name: 'Test name',
|
|
72
78
|
placeOfBirth: 'London'
|
|
73
79
|
});
|
|
@@ -141,17 +147,17 @@ describe('Local Model', () => {
|
|
|
141
147
|
|
|
142
148
|
it('removes properties from model when passed a string', () => {
|
|
143
149
|
model.unset('a');
|
|
144
|
-
model.toJSON().
|
|
150
|
+
expect(model.toJSON()).to.eql({ b: 2, c: 3 });
|
|
145
151
|
});
|
|
146
152
|
|
|
147
153
|
it('removes properties from model when passed an array', () => {
|
|
148
154
|
model.unset(['a', 'b']);
|
|
149
|
-
model.toJSON().
|
|
155
|
+
expect(model.toJSON()).to.eql({ c: 3 });
|
|
150
156
|
});
|
|
151
157
|
|
|
152
158
|
it('does nothing if passed a property that does not exist', () => {
|
|
153
159
|
model.unset('foo');
|
|
154
|
-
model.toJSON().
|
|
160
|
+
expect(model.toJSON()).to.eql({ a: 1, b: 2, c: 3 });
|
|
155
161
|
});
|
|
156
162
|
|
|
157
163
|
it('emits a change event', () => {
|
|
@@ -225,7 +231,7 @@ describe('Local Model', () => {
|
|
|
225
231
|
|
|
226
232
|
it('clears model attributes', () => {
|
|
227
233
|
model.reset();
|
|
228
|
-
model.toJSON().
|
|
234
|
+
expect(model.toJSON()).to.eql({});
|
|
229
235
|
expect(model.get('name')).to.be.undefined;
|
|
230
236
|
expect(model.get('age')).to.be.undefined;
|
|
231
237
|
});
|
|
@@ -244,9 +250,9 @@ describe('Local Model', () => {
|
|
|
244
250
|
model.on('change:age', listener2);
|
|
245
251
|
model.reset();
|
|
246
252
|
listener1.should.have.been.calledOnce;
|
|
247
|
-
listener1.should.have.been.calledWithExactly(undefined);
|
|
253
|
+
listener1.should.have.been.calledWithExactly(undefined, 'John');
|
|
248
254
|
listener2.should.have.been.calledOnce;
|
|
249
|
-
listener2.should.have.been.calledWithExactly(undefined);
|
|
255
|
+
listener2.should.have.been.calledWithExactly(undefined, 30);
|
|
250
256
|
});
|
|
251
257
|
|
|
252
258
|
it('emits no events if called with silent: true', () => {
|
|
@@ -266,10 +272,22 @@ describe('Local Model', () => {
|
|
|
266
272
|
};
|
|
267
273
|
});
|
|
268
274
|
|
|
269
|
-
it('returns
|
|
270
|
-
model.toJSON()
|
|
275
|
+
it('returns a bare object that\'s the same as the attributes property', () => {
|
|
276
|
+
const result = model.toJSON(true);
|
|
277
|
+
expect(result).to.eql({
|
|
271
278
|
name: 'Test name'
|
|
272
279
|
});
|
|
280
|
+
|
|
281
|
+
expect(result.constructor).to.be.undefined;
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('returns an object that\'s the same as the attributes property with object prototype', () => {
|
|
285
|
+
const result = model.toJSON();
|
|
286
|
+
expect(result).to.eql({
|
|
287
|
+
name: 'Test name'
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
expect(result.constructor).to.be.a('function');
|
|
273
291
|
});
|
|
274
292
|
});
|
|
275
293
|
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const Model = require('../../lib/remote-model');
|
|
4
4
|
const BaseModel = require('../../lib/local-model');
|
|
5
|
-
const _ = require('underscore');
|
|
6
5
|
const logger = require('hmpo-logger');
|
|
7
6
|
|
|
8
7
|
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');
|
|
@@ -543,7 +542,7 @@ describe('Remote Model', () => {
|
|
|
543
542
|
}
|
|
544
543
|
};
|
|
545
544
|
|
|
546
|
-
let savedConfig =
|
|
545
|
+
let savedConfig = Object.assign({}, config);
|
|
547
546
|
|
|
548
547
|
let returnedConfig = model.requestConfig(config);
|
|
549
548
|
|
|
@@ -573,7 +572,7 @@ describe('Remote Model', () => {
|
|
|
573
572
|
method: 'VERB'
|
|
574
573
|
};
|
|
575
574
|
|
|
576
|
-
requestSettings =
|
|
575
|
+
requestSettings = Object.assign({}, settings);
|
|
577
576
|
});
|
|
578
577
|
|
|
579
578
|
afterEach(() => {
|
|
@@ -910,6 +909,11 @@ describe('Remote Model', () => {
|
|
|
910
909
|
model.get('foo').should.equal('bar');
|
|
911
910
|
});
|
|
912
911
|
|
|
912
|
+
it('sets the parsed array data to the model as "data"', () => {
|
|
913
|
+
model.parse([1, 2, 3, 4]);
|
|
914
|
+
model.get('data').should.eql([1, 2, 3, 4]);
|
|
915
|
+
});
|
|
916
|
+
|
|
913
917
|
it('does not set if the data falsey', () => {
|
|
914
918
|
model.set = sinon.stub();
|
|
915
919
|
model.parse(null);
|