@appsemble/utils 0.32.1-test.17 → 0.32.2-test.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/README.md +3 -3
- package/examples.js +21 -0
- package/package.json +2 -2
- package/reference-schemas/remappers/unsorted.js +15 -0
- package/remap.js +52 -20
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#  Appsemble Utilities
|
|
2
2
|
|
|
3
3
|
> Internal utility functions used across multiple Appsemble projects.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@appsemble/utils)
|
|
6
|
-
[](https://gitlab.com/appsemble/appsemble/-/releases/0.32.2-test.0)
|
|
7
7
|
[](https://prettier.io)
|
|
8
8
|
|
|
9
9
|
## Table of Contents
|
|
@@ -26,5 +26,5 @@ not guaranteed.
|
|
|
26
26
|
|
|
27
27
|
## License
|
|
28
28
|
|
|
29
|
-
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.32.
|
|
29
|
+
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.32.2-test.0/LICENSE.md) ©
|
|
30
30
|
[Appsemble](https://appsemble.com)
|
package/examples.js
CHANGED
|
@@ -611,6 +611,9 @@ export const examples = {
|
|
|
611
611
|
zip: 7500,
|
|
612
612
|
},
|
|
613
613
|
},
|
|
614
|
+
favoriteColors: {
|
|
615
|
+
'array.from': ['blue', 'green'],
|
|
616
|
+
},
|
|
614
617
|
},
|
|
615
618
|
},
|
|
616
619
|
{
|
|
@@ -623,12 +626,19 @@ export const examples = {
|
|
|
623
626
|
country: 'France',
|
|
624
627
|
},
|
|
625
628
|
},
|
|
629
|
+
favoriteColors: { 'array.from': ['red', 'green', 'blue'] },
|
|
626
630
|
},
|
|
627
631
|
},
|
|
628
632
|
],
|
|
629
633
|
},
|
|
630
634
|
result: [
|
|
631
635
|
{ path: ['age'], type: 'changed', from: 25, to: 26 },
|
|
636
|
+
{
|
|
637
|
+
path: ['favoriteColors'],
|
|
638
|
+
type: 'changed',
|
|
639
|
+
from: ['blue', 'green'],
|
|
640
|
+
to: ['red', 'green', 'blue'],
|
|
641
|
+
},
|
|
632
642
|
{ path: ['address', 'city'], type: 'changed', from: 'Paris', to: 'Lyon' },
|
|
633
643
|
{ path: ['address', 'zip'], type: 'removed', value: 7500 },
|
|
634
644
|
{ path: ['address', 'country'], type: 'added', value: 'France' },
|
|
@@ -894,6 +904,17 @@ export const examples = {
|
|
|
894
904
|
},
|
|
895
905
|
result: [true, true, true, false, false],
|
|
896
906
|
},
|
|
907
|
+
maths: {
|
|
908
|
+
input: { version: 0 },
|
|
909
|
+
remapper: {
|
|
910
|
+
maths: {
|
|
911
|
+
a: { prop: 'version' },
|
|
912
|
+
b: { static: 1 },
|
|
913
|
+
operation: 'add',
|
|
914
|
+
},
|
|
915
|
+
},
|
|
916
|
+
result: 1,
|
|
917
|
+
},
|
|
897
918
|
};
|
|
898
919
|
/**
|
|
899
920
|
* @param remapper The remapper example to use.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appsemble/utils",
|
|
3
|
-
"version": "0.32.
|
|
3
|
+
"version": "0.32.2-test.0",
|
|
4
4
|
"description": "Utility functions used in Appsemble internally",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"test": "vitest"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@appsemble/types": "0.32.
|
|
42
|
+
"@appsemble/types": "0.32.2-test.0",
|
|
43
43
|
"@odata/parser": "^0.2.14",
|
|
44
44
|
"axios": "^1.0.0",
|
|
45
45
|
"cron-parser": "^4.0.0",
|
|
@@ -257,5 +257,20 @@ For example:
|
|
|
257
257
|
${schemaExample('xml.parse', { result: 'pretty' })}`,
|
|
258
258
|
$ref: '#/components/schemas/RemapperDefinition',
|
|
259
259
|
},
|
|
260
|
+
maths: {
|
|
261
|
+
description: `This can be used to perform a mathematical operation on two numbers
|
|
262
|
+
|
|
263
|
+
For example:
|
|
264
|
+
|
|
265
|
+
${schemaExample('maths', { result: 'pretty' })}`,
|
|
266
|
+
additionalProperties: false,
|
|
267
|
+
properties: {
|
|
268
|
+
a: { $ref: '#/components/schemas/RemapperDefinition' },
|
|
269
|
+
b: { $ref: '#/components/schemas/RemapperDefinition' },
|
|
270
|
+
operation: {
|
|
271
|
+
enum: ['add', 'divide', 'mod', 'multiply', 'subtract'],
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
},
|
|
260
275
|
};
|
|
261
276
|
//# sourceMappingURL=unsorted.js.map
|
package/remap.js
CHANGED
|
@@ -24,6 +24,9 @@ function isEqualArray(a, b) {
|
|
|
24
24
|
}
|
|
25
25
|
return a.every((val, i) => val === b[i]);
|
|
26
26
|
}
|
|
27
|
+
function isNumber(a) {
|
|
28
|
+
return a !== undefined && a != null && a !== '' && !Number.isNaN(Number(a));
|
|
29
|
+
}
|
|
27
30
|
export function remap(remapper, input, context) {
|
|
28
31
|
if (typeof remapper === 'string' ||
|
|
29
32
|
typeof remapper === 'number' ||
|
|
@@ -227,29 +230,31 @@ const mapperImplementations = {
|
|
|
227
230
|
'object.compare'([remapper1, remapper2], input, context) {
|
|
228
231
|
const remapped1 = remap(remapper1, input, context);
|
|
229
232
|
const remapped2 = remap(remapper2, input, context);
|
|
230
|
-
function deepDiff(
|
|
233
|
+
function deepDiff(object1, object2) {
|
|
234
|
+
const stack = [{ obj1: object1, obj2: object2, path: [] }];
|
|
231
235
|
const diffs = [];
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
236
|
+
while (stack.length !== 0) {
|
|
237
|
+
const { obj1, obj2, path } = stack.pop();
|
|
238
|
+
const keys = new Set([...Object.keys(obj1 || {}), ...Object.keys(obj2 || {})]);
|
|
239
|
+
for (const key of keys) {
|
|
240
|
+
const val1 = obj1?.[key];
|
|
241
|
+
const val2 = obj2?.[key];
|
|
242
|
+
const currentPath = [...path, key];
|
|
243
|
+
if (isPlainObject(val1) && isPlainObject(val2)) {
|
|
244
|
+
stack.push({ obj1: val1, obj2: val2, path: currentPath });
|
|
245
|
+
}
|
|
246
|
+
else if (Array.isArray(val1) && Array.isArray(val2) && !isEqualArray(val1, val2)) {
|
|
247
|
+
diffs.push({ path: currentPath, type: 'changed', from: val1, to: val2 });
|
|
248
|
+
}
|
|
249
|
+
else if (!(key in obj1)) {
|
|
250
|
+
diffs.push({ path: currentPath, type: 'added', value: val2 });
|
|
251
|
+
}
|
|
252
|
+
else if (!(key in obj2)) {
|
|
253
|
+
diffs.push({ path: currentPath, type: 'removed', value: val1 });
|
|
254
|
+
}
|
|
255
|
+
else if (val1 !== val2) {
|
|
242
256
|
diffs.push({ path: currentPath, type: 'changed', from: val1, to: val2 });
|
|
243
257
|
}
|
|
244
|
-
}
|
|
245
|
-
else if (!(key in obj1)) {
|
|
246
|
-
diffs.push({ path: currentPath, type: 'added', value: val2 });
|
|
247
|
-
}
|
|
248
|
-
else if (!(key in obj2)) {
|
|
249
|
-
diffs.push({ path: currentPath, type: 'removed', value: val1 });
|
|
250
|
-
}
|
|
251
|
-
else if (val1 !== val2) {
|
|
252
|
-
diffs.push({ path: currentPath, type: 'changed', from: val1, to: val2 });
|
|
253
258
|
}
|
|
254
259
|
}
|
|
255
260
|
return diffs;
|
|
@@ -625,5 +630,32 @@ const mapperImplementations = {
|
|
|
625
630
|
const remapped = remap(value, input, context);
|
|
626
631
|
return remapped !== undefined && remapped != null;
|
|
627
632
|
},
|
|
633
|
+
maths(value, input, context) {
|
|
634
|
+
const { a, b, operation } = value;
|
|
635
|
+
const aRemapped = remap(a, input, context);
|
|
636
|
+
const bRemapped = remap(b, input, context);
|
|
637
|
+
if (!isNumber(aRemapped) || !isNumber(bRemapped)) {
|
|
638
|
+
return -1;
|
|
639
|
+
}
|
|
640
|
+
const na = Number(aRemapped);
|
|
641
|
+
const nb = Number(bRemapped);
|
|
642
|
+
switch (operation) {
|
|
643
|
+
case 'add':
|
|
644
|
+
return na + nb;
|
|
645
|
+
case 'subtract':
|
|
646
|
+
return na - nb;
|
|
647
|
+
case 'multiply':
|
|
648
|
+
return na * nb;
|
|
649
|
+
case 'divide':
|
|
650
|
+
if (nb === 0) {
|
|
651
|
+
return -1;
|
|
652
|
+
}
|
|
653
|
+
return na / nb;
|
|
654
|
+
case 'mod':
|
|
655
|
+
return na % nb;
|
|
656
|
+
default:
|
|
657
|
+
return -1;
|
|
658
|
+
}
|
|
659
|
+
},
|
|
628
660
|
};
|
|
629
661
|
//# sourceMappingURL=remap.js.map
|