-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
396 lines (318 loc) · 11.4 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// The export pattern is a UMD template:
// https://github.com/umdjs/umd/blob/1deb860078252f31ced62fa8e7694f8bbfa6d889/templates/returnExports.js
(function (root, factory) {
/* c8 ignore start */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.JsonMapping = factory();
}
/* c8 ignore stop */
}(this, function () {
var Decode = {};
var STRING = 0;
var NUMBER = 1;
var INTEGER = 3;
var BOOL = 4;
var FIELD = 5;
var OBJECT = 6;
var INSTANCE = 7;
var MAP = 8;
var AND_THEN = 9;
var LAZY = 10;
var MANY = 11;
var SUCCEED = 12;
var FAIL = 13;
var ONE_OF = 14;
var UNKNOWN = 15;
var DICT = 16;
var FIELD_ERROR_META = 999;
/**
* The 'ok' and 'error' wrappers only exist for performance reasons.
* Previously I had an implementation that threw immediately when
* a decoder failed, but that had a hefty impact on the performance
* of Decode.oneOf, because it expects some of it's child decoders
* to fail.
*
* Now we have a separate decodeInternal function which uses the
* result wrappers and a user-exposed decode function which throws
* if the decoder fails.
*/
function ok(value) {
return { ok: true, value: value };
}
function isOk(result) {
return result.ok === true;
}
function err(msg, meta) {
return { ok: false, msg: msg, meta: meta };
}
/* c8 ignore start */
function debugReplace(key, value) {
if (key === '') {
return value;
} else if (Array.isArray(value)) {
if (value.length === 0) {
return value;
} else if (value.length === 1) {
return '<Array with a single item>';
} else {
return '<Array with ' + value.length + ' items>';
}
} else if (typeof value === 'object' && value !== null) {
var fieldStr = '', fieldCount = 0;
for (var key in value) {
if (fieldCount === 3) {
fieldStr += ', ...';
} else if (fieldCount < 3) {
fieldStr += fieldCount === 0 ? '' : ', ';
fieldStr += '\'' + key + '\'';
}
fieldCount++;
}
if (fieldCount === 0) {
return value;
} else if (fieldCount === 1) {
return '<Object with the field ' + fieldStr + '>';
} else if (fieldCount <= 4) {
return '<Object with these fields: ' + fieldStr + '>';
} else {
return '<Object with ' + fieldCount + ' fields, like ' + fieldStr + '>';
}
} else {
return value;
}
}
/* c8 ignore stop */
function toDebugString(value) {
var str = '\n' + JSON.stringify(value, debugReplace, 4);
return str.replace(/\n/g, '\n ') + '\n';
}
function expected(type, value) {
return 'Expected ' + type + ', but got\n' + toDebugString(value);
}
function decode(decoder, value) {
var result = decodeInternal(decoder, value, value);
if (isOk(result)) {
return result.value;
} else {
throw new Error(result.msg);
}
}
function decodeInternal(decoder, value) {
switch (decoder.tag) {
case NUMBER:
if (typeof value !== 'number' || isNaN(value)) {
return err(expected('a number', value));
} else {
return ok(value);
}
case INTEGER:
if (typeof value !== 'number' || Math.trunc(value) !== value) {
return err(expected('an integer', value));
} else {
return ok(value);
}
case STRING:
if (typeof value !== 'string') {
return err(expected('a string', value));
} else {
return ok(value);
}
case FIELD: {
if (typeof value !== 'object' || value === null) {
return err(expected('an object with a field named \'' + decoder.key + '\'', value));
} else if (!(decoder.key in value)) {
if ('otherwise' in decoder) {
return ok(decoder.otherwise);
}
return err(expected('an object with a field named \'' + decoder.key + '\'', value));
} else {
var result = decodeInternal(decoder.child, value[decoder.key]);
if (isOk(result)) {
return result;
} else {
var msg = result.msg;
msg += '\nwhen attempting to decode the field \'' + decoder.key + '\' of\n' + toDebugString(value);
return err(msg, result.meta);
}
}
}
case BOOL:
if (typeof value !== 'boolean') {
return err(expected('a boolean', value));
} else {
return ok(value);
}
case SUCCEED:
return ok(decoder.value);
case FAIL:
return err(decoder.message);
case LAZY:
var child = decoder.fn();
return decodeInternal(child, value);
case MAP: {
var result = decodeInternal(decoder.child, value);
if (isOk(result)) {
return ok(decoder.fn(result.value));
} else {
return result;
}
}
case AND_THEN: {
var result = decodeInternal(decoder.child, value);
if (isOk(result)) {
return decodeInternal(decoder.fn(result.value), value);
} else {
return result;
}
}
case MANY: {
if (!Array.isArray(value)) {
return err(expected('an array', value));
}
var arr = new Array(value.length);
for (var i=0; i<value.length; i++) {
var result = decodeInternal(decoder.child, value[i]);
if (isOk(result)) {
arr[i] = result.value;
} else {
var msg = result.msg;
msg += '\nwhen attempting to decode the item at index ' + i + ' of\n' + toDebugString(value);
return err(msg, result.meta);
}
}
return ok(arr);
}
case OBJECT:
return decodeObj(decoder.layout, {}, value);
case INSTANCE:
return decodeObj(decoder.layout, new decoder.ctor(), value);
case ONE_OF: {
var decs = decoder.decoders;
for (var i=0; i<decs.length; i++) {
var result = decodeInternal(decs[i], value);
if (isOk(result)) {
return result;
}
}
return err('No oneOf decoder matched');
}
case UNKNOWN: {
return ok(value);
}
case DICT: {
if (typeof value !== 'object' || value === null) {
return err(expected('an object', value));
} else {
var result = {};
for (var key in value) {
var child_value = decodeInternal(decoder.child, value[key]);
if (isOk(child_value)) {
result[key] = child_value.value;
} else {
// TODO Wrap in key info
return child_value;
}
}
return ok(result);
}
}
}
}
function decodeObj(layout, obj, value) {
for (var key in layout) {
var child = layout[key],
result = decodeInternal(child, value);
if (isOk(result)) {
obj[key] = result.value;
} else {
if (result.meta !== FIELD_ERROR_META) {
var msg = 'Did you forget to wrap your decoder in \'Decode.field\'?'
msg += ' This is not done automatically for you when using \'Decode.object\' or \'Decode.instance\'. Here\'s the actual error: ';
msg += result.msg;
return err(msg, FIELD_ERROR_META);
}
return result;
}
}
return ok(obj);
}
Decode.field = function(key, child) {
return { tag: FIELD, key: key, child: child };
};
Decode.string = { tag: STRING };
Decode.number = { tag: NUMBER };
Decode.integer = { tag: INTEGER };
Decode.bool = { tag: BOOL };
Decode.object = function(layout) {
return { tag: OBJECT, layout: layout };
};
Decode.instance = function(ctor, layout) {
return { tag: INSTANCE, ctor: ctor, layout: layout };
};
Decode.lazy = function(fn) {
return { tag: LAZY, fn: fn };
};
Decode.many = function(child) {
return { tag: MANY, child: child };
};
Decode.andThen = function(fn, child) {
return { tag: AND_THEN, child: child, fn: fn };
};
Decode.map = function(fn, child) {
return { tag: MAP, child: child, fn: fn };
};
Decode.succeed = function(value) {
return { tag: SUCCEED, value: value };
};
Decode.fail = function(message) {
return { tag: FAIL, message: message };
};
Decode.oneOf = function(decoders) {
return { tag: ONE_OF, decoders: decoders };
};
Decode.optional = function(val, child) {
return Decode.oneOf([
child,
Decode.succeed(val)
]);
};
Decode.at = function(path, child) {
var dec = child, i = path.length;
while (i-- > 0) {
dec = Decode.field(path[i], dec);
}
return dec;
};
Decode.unknown = { tag: UNKNOWN };
Decode.dict = function(child) {
return { tag: DICT, child: child };
};
Decode.optionalField = function(key, val, child) {
return { tag: FIELD, key: key, child: child, otherwise: val };
};
Decode.optionalAt = function(path, val, child) {
var dec = child, i = path.length;
while (i-- > 0) {
dec = Decode.optionalField(path[i], val, dec);
}
return dec;
};
return {
Decode: Decode,
decode: decode,
decodeString: function(decoder, str) {
var val = JSON.parse(str);
return decode(decoder, val);
},
expected: expected,
};
}));