@jplorg/jpl 1.0.0 → 1.2.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.
@@ -130,6 +130,44 @@ func map(f): ([.[] | f()])
130
130
  | func all(cond): (allBy(func (): (.[]), cond))
131
131
  | func anyBy(f, cond): (not isEmpty(func (): (f() | cond() or void())))
132
132
  | func any(cond): (anyBy(func (): (.[]), cond))
133
+
134
+ | func getPath(path): (
135
+ fields = (path | if type() == 'string' then . / '.' end)
136
+ | reduce->(fields, func (sum): sum[if type->(sum) == 'array' then toNumber() else toString() end], .)
137
+ )
138
+ | func updatePath(path, update): (
139
+ fields = (path | if type() == 'string' then (. / '.')[] |= (v = . | try toNumber() catch v) end)
140
+ | reduce->(
141
+ reverse->(fields),
142
+ func (sum): (
143
+ field = .
144
+ | func (): (
145
+ if type() == 'array'
146
+ then (
147
+ input = .
148
+ | try toNumber->(field) catch toString->(field)
149
+ | if type() == 'number'
150
+ then input[.] |= sum()
151
+ else {}[.] |= sum()
152
+ end
153
+ ) elif type() == 'object'
154
+ then (
155
+ .[toString->(field)] |= sum()
156
+ ) elif type->(field) == 'number'
157
+ then (
158
+ [][field] |= sum()
159
+ ) else (
160
+ {}[toString->(field)] |= sum()
161
+ )
162
+ end
163
+ )
164
+ ),
165
+ update
166
+ )->(.)
167
+ )
168
+ | func setPath(path, value): (
169
+ updatePath(path, func (): value)
170
+ )
133
171
  `;
134
172
  const getBuiltins = (0, _createSingleton.default)(async () => {
135
173
  await undefined;
@@ -68,5 +68,35 @@ class JPLProgram {
68
68
  const outputs = await runtime.execute(normalizedInputs);
69
69
  return runtime.stripJSON(outputs);
70
70
  };
71
+
72
+ /**
73
+ * Return a new program with the provided definition's instructions prepended to the program.
74
+ */
75
+ prepend = programDefinition => {
76
+ validateDefinition(programDefinition);
77
+ const mergedDefinition = {
78
+ version: _library.DEFINITION_VERSION,
79
+ instructions: [...programDefinition.instructions, ...this.definition.instructions]
80
+ };
81
+ return new JPLProgram(mergedDefinition, {
82
+ program: this._options,
83
+ runtime: this._runtimeOptions
84
+ });
85
+ };
86
+
87
+ /**
88
+ * Return a new program with the provided definition's instructions appended to the program.
89
+ */
90
+ append = programDefinition => {
91
+ validateDefinition(programDefinition);
92
+ const mergedDefinition = {
93
+ version: _library.DEFINITION_VERSION,
94
+ instructions: [...this.definition.instructions, ...programDefinition.instructions]
95
+ };
96
+ return new JPLProgram(mergedDefinition, {
97
+ program: this._options,
98
+ runtime: this._runtimeOptions
99
+ });
100
+ };
71
101
  }
72
102
  var _default = exports.default = JPLProgram;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jplorg/jpl",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "JPL interpreter",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {
@@ -122,6 +122,44 @@ func map(f): ([.[] | f()])
122
122
  | func all(cond): (allBy(func (): (.[]), cond))
123
123
  | func anyBy(f, cond): (not isEmpty(func (): (f() | cond() or void())))
124
124
  | func any(cond): (anyBy(func (): (.[]), cond))
125
+
126
+ | func getPath(path): (
127
+ fields = (path | if type() == 'string' then . / '.' end)
128
+ | reduce->(fields, func (sum): sum[if type->(sum) == 'array' then toNumber() else toString() end], .)
129
+ )
130
+ | func updatePath(path, update): (
131
+ fields = (path | if type() == 'string' then (. / '.')[] |= (v = . | try toNumber() catch v) end)
132
+ | reduce->(
133
+ reverse->(fields),
134
+ func (sum): (
135
+ field = .
136
+ | func (): (
137
+ if type() == 'array'
138
+ then (
139
+ input = .
140
+ | try toNumber->(field) catch toString->(field)
141
+ | if type() == 'number'
142
+ then input[.] |= sum()
143
+ else {}[.] |= sum()
144
+ end
145
+ ) elif type() == 'object'
146
+ then (
147
+ .[toString->(field)] |= sum()
148
+ ) elif type->(field) == 'number'
149
+ then (
150
+ [][field] |= sum()
151
+ ) else (
152
+ {}[toString->(field)] |= sum()
153
+ )
154
+ end
155
+ )
156
+ ),
157
+ update
158
+ )->(.)
159
+ )
160
+ | func setPath(path, value): (
161
+ updatePath(path, func (): value)
162
+ )
125
163
  `;
126
164
 
127
165
  const getBuiltins = createSingleton(async () => {
@@ -75,6 +75,40 @@ class JPLProgram {
75
75
  const outputs = await runtime.execute(normalizedInputs);
76
76
  return runtime.stripJSON(outputs);
77
77
  };
78
+
79
+ /**
80
+ * Return a new program with the provided definition's instructions prepended to the program.
81
+ */
82
+ prepend = (programDefinition) => {
83
+ validateDefinition(programDefinition);
84
+
85
+ const mergedDefinition = {
86
+ version: DEFINITION_VERSION,
87
+ instructions: [...programDefinition.instructions, ...this.definition.instructions],
88
+ };
89
+
90
+ return new JPLProgram(mergedDefinition, {
91
+ program: this._options,
92
+ runtime: this._runtimeOptions,
93
+ });
94
+ };
95
+
96
+ /**
97
+ * Return a new program with the provided definition's instructions appended to the program.
98
+ */
99
+ append = (programDefinition) => {
100
+ validateDefinition(programDefinition);
101
+
102
+ const mergedDefinition = {
103
+ version: DEFINITION_VERSION,
104
+ instructions: [...this.definition.instructions, ...programDefinition.instructions],
105
+ };
106
+
107
+ return new JPLProgram(mergedDefinition, {
108
+ program: this._options,
109
+ runtime: this._runtimeOptions,
110
+ });
111
+ };
78
112
  }
79
113
 
80
114
  export default JPLProgram;