@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.
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ /**
3
+ * Peel and apply trailing no-arg Connectors arrow methods (`->first` / `->last` / `->size`).
4
+ *
5
+ * Used by string interpolation for `$request.headers.x->first` without depending on the
6
+ * full JSONSelection evaluator (and without creating a connect <-> string-interpolation cycle).
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.NO_ARG_ARROW_METHODS = void 0;
10
+ exports.splitArrowMethods = splitArrowMethods;
11
+ exports.applyArrowMethods = applyArrowMethods;
12
+ exports.NO_ARG_ARROW_METHODS = new Set(['first', 'last', 'size']);
13
+ /**
14
+ * Peel trailing Connectors-style `->first` / `->last` / `->size` (no args) from a key.
15
+ */
16
+ function splitArrowMethods(rawKey) {
17
+ const methods = [];
18
+ let key = rawKey;
19
+ while (true) {
20
+ const idx = key.lastIndexOf('->');
21
+ if (idx < 0) {
22
+ break;
23
+ }
24
+ const method = key.slice(idx + 2);
25
+ if (!exports.NO_ARG_ARROW_METHODS.has(method)) {
26
+ break;
27
+ }
28
+ methods.unshift(method);
29
+ key = key.slice(0, idx);
30
+ }
31
+ return { key, arrowMethods: methods };
32
+ }
33
+ /**
34
+ * Apply peeled `->first` / `->last` / `->size` methods to a looked-up value.
35
+ */
36
+ function applyArrowMethods(methods, value) {
37
+ return methods.reduce((val, method) => {
38
+ if (method === 'first') {
39
+ return Array.isArray(val) ? val[0] : val;
40
+ }
41
+ if (method === 'last') {
42
+ return Array.isArray(val) ? (val.length ? val[val.length - 1] : undefined) : val;
43
+ }
44
+ if (method === 'size') {
45
+ if (Array.isArray(val)) {
46
+ return val.length;
47
+ }
48
+ if (val && typeof val === 'object') {
49
+ return Object.keys(val).length;
50
+ }
51
+ if (typeof val === 'string') {
52
+ return val.length;
53
+ }
54
+ return 0;
55
+ }
56
+ return val;
57
+ }, value);
58
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyArrowMethods = exports.splitArrowMethods = exports.NO_ARG_ARROW_METHODS = exports.lookupVar = exports.evalConnectorsVarExpr = exports.evalMatchMethod = exports.evalPath = exports.applySelection = void 0;
4
+ var selection_js_1 = require("./selection.js");
5
+ Object.defineProperty(exports, "applySelection", { enumerable: true, get: function () { return selection_js_1.applySelection; } });
6
+ Object.defineProperty(exports, "evalPath", { enumerable: true, get: function () { return selection_js_1.evalPath; } });
7
+ Object.defineProperty(exports, "evalMatchMethod", { enumerable: true, get: function () { return selection_js_1.evalMatchMethod; } });
8
+ Object.defineProperty(exports, "evalConnectorsVarExpr", { enumerable: true, get: function () { return selection_js_1.evalConnectorsVarExpr; } });
9
+ Object.defineProperty(exports, "lookupVar", { enumerable: true, get: function () { return selection_js_1.lookupVar; } });
10
+ var arrow_methods_js_1 = require("./arrow-methods.js");
11
+ Object.defineProperty(exports, "NO_ARG_ARROW_METHODS", { enumerable: true, get: function () { return arrow_methods_js_1.NO_ARG_ARROW_METHODS; } });
12
+ Object.defineProperty(exports, "splitArrowMethods", { enumerable: true, get: function () { return arrow_methods_js_1.splitArrowMethods; } });
13
+ Object.defineProperty(exports, "applyArrowMethods", { enumerable: true, get: function () { return arrow_methods_js_1.applyArrowMethods; } });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}