-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36-array-methods.ts
More file actions
72 lines (60 loc) · 2.32 KB
/
36-array-methods.ts
File metadata and controls
72 lines (60 loc) · 2.32 KB
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
// Array Methods & Object Utilities
//
// Array methods and Object utilities that compile to JSONata
// built-in functions for data manipulation.
//
// Query Language: JSONata (default)
//
// These methods require JSONata mode. To compile with JSONPath instead
// (where these would be errors), use:
// simplesteps compile 36-array-methods.ts --query-language jsonpath
//
// Or programmatically:
// compile({ sourceFiles: ['36-array-methods.ts'], queryLanguage: 'JSONPath' })
//
// Mapping table:
// arr.join(delim) → $join(arr, delim)
// arr.reverse() → $reverse(arr)
// arr.sort() → $sort(arr)
// arr.concat(b) → $append(arr, b)
// arr.length → $count(arr) [also: States.ArrayLength in JSONPath]
// arr.includes(val) → States.ArrayContains(arr, val) [works in both modes]
// Object.keys(o) → $keys(o)
// Object.values(o) → $lookup(o, $keys(o))
import { Steps, SimpleStepContext } from '../../packages/core/src/runtime/index';
import { Lambda } from '../../packages/core/src/runtime/services/Lambda';
const getInventory = Lambda<
{ warehouse: string },
{ items: string[]; backorder: string[]; metadata: Record<string, string> }
>('arn:aws:lambda:us-east-1:123:function:GetInventory');
export const arrayMethods = Steps.createFunction(
async (context: SimpleStepContext, input: { warehouse: string; requiredItem: string }) => {
const inv = await getInventory.call({ warehouse: input.warehouse });
// Join → $join(arr, delim)
const itemList = inv.items.join(', ');
// Reverse → $reverse(arr)
const reversed = inv.items.reverse();
// Sort → $sort(arr)
const sorted = inv.items.sort();
// Concat → $append(arr, arr)
const allItems = inv.items.concat(inv.backorder);
// Length → $count(arr)
const totalCount = inv.items.length;
// Includes → States.ArrayContains (works in both modes)
const inStock = inv.items.includes(input.requiredItem);
// Object.keys → $keys(o)
const metaKeys = Object.keys(inv.metadata);
// Object.values → $lookup(o, $keys(o))
const metaValues = Object.values(inv.metadata);
return {
itemList,
reversed,
sorted,
allItems,
totalCount,
inStock,
metaKeys,
metaValues,
};
},
);