yayson 2.1.0 → 3.0.0
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/.eslintrc.json +28 -0
- package/.github/workflows/node.js.yml +1 -1
- package/.mocharc.js +4 -4
- package/.nvmrc +1 -0
- package/README.md +77 -78
- package/babel.config.json +4 -0
- package/legacy.js +2 -0
- package/package.json +26 -29
- package/prettier.config.js +5 -0
- package/src/legacy.js +14 -0
- package/src/yayson/adapter.js +18 -0
- package/src/yayson/adapters/index.js +1 -0
- package/src/yayson/adapters/sequelize.js +11 -0
- package/src/yayson/legacy-presenter.js +121 -0
- package/src/yayson/legacy-store.js +156 -0
- package/src/yayson/presenter.js +198 -0
- package/src/yayson/store.js +194 -0
- package/src/yayson.js +23 -0
- package/webpack.browser.js +27 -0
- package/webpack.common.js +39 -0
- package/webpack.dist.js +21 -0
- package/.circleci/config.yml +0 -35
- package/bower.json +0 -31
- package/coffeelint.json +0 -120
- package/gulpfile.coffee +0 -43
- package/lib/yayson/adapter.js +0 -22
- package/lib/yayson/adapters/index.js +0 -3
- package/lib/yayson/adapters/sequelize.js +0 -14
- package/lib/yayson/presenter.js +0 -209
- package/lib/yayson/store.js +0 -169
- package/lib/yayson/utils.js +0 -47
- package/lib/yayson.js +0 -53
package/bower.json
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "yayson",
|
|
3
|
-
"description": "A library for serializing and reading JSON API standardized data in JavaScript.",
|
|
4
|
-
"main": "dist/yayson.js",
|
|
5
|
-
"authors": [
|
|
6
|
-
"Johannes Edelstam <johannes@edelst.am>",
|
|
7
|
-
"Jonny Strömberg <jonny.stromberg@gmail.com>"
|
|
8
|
-
],
|
|
9
|
-
"license": "MIT",
|
|
10
|
-
"keywords": [
|
|
11
|
-
"json-api",
|
|
12
|
-
"json",
|
|
13
|
-
"api",
|
|
14
|
-
"response",
|
|
15
|
-
"format",
|
|
16
|
-
"serialization",
|
|
17
|
-
"deserialization",
|
|
18
|
-
"yay",
|
|
19
|
-
"wow",
|
|
20
|
-
"omg"
|
|
21
|
-
],
|
|
22
|
-
"homepage": "https://github.com/confetti/yayson",
|
|
23
|
-
"moduleType": [],
|
|
24
|
-
"ignore": [
|
|
25
|
-
"**/.*",
|
|
26
|
-
"node_modules",
|
|
27
|
-
"bower_components",
|
|
28
|
-
"test",
|
|
29
|
-
"tests"
|
|
30
|
-
]
|
|
31
|
-
}
|
package/coffeelint.json
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"arrow_spacing": {
|
|
3
|
-
"level": "ignore"
|
|
4
|
-
},
|
|
5
|
-
"braces_spacing": {
|
|
6
|
-
"level": "ignore",
|
|
7
|
-
"spaces": 0
|
|
8
|
-
},
|
|
9
|
-
"camel_case_classes": {
|
|
10
|
-
"level": "error"
|
|
11
|
-
},
|
|
12
|
-
"coffeescript_error": {
|
|
13
|
-
"level": "error"
|
|
14
|
-
},
|
|
15
|
-
"colon_assignment_spacing": {
|
|
16
|
-
"level": "ignore",
|
|
17
|
-
"spacing": {
|
|
18
|
-
"left": 0,
|
|
19
|
-
"right": 0
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
"cyclomatic_complexity": {
|
|
23
|
-
"value": 10,
|
|
24
|
-
"level": "ignore"
|
|
25
|
-
},
|
|
26
|
-
"duplicate_key": {
|
|
27
|
-
"level": "error"
|
|
28
|
-
},
|
|
29
|
-
"empty_constructor_needs_parens": {
|
|
30
|
-
"level": "ignore"
|
|
31
|
-
},
|
|
32
|
-
"ensure_comprehensions": {
|
|
33
|
-
"level": "warn"
|
|
34
|
-
},
|
|
35
|
-
"indentation": {
|
|
36
|
-
"value": 2,
|
|
37
|
-
"level": "error"
|
|
38
|
-
},
|
|
39
|
-
"line_endings": {
|
|
40
|
-
"level": "ignore",
|
|
41
|
-
"value": "unix"
|
|
42
|
-
},
|
|
43
|
-
"max_line_length": {
|
|
44
|
-
"value": 180,
|
|
45
|
-
"level": "error",
|
|
46
|
-
"limitComments": true
|
|
47
|
-
},
|
|
48
|
-
"missing_fat_arrows": {
|
|
49
|
-
"level": "ignore"
|
|
50
|
-
},
|
|
51
|
-
"newlines_after_classes": {
|
|
52
|
-
"value": 3,
|
|
53
|
-
"level": "ignore"
|
|
54
|
-
},
|
|
55
|
-
"no_backticks": {
|
|
56
|
-
"level": "error"
|
|
57
|
-
},
|
|
58
|
-
"no_debugger": {
|
|
59
|
-
"level": "warn"
|
|
60
|
-
},
|
|
61
|
-
"no_empty_functions": {
|
|
62
|
-
"level": "ignore"
|
|
63
|
-
},
|
|
64
|
-
"no_empty_param_list": {
|
|
65
|
-
"level": "ignore"
|
|
66
|
-
},
|
|
67
|
-
"no_implicit_braces": {
|
|
68
|
-
"level": "ignore",
|
|
69
|
-
"strict": true
|
|
70
|
-
},
|
|
71
|
-
"no_implicit_parens": {
|
|
72
|
-
"strict": true,
|
|
73
|
-
"level": "ignore"
|
|
74
|
-
},
|
|
75
|
-
"no_interpolation_in_single_quotes": {
|
|
76
|
-
"level": "ignore"
|
|
77
|
-
},
|
|
78
|
-
"no_plusplus": {
|
|
79
|
-
"level": "ignore"
|
|
80
|
-
},
|
|
81
|
-
"no_stand_alone_at": {
|
|
82
|
-
"level": "ignore"
|
|
83
|
-
},
|
|
84
|
-
"no_tabs": {
|
|
85
|
-
"level": "error"
|
|
86
|
-
},
|
|
87
|
-
"no_throwing_strings": {
|
|
88
|
-
"level": "error"
|
|
89
|
-
},
|
|
90
|
-
"no_trailing_semicolons": {
|
|
91
|
-
"level": "error"
|
|
92
|
-
},
|
|
93
|
-
"no_trailing_whitespace": {
|
|
94
|
-
"level": "error",
|
|
95
|
-
"allowed_in_comments": false,
|
|
96
|
-
"allowed_in_empty_lines": true
|
|
97
|
-
},
|
|
98
|
-
"no_unnecessary_double_quotes": {
|
|
99
|
-
"level": "ignore"
|
|
100
|
-
},
|
|
101
|
-
"no_unnecessary_fat_arrows": {
|
|
102
|
-
"level": "warn"
|
|
103
|
-
},
|
|
104
|
-
"non_empty_constructor_needs_parens": {
|
|
105
|
-
"level": "ignore"
|
|
106
|
-
},
|
|
107
|
-
"prefer_english_operator": {
|
|
108
|
-
"level": "ignore",
|
|
109
|
-
"doubleNotLevel": "ignore"
|
|
110
|
-
},
|
|
111
|
-
"space_operators": {
|
|
112
|
-
"level": "ignore"
|
|
113
|
-
},
|
|
114
|
-
"spacing_after_comma": {
|
|
115
|
-
"level": "ignore"
|
|
116
|
-
},
|
|
117
|
-
"transform_messes_up_line_numbers": {
|
|
118
|
-
"level": "warn"
|
|
119
|
-
}
|
|
120
|
-
}
|
package/gulpfile.coffee
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
gulp = require 'gulp'
|
|
2
|
-
source = require 'vinyl-source-stream'
|
|
3
|
-
coffeeify = require 'coffeeify'
|
|
4
|
-
browserify = require 'browserify'
|
|
5
|
-
gutil = require 'gulp-util'
|
|
6
|
-
coffee = require 'gulp-coffee'
|
|
7
|
-
http = require 'http'
|
|
8
|
-
ecstatic = require 'ecstatic'
|
|
9
|
-
|
|
10
|
-
serverport = 5005
|
|
11
|
-
|
|
12
|
-
gulp.task 'browserify', ->
|
|
13
|
-
browserify('./src/yayson.coffee', {extensions: ['.coffee'], ignoreMissing: true, standalone: 'yayson'})
|
|
14
|
-
.transform(coffeeify)
|
|
15
|
-
.bundle()
|
|
16
|
-
.pipe(source('yayson.js'))
|
|
17
|
-
.pipe(gulp.dest('./dist/'))
|
|
18
|
-
|
|
19
|
-
gulp.task 'build', ->
|
|
20
|
-
gulp.src('./src/**/*.coffee')
|
|
21
|
-
.pipe(coffee({bare: true}).on('error', gutil.log))
|
|
22
|
-
.pipe(gulp.dest('./lib/'))
|
|
23
|
-
|
|
24
|
-
gulp.task 'default', gulp.series('browserify', 'build')
|
|
25
|
-
|
|
26
|
-
gulp.task 'test-serve', ->
|
|
27
|
-
http.createServer(ecstatic root: __dirname + '/' ).listen(serverport)
|
|
28
|
-
|
|
29
|
-
console.log "Listening on #{serverport}"
|
|
30
|
-
|
|
31
|
-
gulp.task 'test-coffee', ->
|
|
32
|
-
browserify('./test/browser.coffee', {extensions: ['.coffee']})
|
|
33
|
-
.transform(coffeeify)
|
|
34
|
-
.bundle()
|
|
35
|
-
.pipe(source('browser.js'))
|
|
36
|
-
.pipe(gulp.dest('./test/.tmp/'))
|
|
37
|
-
|
|
38
|
-
gulp.task 'test-watch', ->
|
|
39
|
-
gulp.watch('./src/**/*.coffee', {ignoreInitial: false}, gulp.series('test-coffee'))
|
|
40
|
-
gulp.watch('./test/**/*.coffee', gulp.series('test-coffee'))
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
gulp.task 'test-browser', gulp.parallel('test-watch', 'test-serve')
|
package/lib/yayson/adapter.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
var Adapter;
|
|
2
|
-
|
|
3
|
-
Adapter = class Adapter {
|
|
4
|
-
static get(model, key) {
|
|
5
|
-
if (key) {
|
|
6
|
-
return model[key];
|
|
7
|
-
}
|
|
8
|
-
return model;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
static id(model) {
|
|
12
|
-
var id;
|
|
13
|
-
id = this.get(model, 'id');
|
|
14
|
-
if (id === void 0) {
|
|
15
|
-
return id;
|
|
16
|
-
}
|
|
17
|
-
return `${id}`;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
module.exports = Adapter;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
var Adapter, SequelizeAdapter;
|
|
2
|
-
|
|
3
|
-
Adapter = require('../adapter');
|
|
4
|
-
|
|
5
|
-
SequelizeAdapter = class SequelizeAdapter extends Adapter {
|
|
6
|
-
static get(model, key) {
|
|
7
|
-
if (model != null) {
|
|
8
|
-
return model.get(key);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
module.exports = SequelizeAdapter;
|
package/lib/yayson/presenter.js
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
module.exports = function(utils, adapter) {
|
|
2
|
-
var Presenter;
|
|
3
|
-
Presenter = (function() {
|
|
4
|
-
var buildLinks;
|
|
5
|
-
|
|
6
|
-
class Presenter {
|
|
7
|
-
constructor(scope = {}) {
|
|
8
|
-
this.scope = scope;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
id(instance) {
|
|
12
|
-
return this.constructor.adapter.id(instance);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
selfLinks(instance) {}
|
|
16
|
-
|
|
17
|
-
links() {}
|
|
18
|
-
|
|
19
|
-
relationships() {}
|
|
20
|
-
|
|
21
|
-
attributes(instance) {
|
|
22
|
-
var attributes, key, relationships;
|
|
23
|
-
if (instance == null) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
attributes = utils.clone(this.constructor.adapter.get(instance));
|
|
27
|
-
delete attributes['id'];
|
|
28
|
-
relationships = this.relationships();
|
|
29
|
-
for (key in relationships) {
|
|
30
|
-
delete attributes[key];
|
|
31
|
-
}
|
|
32
|
-
return attributes;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
includeRelationships(scope, instance) {
|
|
36
|
-
var data, factory, key, presenter, relationships, results;
|
|
37
|
-
relationships = this.relationships();
|
|
38
|
-
results = [];
|
|
39
|
-
for (key in relationships) {
|
|
40
|
-
factory = relationships[key] || (function() {
|
|
41
|
-
throw new Error(`Presenter for ${key} in ${this.type} is not defined`);
|
|
42
|
-
}).call(this);
|
|
43
|
-
presenter = new factory(scope);
|
|
44
|
-
data = this.constructor.adapter.get(instance, key);
|
|
45
|
-
if (data != null) {
|
|
46
|
-
results.push(presenter.toJSON(data, {
|
|
47
|
-
include: true
|
|
48
|
-
}));
|
|
49
|
-
} else {
|
|
50
|
-
results.push(void 0);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return results;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
buildRelationships(instance) {
|
|
57
|
-
var build, buildData, data, key, links, presenter, relationships, rels;
|
|
58
|
-
if (instance == null) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
rels = this.relationships();
|
|
62
|
-
links = this.links(instance) || {};
|
|
63
|
-
relationships = null;
|
|
64
|
-
for (key in rels) {
|
|
65
|
-
data = this.constructor.adapter.get(instance, key);
|
|
66
|
-
presenter = rels[key];
|
|
67
|
-
buildData = (d) => {
|
|
68
|
-
return data = {
|
|
69
|
-
id: this.constructor.adapter.id(d),
|
|
70
|
-
type: presenter.prototype.type
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
build = (d) => {
|
|
74
|
-
var rel;
|
|
75
|
-
rel = {};
|
|
76
|
-
if (d != null) {
|
|
77
|
-
rel.data = buildData(d);
|
|
78
|
-
}
|
|
79
|
-
if (links[key] != null) {
|
|
80
|
-
rel.links = buildLinks(links[key]);
|
|
81
|
-
} else if (d == null) {
|
|
82
|
-
rel.data = null;
|
|
83
|
-
}
|
|
84
|
-
return rel;
|
|
85
|
-
};
|
|
86
|
-
relationships || (relationships = {});
|
|
87
|
-
relationships[key] || (relationships[key] = {});
|
|
88
|
-
if (data instanceof Array) {
|
|
89
|
-
relationships[key].data = data.map(buildData);
|
|
90
|
-
if (links[key] != null) {
|
|
91
|
-
relationships[key].links = buildLinks(links[key]);
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
relationships[key] = build(data);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return relationships;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
buildSelfLink(instance) {
|
|
101
|
-
return buildLinks(this.selfLinks(instance));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
toJSON(instanceOrCollection, options = {}) {
|
|
105
|
-
var added, base, base1, base2, collection, instance, links, model, relationships;
|
|
106
|
-
if (options.meta != null) {
|
|
107
|
-
this.scope.meta = options.meta;
|
|
108
|
-
}
|
|
109
|
-
if (options.links != null) {
|
|
110
|
-
this.scope.links = options.links;
|
|
111
|
-
}
|
|
112
|
-
(base = this.scope).data || (base.data = null);
|
|
113
|
-
if (instanceOrCollection == null) {
|
|
114
|
-
return this.scope;
|
|
115
|
-
}
|
|
116
|
-
if (instanceOrCollection instanceof Array) {
|
|
117
|
-
collection = instanceOrCollection;
|
|
118
|
-
(base1 = this.scope).data || (base1.data = []);
|
|
119
|
-
collection.forEach((instance) => {
|
|
120
|
-
return this.toJSON(instance, options);
|
|
121
|
-
});
|
|
122
|
-
} else {
|
|
123
|
-
instance = instanceOrCollection;
|
|
124
|
-
added = true;
|
|
125
|
-
model = {
|
|
126
|
-
id: this.id(instance),
|
|
127
|
-
type: this.type,
|
|
128
|
-
attributes: this.attributes(instance)
|
|
129
|
-
};
|
|
130
|
-
if (model.id === void 0) {
|
|
131
|
-
delete model.id;
|
|
132
|
-
}
|
|
133
|
-
relationships = this.buildRelationships(instance);
|
|
134
|
-
if (relationships != null) {
|
|
135
|
-
model.relationships = relationships;
|
|
136
|
-
}
|
|
137
|
-
links = this.buildSelfLink(instance);
|
|
138
|
-
if (links != null) {
|
|
139
|
-
model.links = links;
|
|
140
|
-
}
|
|
141
|
-
if (options.include) {
|
|
142
|
-
(base2 = this.scope).included || (base2.included = []);
|
|
143
|
-
if (!utils.any(this.scope.included.concat(this.scope.data), function(i) {
|
|
144
|
-
return i.id === model.id && i.type === model.type;
|
|
145
|
-
})) {
|
|
146
|
-
this.scope.included.push(model);
|
|
147
|
-
} else {
|
|
148
|
-
added = false;
|
|
149
|
-
}
|
|
150
|
-
} else if (this.scope.data != null) {
|
|
151
|
-
if (!(this.scope.data instanceof Array && utils.any(this.scope.data, function(i) {
|
|
152
|
-
return i.id === model.id;
|
|
153
|
-
}))) {
|
|
154
|
-
this.scope.data.push(model);
|
|
155
|
-
} else {
|
|
156
|
-
added = false;
|
|
157
|
-
}
|
|
158
|
-
} else {
|
|
159
|
-
this.scope.data = model;
|
|
160
|
-
}
|
|
161
|
-
if (added) {
|
|
162
|
-
this.includeRelationships(this.scope, instance);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return this.scope;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
render(instanceOrCollection, options) {
|
|
169
|
-
if (utils.isPromise(instanceOrCollection)) {
|
|
170
|
-
return instanceOrCollection.then((data) => {
|
|
171
|
-
return this.toJSON(data, options);
|
|
172
|
-
});
|
|
173
|
-
} else {
|
|
174
|
-
return this.toJSON(instanceOrCollection, options);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
static toJSON() {
|
|
179
|
-
return (new this).toJSON(...arguments);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
static render() {
|
|
183
|
-
return (new this).render(...arguments);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
buildLinks = function(link) {
|
|
189
|
-
if (link == null) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
if ((link.self != null) || (link.related != null)) {
|
|
193
|
-
return link;
|
|
194
|
-
} else {
|
|
195
|
-
return {
|
|
196
|
-
self: link
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
Presenter.adapter = adapter;
|
|
202
|
-
|
|
203
|
-
Presenter.prototype.type = 'objects';
|
|
204
|
-
|
|
205
|
-
return Presenter;
|
|
206
|
-
|
|
207
|
-
}).call(this);
|
|
208
|
-
return module.exports = Presenter;
|
|
209
|
-
};
|
package/lib/yayson/store.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
module.exports = function(utils) {
|
|
2
|
-
var Record, Store;
|
|
3
|
-
Record = class Record {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
({id: this.id, type: this.type, attributes: this.attributes, relationships: this.relationships, links: this.links, meta: this.meta} = options);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
return Store = class Store {
|
|
10
|
-
constructor(options) {
|
|
11
|
-
this.reset();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
reset() {
|
|
15
|
-
this.records = [];
|
|
16
|
-
return this.relations = {};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
toModel(rec, type, models) {
|
|
20
|
-
var base, currentModel, data, key, links, meta, model, name, ref, rel, resolve, typeAttribute;
|
|
21
|
-
model = utils.clone(rec.attributes) || {};
|
|
22
|
-
if (model.type) {
|
|
23
|
-
typeAttribute = model.type;
|
|
24
|
-
}
|
|
25
|
-
model.id = rec.id;
|
|
26
|
-
model.type = rec.type;
|
|
27
|
-
models[type] || (models[type] = {});
|
|
28
|
-
(base = models[type])[name = rec.id] || (base[name] = model);
|
|
29
|
-
if (model.hasOwnProperty('meta')) {
|
|
30
|
-
model.attributes = {
|
|
31
|
-
meta: model.meta
|
|
32
|
-
};
|
|
33
|
-
delete model.meta;
|
|
34
|
-
}
|
|
35
|
-
if (rec.meta != null) {
|
|
36
|
-
model.meta = rec.meta;
|
|
37
|
-
}
|
|
38
|
-
if (rec.links != null) {
|
|
39
|
-
model.links = rec.links;
|
|
40
|
-
}
|
|
41
|
-
if (rec.relationships != null) {
|
|
42
|
-
ref = rec.relationships;
|
|
43
|
-
for (key in ref) {
|
|
44
|
-
rel = ref[key];
|
|
45
|
-
data = rel.data;
|
|
46
|
-
links = rel.links;
|
|
47
|
-
meta = rel.meta;
|
|
48
|
-
model[key] = null;
|
|
49
|
-
if (!((data != null) || (links != null))) {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
resolve = ({type, id}) => {
|
|
53
|
-
return this.find(type, id, models);
|
|
54
|
-
};
|
|
55
|
-
model[key] = data instanceof Array ? data.map(resolve) : data != null ? resolve(data) : {};
|
|
56
|
-
// Model of the relation
|
|
57
|
-
currentModel = model[key];
|
|
58
|
-
if (currentModel != null) {
|
|
59
|
-
// retain the links and meta from the relationship entry
|
|
60
|
-
// use as underscore property name because the currentModel may also have a link and meta reference
|
|
61
|
-
currentModel._links = links || {};
|
|
62
|
-
currentModel._meta = meta || {};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (typeAttribute) {
|
|
67
|
-
model.type = typeAttribute;
|
|
68
|
-
}
|
|
69
|
-
return model;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
findRecord(type, id) {
|
|
73
|
-
return utils.find(this.records, function(r) {
|
|
74
|
-
return r.type === type && r.id === id;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
findRecords(type) {
|
|
79
|
-
return utils.filter(this.records, function(r) {
|
|
80
|
-
return r.type === type;
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
find(type, id, models = {}) {
|
|
85
|
-
var rec;
|
|
86
|
-
rec = this.findRecord(type, id);
|
|
87
|
-
if (rec == null) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
models[type] || (models[type] = {});
|
|
91
|
-
return models[type][id] || this.toModel(rec, type, models);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
findAll(type, models = {}) {
|
|
95
|
-
var recs;
|
|
96
|
-
recs = this.findRecords(type);
|
|
97
|
-
if (recs == null) {
|
|
98
|
-
return [];
|
|
99
|
-
}
|
|
100
|
-
recs.forEach((rec) => {
|
|
101
|
-
models[type] || (models[type] = {});
|
|
102
|
-
return this.toModel(rec, type, models);
|
|
103
|
-
});
|
|
104
|
-
return utils.values(models[type]);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
remove(type, id) {
|
|
108
|
-
var records, remove;
|
|
109
|
-
remove = (record) => {
|
|
110
|
-
var index;
|
|
111
|
-
index = this.records.indexOf(record);
|
|
112
|
-
if (!(index < 0)) {
|
|
113
|
-
return this.records.splice(index, 1);
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
if (id != null) {
|
|
117
|
-
return remove(this.findRecord(type, id));
|
|
118
|
-
} else {
|
|
119
|
-
records = this.findRecords(type);
|
|
120
|
-
return records.map(remove);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
sync(body) {
|
|
125
|
-
var models, recs, result, sync;
|
|
126
|
-
sync = (data) => {
|
|
127
|
-
var add;
|
|
128
|
-
if (data == null) {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
add = (obj) => {
|
|
132
|
-
var id, rec, type;
|
|
133
|
-
({type, id} = obj);
|
|
134
|
-
this.remove(type, id);
|
|
135
|
-
rec = new Record(obj);
|
|
136
|
-
this.records.push(rec);
|
|
137
|
-
return rec;
|
|
138
|
-
};
|
|
139
|
-
if (data instanceof Array) {
|
|
140
|
-
return data.map(add);
|
|
141
|
-
} else {
|
|
142
|
-
return add(data);
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
sync(body.included);
|
|
146
|
-
recs = sync(body.data);
|
|
147
|
-
if (recs == null) {
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
models = {};
|
|
151
|
-
result = null;
|
|
152
|
-
if (recs instanceof Array) {
|
|
153
|
-
result = recs.map((rec) => {
|
|
154
|
-
return this.toModel(rec, rec.type, models);
|
|
155
|
-
});
|
|
156
|
-
} else {
|
|
157
|
-
result = this.toModel(recs, recs.type, models);
|
|
158
|
-
}
|
|
159
|
-
if (body.hasOwnProperty('links')) {
|
|
160
|
-
result.links = body.links;
|
|
161
|
-
}
|
|
162
|
-
if (body.hasOwnProperty('meta')) {
|
|
163
|
-
result.meta = body.meta;
|
|
164
|
-
}
|
|
165
|
-
return result;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
};
|
|
169
|
-
};
|
package/lib/yayson/utils.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
module.exports = function(_ = {}, Q = {}) {
|
|
2
|
-
var utils;
|
|
3
|
-
return utils = {
|
|
4
|
-
find: _.find || function(arr, callback) {
|
|
5
|
-
var elem, i, len;
|
|
6
|
-
for (i = 0, len = arr.length; i < len; i++) {
|
|
7
|
-
elem = arr[i];
|
|
8
|
-
if (callback(elem)) {
|
|
9
|
-
return elem;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return void 0;
|
|
13
|
-
},
|
|
14
|
-
filter: _.filter || function(arr, callback) {
|
|
15
|
-
var elem, i, len, res;
|
|
16
|
-
res = [];
|
|
17
|
-
for (i = 0, len = arr.length; i < len; i++) {
|
|
18
|
-
elem = arr[i];
|
|
19
|
-
if (callback(elem)) {
|
|
20
|
-
res.push(elem);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return res;
|
|
24
|
-
},
|
|
25
|
-
values: _.values || function(obj = {}) {
|
|
26
|
-
return Object.keys(obj).map(function(key) {
|
|
27
|
-
return obj[key];
|
|
28
|
-
});
|
|
29
|
-
},
|
|
30
|
-
clone: _.clone || function(obj = {}) {
|
|
31
|
-
var clone, key, val;
|
|
32
|
-
clone = {};
|
|
33
|
-
for (key in obj) {
|
|
34
|
-
val = obj[key];
|
|
35
|
-
clone[key] = val;
|
|
36
|
-
}
|
|
37
|
-
return clone;
|
|
38
|
-
},
|
|
39
|
-
any: _.any || function(arr, callback) {
|
|
40
|
-
return utils.find(arr, callback) != null;
|
|
41
|
-
},
|
|
42
|
-
// stolen from https://github.com/kriskowal/q
|
|
43
|
-
isPromise: Q.isPromise || function(obj) {
|
|
44
|
-
return obj === Object(obj) && typeof obj.promiseDispatch === "function" && typeof obj.inspect === "function";
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
};
|