@rljson/rljson 0.0.12 → 0.0.15

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/dist/rljson.js CHANGED
@@ -1,160 +1,140 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { hip, hsh } from "@rljson/hash";
1
5
  import { exampleJsonObject } from "@rljson/json";
2
6
  // @license
3
- const bakeryExample = {
4
- buffets: {
5
- _type: "buffets",
6
- _data: [
7
- // Counter
8
- {
9
- items: [
10
- // Angle pie
11
- {
12
- table: "cakes",
13
- ref: "ap"
7
+ const bakeryExample = () => {
8
+ const result = {
9
+ _idSets: {
10
+ _type: "idSets",
11
+ _data: [
12
+ {
13
+ add: ["slice0", "slice1"],
14
+ _hash: "Ko990SJfgPJvNGxC63CRf7"
15
+ }
16
+ ],
17
+ _hash: "NiojsJvZ7iEU7WeMWttzyJ"
18
+ },
19
+ buffets: {
20
+ _type: "buffets",
21
+ _data: [
22
+ {
23
+ items: [
24
+ {
25
+ table: "cakes",
26
+ ref: "KdLv3zTftqKKUeqYrTtO2r"
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ },
32
+ cakes: {
33
+ _type: "cakes",
34
+ _data: [
35
+ {
36
+ idSet: "Ko990SJfgPJvNGxC63CRf7",
37
+ collections: "layers",
38
+ layers: {
39
+ _hash: "RBNvo1WzZ4oRRq0W9-hknp"
14
40
  },
15
- // Cumb cake
16
- {
17
- table: "cakes",
18
- ref: "cc"
41
+ _hash: "KdLv3zTftqKKUeqYrTtO2r"
42
+ }
43
+ ]
44
+ },
45
+ slices: {
46
+ _type: "idSets",
47
+ _data: [
48
+ {
49
+ add: ["slice0", "slice1"],
50
+ remove: [],
51
+ _hash: "wyYfK5E4ArrMKQ_zvi2-EE"
52
+ }
53
+ ],
54
+ _hash: "Qt6FzyzwHdEdYC3fKUXaAm"
55
+ },
56
+ layers: {
57
+ _type: "collections",
58
+ _data: [
59
+ {
60
+ properties: "recipes",
61
+ assign: {
62
+ slice0: "uRTo_Jmt9lOA09e2QnwB9W",
63
+ slice1: "uRTo_Jmt9lOA09e2QnwB9W"
19
64
  }
20
- ]
21
- },
22
- // Fridge
23
- {
24
- id: "fridge",
25
- items: [
26
- // Black forest cake
27
- {
28
- table: "cakes",
29
- ref: "bfc"
65
+ }
66
+ ]
67
+ },
68
+ recipes: {
69
+ _type: "collections",
70
+ _data: [
71
+ {
72
+ properties: "recipeIngredients",
73
+ assign: {
74
+ flour: "RCA4yzQe6mYOqquoLijKop"
30
75
  },
31
- // Lemon cheese cake
32
- {
33
- table: "cakes",
34
- ref: "lcc"
35
- }
36
- ]
37
- }
38
- ]
39
- },
40
- cakes: {
41
- _type: "cakes",
42
- _data: []
43
- },
44
- layers: {
45
- _type: "collections",
46
- _data: []
47
- },
48
- sliceIds: {
49
- _type: "idSets",
50
- _data: [
51
- {
52
- base: null,
53
- add: [
54
- "slice0",
55
- "slice1",
56
- "slice2",
57
- "slice3",
58
- "slice4",
59
- "slice5",
60
- "slice6",
61
- "slice7",
62
- "slice8",
63
- "slice9",
64
- "slice10",
65
- "slice11"
66
- ],
67
- remove: []
68
- }
69
- ]
70
- },
71
- nutritiveValues: {
72
- _type: "properties",
73
- _data: []
74
- }
76
+ _hash: "uRTo_Jmt9lOA09e2QnwB9W"
77
+ }
78
+ ]
79
+ },
80
+ recipeIngredients: {
81
+ _type: "properties",
82
+ _data: [
83
+ {
84
+ ingredientsRef: "CdSJV-WOfnFT1svec3iJ6x",
85
+ quantity: 500,
86
+ _hash: "RCA4yzQe6mYOqquoLijKop"
87
+ }
88
+ ],
89
+ _hash: "R6dJq4ZJ3QDa9Bz8QJDhNq"
90
+ },
91
+ ingredients: {
92
+ _type: "properties",
93
+ _data: [
94
+ {
95
+ name: "flour",
96
+ amountUnit: "g",
97
+ nutritiveValuesRef: "gZXFSlrl5QAs5hOVsq5sWB",
98
+ _hash: "CdSJV-WOfnFT1svec3iJ6x"
99
+ }
100
+ ],
101
+ _hash: "FgJeTM0NcZvXwFcU-PD8Jf"
102
+ },
103
+ nutritiveValues: {
104
+ _type: "properties",
105
+ _data: [
106
+ {
107
+ energy: 364,
108
+ fat: 0.98,
109
+ protein: 10.33,
110
+ carbohydrates: 76.31,
111
+ _hash: "gZXFSlrl5QAs5hOVsq5sWB"
112
+ }
113
+ ],
114
+ _hash: "7k4OqQtk71w3yVGMoA9F6p"
115
+ }
116
+ };
117
+ return result;
75
118
  };
76
119
  // @license
77
- const exampleBuffetsTable = bakeryExample.buffets;
120
+ const exampleBuffetsTable = () => bakeryExample().buffets;
78
121
  // @license
79
- const exampleCakesTable = Object.freeze(bakeryExample.cakes);
122
+ const exampleCakesTable = () => bakeryExample().cakes;
80
123
  // @license
81
- const exampleCollectionsTable = Object.freeze(
82
- bakeryExample.layers
83
- );
124
+ const exampleCollectionsTable = () => bakeryExample().layers;
84
125
  // @license
85
- const exampleIdSetsTable = Object.freeze(bakeryExample.sliceIds);
126
+ const exampleIdSetsTable = () => bakeryExample().slices;
86
127
  // @license
87
- const examplePropertiesTable = Object.freeze(bakeryExample.nutritiveValues);
128
+ const examplePropertiesTable = () => bakeryExample().nutritiveValues;
88
129
  // @license
89
- class Example {
90
- /**
91
- * Returns the Rljson bakery example
92
- */
93
- static bakery() {
94
- return {
95
- // A bakery is a collection of buffets
96
- bakery: {
97
- name: "Bakery",
98
- _type: "buffets",
99
- _data: [
100
- // Counter
101
- {
102
- id: "counter",
103
- items: [
104
- // Angle pie
105
- {
106
- table: "cakes",
107
- ref: "ap"
108
- },
109
- // Cumb cake
110
- {
111
- table: "cakes",
112
- ref: "cc"
113
- }
114
- ]
115
- },
116
- // Fridge
117
- {
118
- id: "fridge",
119
- items: [
120
- // Black forest
121
- {
122
- table: "cakes",
123
- ref: "bf"
124
- },
125
- // Lemon cheese cake
126
- {
127
- table: "cakes",
128
- ref: "cc"
129
- }
130
- ]
131
- }
132
- ]
133
- }
134
- };
135
- }
136
- /**
137
- * Returns an Rljson object with one row containing all JSON types
138
- */
139
- static withAllJsonTypes() {
140
- return {
141
- table: {
142
- _type: "properties",
143
- _data: [exampleJsonObject()]
144
- }
145
- };
146
- }
147
- /**
148
- * Returns an empty Rljson object
149
- */
150
- static empty() {
130
+ const _Example = class _Example {
131
+ };
132
+ __publicField(_Example, "ok", {
133
+ bakery: () => bakeryExample(),
134
+ empty: () => {
151
135
  return {};
152
- }
153
- /**
154
- * Returns an Rljson with a table containing all combinations of true and
155
- * false. This is useful for testing search operators.
156
- */
157
- static binary() {
136
+ },
137
+ binary: () => {
158
138
  return {
159
139
  table: {
160
140
  _type: "properties",
@@ -166,11 +146,81 @@ class Example {
166
146
  ]
167
147
  }
168
148
  };
169
- }
170
- /**
171
- * An more complex example containing an table with multiple rows
172
- */
173
- static multiRow() {
149
+ },
150
+ singleRow: () => {
151
+ const tableCfgs = {
152
+ _hash: "",
153
+ _type: "properties",
154
+ _data: [
155
+ {
156
+ _hash: "R-rCQ4YwYYJAp6uAo6S_6n",
157
+ name: "Single Row Table",
158
+ jsonKey: "table",
159
+ columns: {
160
+ int: {
161
+ jsonKey: "int",
162
+ type: "number",
163
+ name: "Integer",
164
+ nameShort: "Int"
165
+ },
166
+ double: {
167
+ jsonKey: "double",
168
+ type: "number",
169
+ name: "Double",
170
+ nameShort: "Dbl"
171
+ },
172
+ string: {
173
+ jsonKey: "string",
174
+ type: "string",
175
+ name: "String",
176
+ nameShort: "Str"
177
+ },
178
+ boolean: {
179
+ jsonKey: "boolean",
180
+ type: "boolean",
181
+ name: "Boolean",
182
+ nameShort: "Bool"
183
+ },
184
+ null: {
185
+ jsonKey: "null",
186
+ type: "null",
187
+ name: "null",
188
+ nameShort: "null"
189
+ },
190
+ jsonArray: {
191
+ jsonKey: "jsonArray",
192
+ type: "jsonArray",
193
+ name: "Json Array",
194
+ nameShort: "Jarray"
195
+ },
196
+ json: {
197
+ jsonKey: "json",
198
+ type: "json",
199
+ name: "Json",
200
+ nameShort: "Json"
201
+ },
202
+ jsonValue: {
203
+ jsonKey: "jsonValue",
204
+ type: "jsonValue",
205
+ name: "Json Value",
206
+ nameShort: "Jval"
207
+ }
208
+ }
209
+ }
210
+ ]
211
+ };
212
+ const result = {
213
+ _tableCfgs: tableCfgs,
214
+ table: {
215
+ _type: "properties",
216
+ _tableCfg: "R-rCQ4YwYYJAp6uAo6S_6n",
217
+ _data: [exampleJsonObject()],
218
+ _hash: ""
219
+ }
220
+ };
221
+ return result;
222
+ },
223
+ multipleRows: () => {
174
224
  return {
175
225
  table: {
176
226
  _type: "properties",
@@ -199,30 +249,284 @@ class Example {
199
249
  ]
200
250
  }
201
251
  };
202
- }
203
- /**
204
- * Returns an Rljson object with a broken table name
205
- */
206
- static withBrokenTableName() {
252
+ },
253
+ singleRef: () => {
207
254
  return {
208
- brok$en: {
255
+ tableA: {
209
256
  _type: "properties",
210
- _data: []
257
+ _data: [
258
+ {
259
+ keyA0: "a0"
260
+ },
261
+ {
262
+ keyA1: "a1"
263
+ }
264
+ ]
265
+ },
266
+ tableB: {
267
+ _type: "properties",
268
+ _data: [
269
+ {
270
+ tableARef: "KFQrf4mEz0UPmUaFHwH4T6"
271
+ }
272
+ ]
273
+ }
274
+ };
275
+ },
276
+ complete: () => {
277
+ return {
278
+ _idSets: {
279
+ _type: "idSets",
280
+ _data: [
281
+ {
282
+ add: ["id0", "id1"],
283
+ _hash: "MgHRBYSrhpyl4rvsOmAWcQ"
284
+ }
285
+ ]
286
+ },
287
+ properties: {
288
+ _type: "properties",
289
+ _data: [
290
+ { a: "0", _hash: "AFhW-fMzdCiz6bUZscp1Lf" },
291
+ { a: "1", _hash: "mv6w8rID8lQxLsje1EHQMY" }
292
+ ]
293
+ },
294
+ collections: {
295
+ _type: "collections",
296
+ _data: [
297
+ {
298
+ idSet: "MgHRBYSrhpyl4rvsOmAWcQ",
299
+ properties: "properties",
300
+ _hash: "sxv2NCM6UNOcX-i9FhOs5W",
301
+ assign: {}
302
+ },
303
+ {
304
+ base: "sxv2NCM6UNOcX-i9FhOs5W",
305
+ idSet: "MgHRBYSrhpyl4rvsOmAWcQ",
306
+ properties: "properties",
307
+ assign: {
308
+ id0: "AFhW-fMzdCiz6bUZscp1Lf",
309
+ id1: "mv6w8rID8lQxLsje1EHQMY"
310
+ },
311
+ _hash: "QB2JC6X_-rUAoixuldzWP-"
312
+ }
313
+ ]
314
+ },
315
+ cakes: {
316
+ _type: "cakes",
317
+ _data: [
318
+ {
319
+ idSet: "MgHRBYSrhpyl4rvsOmAWcQ",
320
+ collections: "collections",
321
+ layers: {
322
+ layer0: "sxv2NCM6UNOcX-i9FhOs5W",
323
+ layer1: "QB2JC6X_-rUAoixuldzWP-"
324
+ },
325
+ _hash: "QlTVJL3uoXO1L_fw2evLPe"
326
+ }
327
+ ]
328
+ },
329
+ buffets: {
330
+ _type: "buffets",
331
+ _data: [
332
+ {
333
+ items: [
334
+ {
335
+ table: "cakes",
336
+ ref: "QlTVJL3uoXO1L_fw2evLPe"
337
+ },
338
+ {
339
+ table: "collections",
340
+ ref: "QB2JC6X_-rUAoixuldzWP-"
341
+ }
342
+ ]
343
+ }
344
+ ]
211
345
  }
212
346
  };
213
347
  }
214
- }
348
+ });
349
+ __publicField(_Example, "broken", {
350
+ base: {
351
+ brokenTableName: () => {
352
+ return {
353
+ brok$en: {
354
+ _type: "properties",
355
+ _data: []
356
+ }
357
+ };
358
+ },
359
+ missingData: () => {
360
+ return {
361
+ table: {
362
+ _type: "properties"
363
+ }
364
+ };
365
+ },
366
+ dataNotBeingAnArray: () => {
367
+ return {
368
+ table: {
369
+ _type: "properties",
370
+ _data: {}
371
+ }
372
+ };
373
+ },
374
+ missingRef: () => {
375
+ return {
376
+ tableA: {
377
+ _type: "properties",
378
+ _data: [
379
+ {
380
+ keyA0: "a0"
381
+ },
382
+ {
383
+ keyA1: "a1"
384
+ }
385
+ ]
386
+ },
387
+ tableB: {
388
+ _type: "properties",
389
+ _data: [
390
+ {
391
+ tableARef: "MISSINGREF"
392
+ // MISSINGREF does not exist in tableA
393
+ }
394
+ ]
395
+ }
396
+ };
397
+ },
398
+ missingReferencedTable: () => {
399
+ return {
400
+ tableB: {
401
+ _type: "properties",
402
+ _data: [
403
+ {
404
+ tableARef: "MISSINGREF"
405
+ // tableA is missing
406
+ }
407
+ ]
408
+ }
409
+ };
410
+ }
411
+ },
412
+ tableCfg: {
413
+ wrongType: () => {
414
+ const result = _Example.ok.singleRow();
415
+ const tableCfg = result._tableCfgs._data[0];
416
+ tableCfg.columns["int"].type = "numberBroken";
417
+ return hip(result, true, false);
418
+ }
419
+ },
420
+ collections: {
421
+ missingBase: () => {
422
+ const result = _Example.ok.complete();
423
+ const collection1 = result.collections._data[1];
424
+ collection1.base = "MISSING";
425
+ return hip(result, true, false);
426
+ },
427
+ missingIdSet: () => {
428
+ const result = _Example.ok.complete();
429
+ const collection1 = result.collections._data[1];
430
+ collection1.idSet = "MISSING1";
431
+ return hip(result, true, false);
432
+ },
433
+ missingAssignedPropertyTable: () => {
434
+ const result = _Example.ok.complete();
435
+ delete result.properties;
436
+ return result;
437
+ },
438
+ missingAssignedProperty: () => {
439
+ const result = _Example.ok.complete();
440
+ result.properties._data.splice(1, 2);
441
+ return result;
442
+ }
443
+ },
444
+ cakes: {
445
+ missingIdSet: () => {
446
+ const result = _Example.ok.complete();
447
+ result.cakes._data[0].idSet = "MISSING";
448
+ hip(result.cakes, true, false);
449
+ return result;
450
+ },
451
+ missingCollectionsTable: () => {
452
+ const result = _Example.ok.complete();
453
+ result.cakes._data[0].collections = "MISSING";
454
+ hip(result.cakes, true, false);
455
+ return result;
456
+ },
457
+ missingLayerCollection: () => {
458
+ const result = _Example.ok.complete();
459
+ result.cakes._data[0].layers["layer0"] = "MISSING0";
460
+ result.cakes._data[0].layers["layer1"] = "MISSING1";
461
+ hip(result.cakes, true, false);
462
+ return result;
463
+ }
464
+ },
465
+ buffets: {
466
+ missingTable: () => {
467
+ const result = _Example.ok.complete();
468
+ const buffet = result.buffets._data[0];
469
+ buffet.items[0].table = "MISSING0";
470
+ buffet.items[1].table = "MISSING1";
471
+ hip(buffet, true, false);
472
+ return result;
473
+ },
474
+ missingItems: () => {
475
+ const result = _Example.ok.complete();
476
+ const buffet = result.buffets._data[0];
477
+ buffet.items[0].ref = "MISSING0";
478
+ buffet.items[1].ref = "MISSING1";
479
+ hip(buffet, true, false);
480
+ return result;
481
+ }
482
+ }
483
+ });
484
+ let Example = _Example;
485
+ // @license
486
+ const rljsonIndexed = (rljson) => {
487
+ const result = {};
488
+ for (const key in rljson) {
489
+ const item = rljson[key];
490
+ if (typeof item != "object") {
491
+ result[key] = item;
492
+ continue;
493
+ }
494
+ const dataIndexed = {};
495
+ result[key] = { ...item };
496
+ if (!Array.isArray(item._data)) {
497
+ continue;
498
+ }
499
+ for (const row of item._data) {
500
+ const hashedRow = row._hash ? row : hsh(row);
501
+ const hash = hashedRow._hash;
502
+ dataIndexed[hash] = hashedRow;
503
+ }
504
+ result[key]._data = dataIndexed;
505
+ }
506
+ return result;
507
+ };
215
508
  // @license
216
509
  const reservedFieldNames = ["_type", "_data"];
217
510
  const reservedTableNames = ["_hash", "_tables", "_columns"];
218
- const exampleRljson = () => Example.withAllJsonTypes();
511
+ const exampleRljson = () => Example.ok.singleRow();
512
+ const iterateTables = (rljson, callback) => {
513
+ for (const tableName in rljson) {
514
+ const value = rljson[tableName];
515
+ if (typeof value !== "object" || !Array.isArray(value._data)) {
516
+ continue;
517
+ }
518
+ callback(tableName, rljson[tableName]);
519
+ }
520
+ };
219
521
  // @license
220
- const exampleTypedefs = Object.freeze({
221
- ref: "ref",
222
- itemId: "itemId",
223
- tableName: "tableName",
224
- contentType: "collections"
225
- });
522
+ const exampleTypedefs = () => {
523
+ return {
524
+ ref: "ref",
525
+ itemId: "itemId",
526
+ tableName: "tableName",
527
+ contentType: "collections"
528
+ };
529
+ };
226
530
  export {
227
531
  Example,
228
532
  exampleBuffetsTable,
@@ -232,6 +536,8 @@ export {
232
536
  examplePropertiesTable,
233
537
  exampleRljson,
234
538
  exampleTypedefs,
539
+ iterateTables,
235
540
  reservedFieldNames,
236
- reservedTableNames
541
+ reservedTableNames,
542
+ rljsonIndexed
237
543
  };