@appsemble/utils 0.25.2 → 0.27.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.
@@ -1,3 +1,4 @@
1
+ import { schemaExample } from '../../examples.js';
1
2
  export const arrayRemappers = {
2
3
  'array.map': {
3
4
  $ref: '#/components/schemas/RemapperDefinition',
@@ -9,36 +10,7 @@ Output can be an empty array if the supplied data isn’t an array.
9
10
  For example, if you want to sort through a list of people and only get their occupations you can do
10
11
  the following:
11
12
 
12
- Input:
13
-
14
- \`\`\`json
15
- [
16
- {
17
- "name": "Peter",
18
- "occupation": "Delivery driver"
19
- },
20
- {
21
- "name": "Otto",
22
- "occupation": "Scientist"
23
- },
24
- {
25
- "name": "Harry",
26
- "occupation": "CEO"
27
- }
28
- ]
29
- \`\`\`
30
-
31
- \`\`\`yaml
32
- array.map:
33
- object.omit:
34
- - name
35
- \`\`\`
36
-
37
- Result:
38
-
39
- \`\`\`json
40
- [{ "occupation": "Delivery driver" }, { "occupation": "Scientist" }, { "occupation": "CEO" }]
41
- \`\`\`
13
+ ${schemaExample('array.map', { input: 'pretty' })}
42
14
 
43
15
  Another great use for \`array.map\` is to combine it with the \`if\` remapper and sort your arrays on
44
16
  specific values.
@@ -46,30 +18,7 @@ specific values.
46
18
  Using the same input data from the previous example, look at how you can change the code to get
47
19
  people from a specific occupation:
48
20
 
49
- \`\`\`yaml
50
- - array.map:
51
- if:
52
- condition: { equals: [{ prop: occupation }, Scientist] }
53
- then:
54
- object.from:
55
- name:
56
- prop: name
57
- occupation:
58
- prop: occupation
59
- else: null
60
- - null.strip: null
61
- \`\`\`
62
-
63
- Result:
64
-
65
- \`\`\`json
66
- [
67
- {
68
- "name": "Otto",
69
- "occupation": "Scientist"
70
- }
71
- ]
72
- \`\`\`
21
+ ${schemaExample('array.map.1', { input: 'pretty', exclude: ['input'] })}
73
22
 
74
23
  Because \`array.map\` returns an array, every item has to return something. This is why we have to
75
24
  return the full object with the data we want in the \`then\` section. It’s also why we return \`null\`.
@@ -88,21 +37,7 @@ If the value Remapper results in undefined or null, the entire entry is used for
88
37
 
89
38
  If the input is not an array, the input is returned without any modifications.
90
39
 
91
- Input:
92
-
93
- \`\`\`json
94
- [1, 1, 2, 3]
95
- \`\`\`
96
-
97
- \`\`\`yaml
98
- array.unique: null
99
- \`\`\`
100
-
101
- Result:
102
-
103
- \`\`\`json
104
- [1, 2, 3]
105
- \`\`\`
40
+ ${schemaExample('array.unique')}
106
41
 
107
42
  You can also check for more complex values in arrays. The remapper accepts remappers as well, so you
108
43
  can also use entire objects to check for unique values.
@@ -175,22 +110,7 @@ Returns a single object or value from an array based on the given condition.
175
110
 
176
111
  For example:
177
112
 
178
- Input:
179
-
180
- \`\`\`json
181
- [{ name: "Craig" }, { name: "Joey" }, { name: "Stuart" }]
182
- \`\`\`
183
- \`\`\`yaml
184
- array.find:
185
- equals: [{ prop: "name" }, "Craig" ]
186
- \`\`\`
187
-
188
- Result:
189
-
190
- \`\`\`json
191
- { name: "Craig" }
192
- \`\`\`
193
-
113
+ ${schemaExample('array.find')}
194
114
  `,
195
115
  },
196
116
  'array.from': {
@@ -204,16 +124,7 @@ values.
204
124
 
205
125
  For example:
206
126
 
207
- \`\`\`yaml
208
- array.from:
209
- - Peter
210
- - Otto
211
- - Harry
212
- \`\`\`
213
-
214
- \`\`\`json
215
- ["Peter", "Otto", "Harry"]
216
- \`\`\`
127
+ ${schemaExample('array.from', { exclude: ['input'] })}
217
128
 
218
129
  This remapper can also be used to convert given data into an array.
219
130
 
@@ -253,35 +164,7 @@ Append new values to the end of an array. If the input is not an array an empty
253
164
 
254
165
  Using the array from the previous example, we can add a new object on top of it using this remapper:
255
166
 
256
- \`\`\`yaml
257
- array.append:
258
- - object.from:
259
- name: James
260
- occupation: News reporter
261
- \`\`\`
262
-
263
- Result:
264
-
265
- \`\`\`json
266
- [
267
- {
268
- "name": "Peter",
269
- "occupation": "Delivery driver"
270
- },
271
- {
272
- "name": "Otto",
273
- "occupation": "Scientist"
274
- },
275
- {
276
- "name": "Harry",
277
- "occupation": "CEO"
278
- },
279
- {
280
- "name": "James",
281
- "occupation": "News reporter"
282
- }
283
- ]
284
- \`\`\`
167
+ ${schemaExample('array.append', { result: 'pretty', exclude: ['input'] })}
285
168
 
286
169
  Extra example:
287
170
 
@@ -325,29 +208,7 @@ Accepts an array of static or remapper values.
325
208
  With the previous example we added a new person to the list of people, so now we can remove that
326
209
  person. We already know the index of this person in the array is \`3\`, so it’s easy:
327
210
 
328
- \`\`\`yaml
329
- array.omit:
330
- - 3
331
- \`\`\`
332
-
333
- Result:
334
-
335
- \`\`\`json
336
- [
337
- {
338
- "name": "Peter",
339
- "occupation": "Delivery driver"
340
- },
341
- {
342
- "name": "Otto",
343
- "occupation": "Scientist"
344
- },
345
- {
346
- "name": "Harry",
347
- "occupation": "CEO"
348
- }
349
- ]
350
- \`\`\`
211
+ ${schemaExample('array.omit', { result: 'pretty', exclude: ['input'] })}
351
212
 
352
213
  However, usually we don’t know the exact index of the item we want to delete. Because the remapper
353
214
  accepts remappers as input we can get the desired item’s ID from another source as well. Take the
@@ -1,3 +1,4 @@
1
+ import { schemaExample } from '../../examples.js';
1
2
  export const conditionalRemappers = {
2
3
  if: {
3
4
  type: 'object',
@@ -7,24 +8,9 @@ Returns value of \`then\` if condition is truthy, otherwise it returns the value
7
8
 
8
9
  For example:
9
10
 
10
- \`\`\`yaml
11
- if:
12
- condition: { equals: [{ prop: inputValue }, 4] }
13
- then:
14
- static: You guessed right!
15
- else:
16
- static: You guessed wrong!
17
- \`\`\`
18
-
19
- If the \`inputValue\` is \`4\`, it goes to the \`then\` remapper and returns:
20
- \`\`\`
21
- You guessed right!
22
- \`\`\`
23
-
24
- If the \`inputValue\` is something other than \`4\`, it goes to the \`else\` remapper and returns:
25
- \`\`\`
26
- You guessed wrong!
27
- \`\`\`
11
+ ${schemaExample('if.then')}
12
+
13
+ ${schemaExample('if.else', { exclude: ['remapper'] })}
28
14
  `,
29
15
  additionalProperties: false,
30
16
  required: ['condition', 'then', 'else'],
@@ -55,12 +41,7 @@ Returns \`true\` if all entries are equal, otherwise \`false\`.
55
41
  In the following example, if the \`inputValue\` and \`expectedValue\` are of the same value, the
56
42
  condition will return \`true\` and the \`then\` remapper will fire.
57
43
 
58
- \`\`\`yaml
59
- condition:
60
- equals:
61
- - prop: inputValue
62
- - prop: expectedValue
63
- \`\`\`
44
+ ${schemaExample('equals')}
64
45
  `,
65
46
  },
66
47
  gt: {
@@ -73,12 +54,7 @@ Returns \`true\` if the first entry is greater than the second entry.
73
54
 
74
55
  For example, if \`stock\` is more than 5 here, it returns \`true\`.
75
56
 
76
- \`\`\`yaml
77
- condition:
78
- gt:
79
- - prop: stock
80
- - 5
81
- \`\`\`
57
+ ${schemaExample('gt')}
82
58
  `,
83
59
  minItems: 2,
84
60
  maxItems: 2,
@@ -96,12 +72,7 @@ Returns \`true\` if the first entry is lesser than the second entry.
96
72
 
97
73
  For example, if \`stock\` is less than 5 here, it returns \`true\`.
98
74
 
99
- \`\`\`yaml
100
- condition:
101
- lt:
102
- - prop: stock
103
- - 5
104
- \`\`\`
75
+ ${schemaExample('gt')}
105
76
  `,
106
77
  minItems: 2,
107
78
  maxItems: 2,
@@ -122,12 +93,7 @@ If only one remapper or none is passed, the remapper value gets computed and the
122
93
 
123
94
  If \`number\` in the following example is something other than 4, the condition returns \`true\`.
124
95
 
125
- \`\`\`yaml
126
- condition:
127
- not:
128
- - prop: number
129
- - 4
130
- \`\`\`
96
+ ${schemaExample('not')}
131
97
  `,
132
98
  },
133
99
  match: {
@@ -139,17 +105,7 @@ Returns the value of the first case where the condition equals true, otherwise r
139
105
  In the following example, let's say the \`Gem\` is a "Ruby". The match remapper then returns
140
106
  \`value: 75\`.
141
107
 
142
- \`\`\`yaml
143
- match:
144
- - case: { equals: [{ prop: Gem }, Diamond] }
145
- value: 100
146
- - case: { equals: [{ prop: Gem }, Ruby] }
147
- value: 75
148
- - case: { equals: [{ prop: Gem }, Gold] }
149
- value: 50
150
- - case: { equals: [{ prop: Gem }, Sapphire] }
151
- value: 25
152
- \`\`\`
108
+ ${schemaExample('match')}
153
109
  `,
154
110
  items: {
155
111
  type: 'object',
@@ -1,3 +1,4 @@
1
+ import { schemaExample } from '../../examples.js';
1
2
  export const dataRemappers = {
2
3
  array: {
3
4
  enum: ['index', 'length'],
@@ -7,42 +8,12 @@ Returns nothing when not in the context of \`array.map’s\`.
7
8
 
8
9
  For example:
9
10
 
10
- Input:
11
- \`\`\`json
12
- ["a", "b", "c"]
13
- \`\`\`
14
-
15
- This remapper definition maps through the input array and creates an object with the length of the
16
- array and the current index of the loop:
17
- \`\`\`yaml
18
- array.map:
19
- object.from:
20
- length: { array: length }
21
- index: { array: index }
22
- \`\`\`
23
-
24
- Result:
25
- \`\`\`json
26
- [
27
- {
28
- "index": 0,
29
- "length": 3
30
- },
31
- {
32
- "index": 1,
33
- "length": 3
34
- },
35
- {
36
- "index": 2,
37
- "length": 3
38
- }
39
- ]
40
- \`\`\`
11
+ ${schemaExample('array', { result: 'pretty' })}
41
12
  `,
42
13
  },
43
14
  app: {
44
15
  enum: ['id', 'locale', 'url'],
45
- description: `Gives actual information about the current app. Using this remapper you will have access to the
16
+ description: `Gives information about the current app. Using this remapper you can get access to the
46
17
  following information:
47
18
 
48
19
  - \`id\`: App ID
@@ -51,13 +22,7 @@ following information:
51
22
 
52
23
  Example:
53
24
 
54
- \`\`\`json
55
- {
56
- "id": 11,
57
- "locale": "en",
58
- "url": "https://example-app.examplecompany.appsemble.app"
59
- }
60
- \`\`\`
25
+ ${schemaExample('app.url', { result: 'pretty', exclude: ['input'] })}
61
26
  `,
62
27
  },
63
28
  context: {
@@ -241,22 +206,7 @@ The result of the final page’s log would then be:
241
206
  ],
242
207
  description: `Gets the chosen property from an object.
243
208
 
244
- \`\`\`json
245
- {
246
- "name": "John",
247
- "age": 52
248
- }
249
- \`\`\`
250
-
251
- \`\`\`yaml
252
- prop: name
253
- \`\`\`
254
-
255
- Result:
256
-
257
- \`\`\`json
258
- "John"
259
- \`\`\`
209
+ ${schemaExample('prop')}
260
210
  `,
261
211
  },
262
212
  root: {
@@ -296,15 +246,7 @@ Result:
296
246
  static: {
297
247
  description: `Create a static value
298
248
 
299
- \`\`\`yaml
300
- static: Hello!
301
- \`\`\`
302
-
303
- Returns the following string:
304
-
305
- \`\`\`
306
- Hello!
307
- \`\`\`
249
+ ${schemaExample('static', { exclude: ['input'] })}
308
250
  `,
309
251
  },
310
252
  translate: {
@@ -370,10 +312,10 @@ Example:
370
312
  > **Note:** For this remapper to work, the user that activated the remapper has to be logged in to
371
313
  > the app
372
314
 
373
- Provides some fields of the appMember object.
315
+ Provides some fields of the appMember object.
374
316
 
375
317
  - \`userId\`: The id of the user to which the appMember object belongs.
376
- - \`memberId\`: The id of the appMember object itself. This value should be used when fetching resources created by the current user.
318
+ - \`memberId\`: The id of the appMember object itself. This value should be used when fetching resources created by the current user.
377
319
  - \`primary_email\`: User’s **primary** email address.
378
320
  - \`name\`: The user’s name.
379
321
  - \`role\`: User's role in the context of the app.
@@ -1,3 +1,4 @@
1
+ import { schemaExample } from '../../examples.js';
1
2
  export const dateRemappers = {
2
3
  'date.add': {
3
4
  type: 'string',
@@ -21,21 +22,7 @@ Full list of supported unit types:
21
22
 
22
23
  For example:
23
24
 
24
- Date now:
25
- \`\`\`json
26
- 2023-06-30T14:50:19.601Z
27
- \`\`\`
28
-
29
- Remapper definition:
30
- \`\`\`yaml
31
- [{ date.now: null }, { date.add: 1w } , { date.format: null }]
32
- \`\`\`
33
-
34
- Result:
35
- \`\`\`json
36
- 2023-07-07T14:50:19.601Z
37
- \`\`\`
38
-
25
+ ${schemaExample('date.add')}
39
26
  `,
40
27
  },
41
28
  'date.format': {
@@ -49,22 +36,9 @@ In an app definition, it’s best to use this when you want to display a date in
49
36
  For example, if your app has a form with a Datepicker field the incoming data will be formatted
50
37
  as a simple date format. If you want to format it to the RFC3339 format, you can use this remapper.
51
38
 
52
- When you submit a form with a DateField, the internal output looks like this:
53
-
54
- \`\`\`js
55
- 2023-07-03
56
- \`\`\`
57
-
58
- You can then format the date so that it uses the RFC3339 format.
59
-
60
- \`\`\`yaml
61
- date.format: null
62
- \`\`\`
39
+ When you submit a form with a DateField, the internal output looks like the following. You can then format the date so that it uses the RFC3339 format.
63
40
 
64
- Result:
65
- \`\`\`js
66
- "2023-07-02T22:00:00.000Z"
67
- \`\`\`
41
+ ${schemaExample('date.format')}
68
42
 
69
43
  The remapper can also be used to format a date or timestamp using a custom format.
70
44
 
@@ -87,14 +61,8 @@ Result:
87
61
  'date.now': {
88
62
  enum: [null],
89
63
  description: `Returns the current date as a JavaScript Date object.
90
- \`\`\`yaml
91
- date.now: null
92
- \`\`\`
93
64
 
94
- Result:
95
- \`\`\`js
96
- "Mon Jul 03 2023 11:47:18 GMT+0200 (Midden-Europese zomertijd)"
97
- \`\`\`
65
+ ${schemaExample('date.now', { exclude: ['input'] })}
98
66
  `,
99
67
  },
100
68
  'date.parse': {
@@ -102,11 +70,8 @@ Result:
102
70
  description: `Convert a string to a date using a given format.
103
71
 
104
72
  For example:
105
- \`\`\`yaml
106
- - static: 02/11/2014 # The date string to parse
107
- - date.parse: MM/dd/yyyy # The given format to parse the date with
108
- # => Tue Feb 11 2014 00:00:00
109
- \`\`\`
73
+
74
+ ${schemaExample('date.parse', { exclude: ['input'] })}
110
75
 
111
76
  See [date-fns](https://date-fns.org/v2.29.3/docs/parse) for the supported formats.
112
77
 
@@ -1,3 +1,4 @@
1
+ import { schemaExample } from '../../examples.js';
1
2
  export const objectRemappers = {
2
3
  'object.assign': {
3
4
  additionalProperties: {
@@ -9,27 +10,7 @@ Let’s say you have an existing object that you want to add an additional value
9
10
  you can use the \`object.assign\` remapper. The remapper takes an existing object and allows the user
10
11
  to assign their own value on top of that.
11
12
 
12
- Input:
13
-
14
- \`\`\`json
15
- {
16
- "title": "Weekly fishing 21"
17
- }
18
- \`\`\`
19
-
20
- \`\`\`yaml
21
- object.assign:
22
- author: John Doe
23
- \`\`\`
24
-
25
- Result:
26
-
27
- \`\`\`json
28
- {
29
- "title": "Weekly fishing 21",
30
- "author": "John Doe"
31
- }
32
- \`\`\`
13
+ ${schemaExample('object.assign')}
33
14
 
34
15
  `,
35
16
  },
@@ -45,18 +26,7 @@ remappers together to make complex objects.
45
26
 
46
27
  As a base, the remapper looks like this:
47
28
 
48
- \`\`\`yaml
49
- object.from:
50
- username: Chris Taub
51
- email: example@hotmail.com
52
- \`\`\`
53
-
54
- \`\`\`json
55
- {
56
- "username": "Chris Taub",
57
- "email": "example@hotmail.com"
58
- }
59
- \`\`\`
29
+ ${schemaExample('object.from', { exclude: ['input'] })}
60
30
 
61
31
  Most of the time you won’t create an object just to store one value. Luckily, this is where the
62
32
  chaining of remappers comes in. You can create an object that contains an \`object.from\` remapper
@@ -118,39 +88,7 @@ In contrary to the previous remapper, what if you have an object from which you
118
88
  value? Then you can use \`object.omit\`. The remapper can remove properties from an existing object
119
89
  based on the given object keys. This includes nested properties.
120
90
 
121
- Input:
122
-
123
- \`\`\`json
124
- {
125
- "title": "Weekly fishing 21",
126
- "author": "John Doe",
127
- "content": {
128
- "introduction": "This is the introduction for the new weekly fishing issue",
129
- "paragraph1": "...",
130
- "interview": "..."
131
- }
132
- }
133
- \`\`\`
134
-
135
- \`\`\`yaml
136
- object.omit:
137
- - author
138
- - - content
139
- - interview
140
- \`\`\`
141
-
142
- Result:
143
-
144
- \`\`\`json
145
- {
146
- "title": "Weekly fishing 21",
147
- "content": {
148
- "introduction": "This is the introduction for the new weekly fishing issue",
149
- "paragraph1": "..."
150
- }
151
- }
152
- \`\`\`
153
-
91
+ ${schemaExample('object.omit', { input: 'pretty', result: 'pretty' })}
154
92
  `,
155
93
  },
156
94
  };
@@ -1,17 +1,10 @@
1
+ import { schemaExample } from '../../examples.js';
1
2
  export const stringRemappers = {
2
3
  'string.case': {
3
4
  enum: ['lower', 'upper'],
4
5
  description: `Convert a string to upper or lower case.
5
- \`\`\`yaml
6
- string.case: upper
7
- \`\`\`
8
-
9
- Result:
10
-
11
- \`\`\`json
12
- "PATRICK"
13
- \`\`\`
14
6
 
7
+ ${schemaExample('string.case')}
15
8
  `,
16
9
  },
17
10
  'string.format': {
@@ -36,18 +29,7 @@ Result:
36
29
  description: `Format a string using remapped input variables.
37
30
  Useful for replacing static text with generated values.
38
31
 
39
- \`\`\`yaml
40
- string.format:
41
- template: 'You have won €{lotteryAmount} in the lottery!!'
42
- values:
43
- lotteryAmount: { prop: lotteryPrize }
44
- \`\`\`
45
-
46
- Result:
47
-
48
- \`\`\`json
49
- "You have won €5000 in the lottery!!"
50
- \`\`\`
32
+ ${schemaExample('string.format')}
51
33
 
52
34
  > **Tip:** Considering this can be inserted anywhere a remapper is accepted. You can also use this
53
35
  > to choose specific URL’s more accurately.
@@ -64,17 +46,7 @@ Result:
64
46
  description: `
65
47
  Uses RegEx to find a value in a string and replace it with a given value.
66
48
 
67
- \`\`\`yaml
68
- # Input: Eindhoven is the best city in the Netherlands
69
- string.replace:
70
- (beszt*)\\w+: cleanest
71
- \`\`\`
72
-
73
- Result:
74
-
75
- \`\`\`json
76
- "Eindhoven is the cleanest city in the Netherlands"
77
- \`\`\`
49
+ ${schemaExample('string.replace')}
78
50
  `,
79
51
  },
80
52
  };
@@ -162,8 +162,7 @@ The options represent the level of logging that will show in the console.
162
162
  - \`appUrl\`: Base URL of the application,
163
163
  - \`pageData\`: Current page data of a FlowPage (See [page remapper](./data#page)),
164
164
  - \`userInfo\`: User's information if they are logged in (See [user remapper](./data#user)),
165
- - \`context\`:
166
- - \`history\`: Complete list of this remapper’s history (See [history remapper](./history))
165
+ - \`context\`: Internal context
167
166
  - \`history\`: Complete list of this remapper’s history (See [history remapper](./history))
168
167
  - \`locale\`: The user’s locale,
169
168
  - \`stepRef\`: In a loop page, gives the properties from the loop’s current data index (See [step remapper](./data#step))
@@ -198,28 +197,7 @@ For example:
198
197
  "locale": "en",
199
198
  "zoneinfo": "Europe/Amsterdam"
200
199
  },
201
- "context": {
202
- "history": [
203
- [
204
- 0,
205
- 4,
206
- null,
207
- null,
208
- "Peter",
209
- 0.4234,
210
- null
211
- ],
212
- {
213
- "name": "Peter",
214
- "age": 49
215
- },
216
- {
217
- "name": "Peter",
218
- "age": 49,
219
- "birthday": "07-08-2023"
220
- }
221
- ]
222
- },
200
+ "context": {},
223
201
  "history": [
224
202
  [
225
203
  0,