@graphql-mesh/json-selection 0.0.0-stage → 0.0.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/cjs/arrow-methods.js +58 -0
- package/cjs/index.js +13 -0
- package/cjs/package.json +1 -0
- package/cjs/selection.js +1096 -0
- package/cjs/types.js +5 -0
- package/esm/arrow-methods.js +53 -0
- package/esm/index.js +2 -0
- package/esm/selection.js +1084 -0
- package/esm/types.js +4 -0
- package/package.json +40 -3
- package/typings/arrow-methods.d.cts +18 -0
- package/typings/arrow-methods.d.ts +18 -0
- package/typings/index.d.cts +5 -0
- package/typings/index.d.ts +5 -0
- package/typings/selection.d.cts +46 -0
- package/typings/selection.d.ts +46 -0
- package/typings/types.d.cts +18 -0
- package/typings/types.d.ts +18 -0
- package/README.md +0 -3
package/cjs/types.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Peel and apply trailing no-arg Connectors arrow methods (`->first` / `->last` / `->size`).
|
|
3
|
+
*
|
|
4
|
+
* Used by string interpolation for `$request.headers.x->first` without depending on the
|
|
5
|
+
* full JSONSelection evaluator (and without creating a connect <-> string-interpolation cycle).
|
|
6
|
+
*/
|
|
7
|
+
export const NO_ARG_ARROW_METHODS = new Set(['first', 'last', 'size']);
|
|
8
|
+
/**
|
|
9
|
+
* Peel trailing Connectors-style `->first` / `->last` / `->size` (no args) from a key.
|
|
10
|
+
*/
|
|
11
|
+
export function splitArrowMethods(rawKey) {
|
|
12
|
+
const methods = [];
|
|
13
|
+
let key = rawKey;
|
|
14
|
+
while (true) {
|
|
15
|
+
const idx = key.lastIndexOf('->');
|
|
16
|
+
if (idx < 0) {
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
const method = key.slice(idx + 2);
|
|
20
|
+
if (!NO_ARG_ARROW_METHODS.has(method)) {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
methods.unshift(method);
|
|
24
|
+
key = key.slice(0, idx);
|
|
25
|
+
}
|
|
26
|
+
return { key, arrowMethods: methods };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Apply peeled `->first` / `->last` / `->size` methods to a looked-up value.
|
|
30
|
+
*/
|
|
31
|
+
export function applyArrowMethods(methods, value) {
|
|
32
|
+
return methods.reduce((val, method) => {
|
|
33
|
+
if (method === 'first') {
|
|
34
|
+
return Array.isArray(val) ? val[0] : val;
|
|
35
|
+
}
|
|
36
|
+
if (method === 'last') {
|
|
37
|
+
return Array.isArray(val) ? (val.length ? val[val.length - 1] : undefined) : val;
|
|
38
|
+
}
|
|
39
|
+
if (method === 'size') {
|
|
40
|
+
if (Array.isArray(val)) {
|
|
41
|
+
return val.length;
|
|
42
|
+
}
|
|
43
|
+
if (val && typeof val === 'object') {
|
|
44
|
+
return Object.keys(val).length;
|
|
45
|
+
}
|
|
46
|
+
if (typeof val === 'string') {
|
|
47
|
+
return val.length;
|
|
48
|
+
}
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
return val;
|
|
52
|
+
}, value);
|
|
53
|
+
}
|
package/esm/index.js
ADDED