@port-labs/jq-node-bindings 0.0.12 → 0.0.13

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/lib/template.js CHANGED
@@ -93,6 +93,19 @@ const renderRecursively = (inputJson, template, execOptions = {}) => {
93
93
  if (typeof template === 'object' && template !== null) {
94
94
  return Object.fromEntries(
95
95
  Object.entries(template).flatMap(([key, value]) => {
96
+ const SPREAD_KEYWORD = "spreadValue";
97
+ const keywordMatcher = `^\\{\\{\\s*${SPREAD_KEYWORD}\\(\\s*\\)\\s*\\}\\}$`; // matches {{ <Keyword>() }} with white spaces where you'd expect them
98
+
99
+ if (key.trim().match(keywordMatcher)) {
100
+ const evaluatedValue = renderRecursively(inputJson, value, execOptions);
101
+ if (typeof evaluatedValue !== "object") {
102
+ throw new Error(
103
+ `Evaluated value should be an object if the key is ${key}. Original value: ${value}, evaluated to: ${JSON.stringify(evaluatedValue)}`
104
+ );
105
+ }
106
+ return Object.entries(evaluatedValue);
107
+ }
108
+
96
109
  const evaluatedKey = renderRecursively(inputJson, key, execOptions);
97
110
  if (!['undefined', 'string'].includes(typeof evaluatedKey) && evaluatedKey !== null) {
98
111
  throw new Error(
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@port-labs/jq-node-bindings",
3
- "version": "v0.0.12",
3
+ "version": "v0.0.13",
4
4
  "description": "Node.js bindings for JQ",
5
- "jq-node-bindings": "0.0.12",
5
+ "jq-node-bindings": "0.0.13",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
8
8
  "configure": "node-gyp configure",
@@ -125,6 +125,14 @@ describe('template', () => {
125
125
  expect(render({'{{""}}': 'bar'})).toEqual({});
126
126
  expect(render({'{{\'\'}}': 'bar'})).toEqual({});
127
127
  });
128
+ it('testing spread key', () => {
129
+ const json = { foo: "bar" };
130
+ const render = (input) => jq.renderRecursively(json, input);
131
+
132
+ expect(render({ "{{spreadValue()}}": { foo: "bar" } })).toEqual({foo: "bar"});
133
+ expect(render({ " {{ spreadValue( ) }} ": { foo: "bar" } })).toEqual({foo: "bar"});
134
+ expect(render({ "{{spreadValue()}}": "{{ . }}" })).toEqual({ foo: "bar" });
135
+ });
128
136
  it('recursive templates should work', () => {
129
137
  const json = { foo: 'bar', bar: 'foo' };
130
138
  const render = (input) => jq.renderRecursively(json, input);
@@ -165,6 +173,10 @@ describe('template', () => {
165
173
  expect(() => { jq.renderRecursively({foo: "bar"}, '{{.foo + 1}}', {throwOnError: true}) }).toThrow("jq: error: string (\"bar\") and number (1) cannot be added");
166
174
  expect(() => { jq.renderRecursively({}, '{{foo}}/{{bar}}', {throwOnError: true}) }).toThrow("jq: compile error: foo/0 is not defined at <top-level>, line 1:");
167
175
  expect(() => { jq.renderRecursively({}, '/{{foo}}/', {throwOnError: true}) }).toThrow("jq: compile error: foo/0 is not defined at <top-level>, line 1:");
176
+ expect(() => { jq.renderRecursively({}, { "{{ spreadValue() }}": "str" }, { throwOnError: true }) })
177
+ .toThrow('Evaluated value should be an object if the key is {{ spreadValue() }}. Original value: str, evaluated to: "str"');
178
+ expect(() => { jq.renderRecursively({}, { "{{ spreadValue() }}": "{{ \"str\" }}" }, { throwOnError: true }) })
179
+ .toThrow('Evaluated value should be an object if the key is {{ spreadValue() }}. Original value: {{ \"str\" }}, evaluated to: "str"');
168
180
  })
169
181
  })
170
182