@openfn/language-common 1.13.5 → 1.15.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/ast.json CHANGED
@@ -86,6 +86,59 @@
86
86
  },
87
87
  "valid": true
88
88
  },
89
+ {
90
+ "name": "fnIf",
91
+ "params": [
92
+ "condition",
93
+ "operation"
94
+ ],
95
+ "docs": {
96
+ "description": "A custom operation that will only execute the function if the condition returns true",
97
+ "tags": [
98
+ {
99
+ "title": "public",
100
+ "description": null,
101
+ "type": null
102
+ },
103
+ {
104
+ "title": "example",
105
+ "description": "fnIf((state) => state?.data?.name, get(\"https://example.com\"));"
106
+ },
107
+ {
108
+ "title": "function",
109
+ "description": null,
110
+ "name": null
111
+ },
112
+ {
113
+ "title": "param",
114
+ "description": "The condition that returns true",
115
+ "type": {
116
+ "type": "NameExpression",
117
+ "name": "Boolean"
118
+ },
119
+ "name": "condition"
120
+ },
121
+ {
122
+ "title": "param",
123
+ "description": "The operation needed to be executed.",
124
+ "type": {
125
+ "type": "NameExpression",
126
+ "name": "Operation"
127
+ },
128
+ "name": "operation"
129
+ },
130
+ {
131
+ "title": "returns",
132
+ "description": null,
133
+ "type": {
134
+ "type": "NameExpression",
135
+ "name": "Operation"
136
+ }
137
+ }
138
+ ]
139
+ },
140
+ "valid": true
141
+ },
89
142
  {
90
143
  "name": "jsonValue",
91
144
  "params": [
@@ -841,6 +894,78 @@
841
894
  },
842
895
  "valid": true
843
896
  },
897
+ {
898
+ "name": "group",
899
+ "params": [
900
+ "arrayOfObjects",
901
+ "keyPath",
902
+ "callback"
903
+ ],
904
+ "docs": {
905
+ "description": "Groups an array of objects by a specified key path.",
906
+ "tags": [
907
+ {
908
+ "title": "public",
909
+ "description": null,
910
+ "type": null
911
+ },
912
+ {
913
+ "title": "example",
914
+ "description": "const users = [\n { name: 'Alice', age: 25, city: 'New York' },\n { name: 'Bob', age: 30, city: 'San Francisco' },\n { name: 'Charlie', age: 25, city: 'New York' },\n { name: 'David', age: 30, city: 'San Francisco' }\n];\ngroup(users, 'city');\n// state is { data: { 'New York': [/Alice, Charlie/], 'San Francisco': [ /Bob, David / ] }"
915
+ },
916
+ {
917
+ "title": "function",
918
+ "description": null,
919
+ "name": null
920
+ },
921
+ {
922
+ "title": "param",
923
+ "description": "The array of objects to be grouped.",
924
+ "type": {
925
+ "type": "TypeApplication",
926
+ "expression": {
927
+ "type": "NameExpression",
928
+ "name": "Array"
929
+ },
930
+ "applications": [
931
+ {
932
+ "type": "NameExpression",
933
+ "name": "Object"
934
+ }
935
+ ]
936
+ },
937
+ "name": "arrayOfObjects"
938
+ },
939
+ {
940
+ "title": "param",
941
+ "description": "The key path to group by.",
942
+ "type": {
943
+ "type": "NameExpression",
944
+ "name": "string"
945
+ },
946
+ "name": "keyPath"
947
+ },
948
+ {
949
+ "title": "param",
950
+ "description": "(Optional) Callback function",
951
+ "type": {
952
+ "type": "NameExpression",
953
+ "name": "function"
954
+ },
955
+ "name": "callback"
956
+ },
957
+ {
958
+ "title": "returns",
959
+ "description": null,
960
+ "type": {
961
+ "type": "NameExpression",
962
+ "name": "Operation"
963
+ }
964
+ }
965
+ ]
966
+ },
967
+ "valid": true
968
+ },
844
969
  {
845
970
  "name": "index",
846
971
  "params": [],
@@ -1319,7 +1444,7 @@
1319
1444
  "options"
1320
1445
  ],
1321
1446
  "docs": {
1322
- "description": "Sets a cursor property on state.\nSupports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,\nwhich will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones \nare not yet supported.\nYou can provide a formatter to customise the final cursor value, which is useful for normalising\ndifferent inputs. The custom formatter runs after natural language date conversion.\nSee the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}",
1447
+ "description": "Sets a cursor property on state.\nSupports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,\nwhich will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones\nare not yet supported.\nYou can provide a formatter to customise the final cursor value, which is useful for normalising\ndifferent inputs. The custom formatter runs after natural language date conversion.\nSee the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}",
1323
1448
  "tags": [
1324
1449
  {
1325
1450
  "title": "public",
package/dist/index.cjs CHANGED
@@ -43,6 +43,8 @@ __export(src_exports, {
43
43
  field: () => field,
44
44
  fields: () => fields,
45
45
  fn: () => fn,
46
+ fnIf: () => fnIf,
47
+ group: () => group,
46
48
  http: () => http_deprecated_exports,
47
49
  humanProper: () => humanProper,
48
50
  index: () => index,
@@ -82,6 +84,8 @@ __export(Adaptor_exports, {
82
84
  field: () => field,
83
85
  fields: () => fields,
84
86
  fn: () => fn,
87
+ fnIf: () => fnIf,
88
+ group: () => group,
85
89
  http: () => http_deprecated_exports,
86
90
  humanProper: () => humanProper,
87
91
  index: () => index,
@@ -99,7 +103,10 @@ __export(Adaptor_exports, {
99
103
  toArray: () => toArray,
100
104
  validate: () => validate
101
105
  });
106
+ var import_get = __toESM(require("lodash/get.js"), 1);
107
+ var import_omit = __toESM(require("lodash/omit.js"), 1);
102
108
  var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
109
+ var import_groupBy = __toESM(require("lodash/groupBy.js"), 1);
103
110
  var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
104
111
  var import_jsonpath_plus = require("jsonpath-plus");
105
112
  var import_csv_parse = require("csv-parse");
@@ -333,6 +340,12 @@ function fn(func) {
333
340
  return func(state);
334
341
  };
335
342
  }
343
+ function fnIf(condition, operation) {
344
+ return (state) => {
345
+ const [resolvedCondition] = expandReferences(state, condition);
346
+ return resolvedCondition ? operation(state) : state;
347
+ };
348
+ }
336
349
  function jsonValue(obj, path) {
337
350
  return (0, import_jsonpath_plus.JSONPath)({ path, json: obj })[0];
338
351
  }
@@ -454,6 +467,17 @@ function merge(dataSource, fields2) {
454
467
  }, []);
455
468
  };
456
469
  }
470
+ function group(arrayOfObjects, keyPath, callback = (s) => s) {
471
+ return (state) => {
472
+ const [resolvedArray, resolvedKeyPath] = expandReferences(
473
+ state,
474
+ arrayOfObjects,
475
+ keyPath
476
+ );
477
+ const results = (0, import_groupBy.default)(resolvedArray, (item) => (0, import_get.default)(item, resolvedKeyPath));
478
+ return callback({ ...state, data: (0, import_omit.default)(results, [void 0]) });
479
+ };
480
+ }
457
481
  function index() {
458
482
  return (state) => {
459
483
  return state.index;
@@ -466,6 +490,9 @@ function toArray(arg) {
466
490
  return new Array().concat(arg);
467
491
  }
468
492
  function composeNextState(state, response) {
493
+ if (!state.references) {
494
+ state.references = [];
495
+ }
469
496
  return {
470
497
  ...state,
471
498
  data: response,
@@ -647,7 +674,11 @@ var cursorKey = "cursor";
647
674
  function cursor(value, options2 = {}) {
648
675
  return (state) => {
649
676
  const { format: format2, ...optionsWithoutFormat } = options2;
650
- const [resolvedValue, resolvedOptions] = expandReferences(state, value, optionsWithoutFormat);
677
+ const [resolvedValue, resolvedOptions] = expandReferences(
678
+ state,
679
+ value,
680
+ optionsWithoutFormat
681
+ );
651
682
  const {
652
683
  defaultValue,
653
684
  key
@@ -695,6 +726,8 @@ var src_default = Adaptor_exports;
695
726
  field,
696
727
  fields,
697
728
  fn,
729
+ fnIf,
730
+ group,
698
731
  http,
699
732
  humanProper,
700
733
  index,
package/dist/index.js CHANGED
@@ -24,6 +24,8 @@ __export(Adaptor_exports, {
24
24
  field: () => field,
25
25
  fields: () => fields,
26
26
  fn: () => fn,
27
+ fnIf: () => fnIf,
28
+ group: () => group,
27
29
  http: () => http_deprecated_exports,
28
30
  humanProper: () => humanProper,
29
31
  index: () => index,
@@ -41,7 +43,10 @@ __export(Adaptor_exports, {
41
43
  toArray: () => toArray,
42
44
  validate: () => validate
43
45
  });
46
+ import get2 from "lodash/get.js";
47
+ import omit from "lodash/omit.js";
44
48
  import curry from "lodash/fp/curry.js";
49
+ import groupBy from "lodash/groupBy.js";
45
50
  import fromPairs from "lodash/fp/fromPairs.js";
46
51
  import { JSONPath } from "jsonpath-plus";
47
52
  import { parse as parse2 } from "csv-parse";
@@ -273,6 +278,12 @@ function fn(func) {
273
278
  return func(state);
274
279
  };
275
280
  }
281
+ function fnIf(condition, operation) {
282
+ return (state) => {
283
+ const [resolvedCondition] = expandReferences(state, condition);
284
+ return resolvedCondition ? operation(state) : state;
285
+ };
286
+ }
276
287
  function jsonValue(obj, path) {
277
288
  return JSONPath({ path, json: obj })[0];
278
289
  }
@@ -394,6 +405,17 @@ function merge(dataSource, fields2) {
394
405
  }, []);
395
406
  };
396
407
  }
408
+ function group(arrayOfObjects, keyPath, callback = (s) => s) {
409
+ return (state) => {
410
+ const [resolvedArray, resolvedKeyPath] = expandReferences(
411
+ state,
412
+ arrayOfObjects,
413
+ keyPath
414
+ );
415
+ const results = groupBy(resolvedArray, (item) => get2(item, resolvedKeyPath));
416
+ return callback({ ...state, data: omit(results, [void 0]) });
417
+ };
418
+ }
397
419
  function index() {
398
420
  return (state) => {
399
421
  return state.index;
@@ -406,6 +428,9 @@ function toArray(arg) {
406
428
  return new Array().concat(arg);
407
429
  }
408
430
  function composeNextState(state, response) {
431
+ if (!state.references) {
432
+ state.references = [];
433
+ }
409
434
  return {
410
435
  ...state,
411
436
  data: response,
@@ -587,7 +612,11 @@ var cursorKey = "cursor";
587
612
  function cursor(value, options2 = {}) {
588
613
  return (state) => {
589
614
  const { format: format2, ...optionsWithoutFormat } = options2;
590
- const [resolvedValue, resolvedOptions] = expandReferences(state, value, optionsWithoutFormat);
615
+ const [resolvedValue, resolvedOptions] = expandReferences(
616
+ state,
617
+ value,
618
+ optionsWithoutFormat
619
+ );
591
620
  const {
592
621
  defaultValue,
593
622
  key
@@ -635,6 +664,8 @@ export {
635
664
  field,
636
665
  fields,
637
666
  fn,
667
+ fnIf,
668
+ group,
638
669
  http_deprecated_exports as http,
639
670
  humanProper,
640
671
  index,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.13.5",
3
+ "version": "1.15.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -32,6 +32,17 @@ export function alterState(func: Function): Operation;
32
32
  * @returns {Operation}
33
33
  */
34
34
  export function fn(func: Function): Operation;
35
+ /**
36
+ * A custom operation that will only execute the function if the condition returns true
37
+ * @public
38
+ * @example
39
+ * fnIf((state) => state?.data?.name, get("https://example.com"));
40
+ * @function
41
+ * @param {Boolean} condition - The condition that returns true
42
+ * @param {Operation} operation - The operation needed to be executed.
43
+ * @returns {Operation}
44
+ */
45
+ export function fnIf(condition: boolean, operation: Operation): Operation;
35
46
  /**
36
47
  * Picks out a single value from a JSON object.
37
48
  * If a JSONPath returns more than one value for the reference, the first
@@ -219,6 +230,25 @@ export function fields(...fields: Fields): any;
219
230
  * @returns {DataSource}
220
231
  */
221
232
  export function merge(dataSource: DataSource, fields: any): DataSource;
233
+ /**
234
+ * Groups an array of objects by a specified key path.
235
+ * @public
236
+ * @example
237
+ * const users = [
238
+ * { name: 'Alice', age: 25, city: 'New York' },
239
+ * { name: 'Bob', age: 30, city: 'San Francisco' },
240
+ * { name: 'Charlie', age: 25, city: 'New York' },
241
+ * { name: 'David', age: 30, city: 'San Francisco' }
242
+ * ];
243
+ * group(users, 'city');
244
+ * // state is { data: { 'New York': [/Alice, Charlie/], 'San Francisco': [ /Bob, David / ] }
245
+ * @function
246
+ * @param {Object[]} arrayOfObjects - The array of objects to be grouped.
247
+ * @param {string} keyPath - The key path to group by.
248
+ * @param {function} callback - (Optional) Callback function
249
+ * @returns {Operation}
250
+ */
251
+ export function group(arrayOfObjects: any[], keyPath: string, callback?: Function): Operation;
222
252
  /**
223
253
  * Returns the index of the current array being iterated.
224
254
  * To be used with `each` as a data source.