-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35-math-methods.ts
More file actions
90 lines (78 loc) · 2.77 KB
/
Copy path35-math-methods.ts
File metadata and controls
90 lines (78 loc) · 2.77 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Math Methods & Type Conversion
//
// JavaScript Math methods and type conversion functions compile to
// JSONata built-in functions that work with dynamic runtime values.
//
// Query Language: JSONata (default)
//
// These methods require JSONata mode. To compile with JSONPath instead
// (where these would be errors), use:
// simplesteps compile 35-math-methods.ts --query-language jsonpath
//
// Or programmatically:
// compile({ sourceFiles: ['35-math-methods.ts'], queryLanguage: 'JSONPath' })
//
// Mapping table:
// Math.floor(x) → $floor(x)
// Math.ceil(x) → $ceil(x)
// Math.round(x) → $round(x)
// Math.abs(x) → $abs(x)
// Math.pow(a, b) → $power(a, b)
// Math.sqrt(x) → $sqrt(x)
// Math.min(a, b) → $min([a, b])
// Math.max(a, b) → $max([a, b])
// Math.random() → $random()
// Number(x) → $number(x)
// String(x) → $string(x)
// Boolean(x) → $boolean(x)
// typeof x → $type(x)
// Date.now() → $millis()
// Array.isArray(x) → $type(x) = 'array'
import { Steps, SimpleStepContext } from '../../packages/core/src/runtime/index';
import { Lambda } from '../../packages/core/src/runtime/services/Lambda';
const getQuote = Lambda<{ productId: string }, { price: number; discount: number; quantity: string; taxRate: number }>(
'arn:aws:lambda:us-east-1:123:function:GetQuote',
);
export const mathMethods = Steps.createFunction(
async (context: SimpleStepContext, input: { productId: string }) => {
const quote = await getQuote.call({ productId: input.productId });
// Rounding → $floor / $ceil / $round
const basePrice = Math.floor(quote.price);
const shippingCeil = Math.ceil(quote.price * 0.1);
const rounded = Math.round(quote.price);
// Absolute value → $abs
const savings = Math.abs(quote.discount);
// Power and square root → $power / $sqrt
const squared = Math.pow(quote.price, 2);
const root = Math.sqrt(quote.price);
// Min/max → $min([...]) / $max([...])
const finalPrice = Math.max(quote.price - quote.discount, 0);
const capped = Math.min(quote.price, 999);
// Random → $random()
const sessionId = Math.random();
// Type conversion → $number / $string / $boolean
const qty = Number(quote.quantity);
const label = String(quote.price);
const hasDiscount = Boolean(quote.discount);
// typeof → $type()
const priceType = typeof quote.price;
// Date.now() → $millis()
const timestamp = Date.now();
return {
basePrice,
shippingCeil,
rounded,
savings,
squared,
root,
finalPrice,
capped,
sessionId,
qty,
label,
hasDiscount,
priceType,
timestamp,
};
},
);