@appsemble/utils 0.28.11 → 0.28.12
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/api/components/schemas/BlockDefinition.js +4 -0
- package/examples.js +7 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +2 -2
- package/reference-schemas/remappers/arrays.js +10 -0
- package/reference-schemas/remappers/strings.js +1 -1
- package/remap.js +9 -1
- package/remap.test.js +48 -0
- package/serializeResource.d.ts +3 -0
- package/serializeResource.js +30 -0
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.28.12)
|
|
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.28.
|
|
29
|
+
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.28.12/LICENSE.md) ©
|
|
30
30
|
[Appsemble](https://appsemble.com)
|
|
@@ -41,6 +41,10 @@ Layout float can be combined with the \`position\` property to place the block o
|
|
|
41
41
|
`,
|
|
42
42
|
enum: ['float', 'grow', 'static'],
|
|
43
43
|
},
|
|
44
|
+
hide: {
|
|
45
|
+
$ref: '#/components/schemas/RemapperDefinition',
|
|
46
|
+
description: 'Whether to render the block or not',
|
|
47
|
+
},
|
|
44
48
|
header: {
|
|
45
49
|
$ref: '#/components/schemas/RemapperDefinition',
|
|
46
50
|
description: 'An optional header to render above the block.',
|
package/examples.js
CHANGED
|
@@ -273,6 +273,13 @@ export const examples = {
|
|
|
273
273
|
},
|
|
274
274
|
result: [1, 2, 3],
|
|
275
275
|
},
|
|
276
|
+
'array.flatten': {
|
|
277
|
+
input: [['Milka', 'Sven'], 'Goldie'],
|
|
278
|
+
remapper: {
|
|
279
|
+
'array.flatten': null,
|
|
280
|
+
},
|
|
281
|
+
result: ['Milka', 'Sven', 'Goldie'],
|
|
282
|
+
},
|
|
276
283
|
'assign.history': {
|
|
277
284
|
input: null,
|
|
278
285
|
remapper: {},
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appsemble/utils",
|
|
3
|
-
"version": "0.28.
|
|
3
|
+
"version": "0.28.12",
|
|
4
4
|
"description": "Utility functions used in Appsemble internally",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"test": "vitest"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@appsemble/types": "0.28.
|
|
40
|
+
"@appsemble/types": "0.28.12",
|
|
41
41
|
"axios": "^1.0.0",
|
|
42
42
|
"cron-parser": "^4.0.0",
|
|
43
43
|
"date-fns": "^2.0.0",
|
|
@@ -245,5 +245,15 @@ actions:
|
|
|
245
245
|
\`\`\`
|
|
246
246
|
`,
|
|
247
247
|
},
|
|
248
|
+
'array.flatten': {
|
|
249
|
+
$ref: '#/components/schemas/RemapperDefinition',
|
|
250
|
+
description: `
|
|
251
|
+
Flatten an array. Accepts an array of static or remapper values.
|
|
252
|
+
|
|
253
|
+
Say for example that each person has some pets. We could get an array of all pets like so:
|
|
254
|
+
|
|
255
|
+
${schemaExample('array.flatten', { input: 'pretty', exclude: ['remapper'] })}
|
|
256
|
+
`,
|
|
257
|
+
},
|
|
248
258
|
};
|
|
249
259
|
//# sourceMappingURL=arrays.js.map
|
|
@@ -12,7 +12,7 @@ ${schemaExample('string.case')}
|
|
|
12
12
|
additionalProperties: false,
|
|
13
13
|
properties: {
|
|
14
14
|
messageId: {
|
|
15
|
-
|
|
15
|
+
$ref: '#/components/schemas/RemapperDefinition',
|
|
16
16
|
description: 'The message id pointing to the template string to format.',
|
|
17
17
|
},
|
|
18
18
|
template: {
|
package/remap.js
CHANGED
|
@@ -250,6 +250,13 @@ const mapperImplementations = {
|
|
|
250
250
|
}));
|
|
251
251
|
return Array.isArray(input) ? input.filter((value, i) => !indices.has(i)) : [];
|
|
252
252
|
},
|
|
253
|
+
'array.flatten'(mapper, input, context) {
|
|
254
|
+
if (!Array.isArray(input)) {
|
|
255
|
+
return input;
|
|
256
|
+
}
|
|
257
|
+
const depth = remap(mapper, input, context);
|
|
258
|
+
return input.flat(depth || Number.POSITIVE_INFINITY);
|
|
259
|
+
},
|
|
253
260
|
static: (input) => input,
|
|
254
261
|
prop(prop, obj, context) {
|
|
255
262
|
let result = obj;
|
|
@@ -387,7 +394,8 @@ const mapperImplementations = {
|
|
|
387
394
|
},
|
|
388
395
|
'string.format'({ messageId, template, values }, input, context) {
|
|
389
396
|
try {
|
|
390
|
-
const
|
|
397
|
+
const remappedMessageId = remap(messageId, input, context);
|
|
398
|
+
const message = context.getMessage({ id: remappedMessageId, defaultMessage: template });
|
|
391
399
|
return message.format(values ? mapValues(values, (val) => remap(val, input, context)) : undefined);
|
|
392
400
|
}
|
|
393
401
|
catch (error) {
|
package/remap.test.js
CHANGED
|
@@ -791,6 +791,54 @@ describe('array.unique', () => {
|
|
|
791
791
|
},
|
|
792
792
|
});
|
|
793
793
|
});
|
|
794
|
+
describe('array.flatten', () => {
|
|
795
|
+
runTests({
|
|
796
|
+
'with unspecified depth': {
|
|
797
|
+
input: [
|
|
798
|
+
'a',
|
|
799
|
+
['b'],
|
|
800
|
+
['c', 'd'],
|
|
801
|
+
[
|
|
802
|
+
['e', 'f'],
|
|
803
|
+
['g', 'h'],
|
|
804
|
+
],
|
|
805
|
+
],
|
|
806
|
+
mappers: [{ 'array.flatten': null }],
|
|
807
|
+
expected: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
|
|
808
|
+
},
|
|
809
|
+
'with statically specified depth': {
|
|
810
|
+
input: [
|
|
811
|
+
'a',
|
|
812
|
+
['b'],
|
|
813
|
+
['c', 'd'],
|
|
814
|
+
[
|
|
815
|
+
['e', 'f'],
|
|
816
|
+
['g', 'h'],
|
|
817
|
+
],
|
|
818
|
+
],
|
|
819
|
+
mappers: [{ 'array.flatten': 1 }],
|
|
820
|
+
expected: ['a', 'b', 'c', 'd', ['e', 'f'], ['g', 'h']],
|
|
821
|
+
},
|
|
822
|
+
'with dynamically specified depth': {
|
|
823
|
+
input: [
|
|
824
|
+
'a',
|
|
825
|
+
['b'],
|
|
826
|
+
['c', 'd'],
|
|
827
|
+
[
|
|
828
|
+
['e', 'f'],
|
|
829
|
+
['g', 'h'],
|
|
830
|
+
],
|
|
831
|
+
],
|
|
832
|
+
mappers: [{ 'array.flatten': { 'number.parse': '1' } }],
|
|
833
|
+
expected: ['a', 'b', 'c', 'd', ['e', 'f'], ['g', 'h']],
|
|
834
|
+
},
|
|
835
|
+
'with non array input': {
|
|
836
|
+
input: 'Oops',
|
|
837
|
+
mappers: [{ 'array.flatten': null }],
|
|
838
|
+
expected: 'Oops',
|
|
839
|
+
},
|
|
840
|
+
});
|
|
841
|
+
});
|
|
794
842
|
describe('array', () => {
|
|
795
843
|
runTests({
|
|
796
844
|
'return undefined if not in the context of array.map': {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { mapValues } from './mapValues.js';
|
|
2
|
+
export function serializeResource(data) {
|
|
3
|
+
const assets = [];
|
|
4
|
+
const extractAssets = (value) => {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
return value.map(extractAssets);
|
|
7
|
+
}
|
|
8
|
+
if (value instanceof Blob) {
|
|
9
|
+
return String(assets.push(value) - 1);
|
|
10
|
+
}
|
|
11
|
+
if (value instanceof Date) {
|
|
12
|
+
return value.toJSON();
|
|
13
|
+
}
|
|
14
|
+
if (value && typeof value === 'object') {
|
|
15
|
+
return mapValues(value, extractAssets);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
const resource = extractAssets(data);
|
|
20
|
+
if (!assets.length) {
|
|
21
|
+
return resource;
|
|
22
|
+
}
|
|
23
|
+
const form = new FormData();
|
|
24
|
+
form.set('resource', JSON.stringify(resource));
|
|
25
|
+
for (const asset of assets) {
|
|
26
|
+
form.append('assets', asset);
|
|
27
|
+
}
|
|
28
|
+
return form;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=serializeResource.js.map
|