@openfn/language-common 1.14.0 → 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
@@ -894,6 +894,78 @@
894
894
  },
895
895
  "valid": true
896
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
+ },
897
969
  {
898
970
  "name": "index",
899
971
  "params": [],
package/dist/index.cjs CHANGED
@@ -44,6 +44,7 @@ __export(src_exports, {
44
44
  fields: () => fields,
45
45
  fn: () => fn,
46
46
  fnIf: () => fnIf,
47
+ group: () => group,
47
48
  http: () => http_deprecated_exports,
48
49
  humanProper: () => humanProper,
49
50
  index: () => index,
@@ -84,6 +85,7 @@ __export(Adaptor_exports, {
84
85
  fields: () => fields,
85
86
  fn: () => fn,
86
87
  fnIf: () => fnIf,
88
+ group: () => group,
87
89
  http: () => http_deprecated_exports,
88
90
  humanProper: () => humanProper,
89
91
  index: () => index,
@@ -101,7 +103,10 @@ __export(Adaptor_exports, {
101
103
  toArray: () => toArray,
102
104
  validate: () => validate
103
105
  });
106
+ var import_get = __toESM(require("lodash/get.js"), 1);
107
+ var import_omit = __toESM(require("lodash/omit.js"), 1);
104
108
  var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
109
+ var import_groupBy = __toESM(require("lodash/groupBy.js"), 1);
105
110
  var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
106
111
  var import_jsonpath_plus = require("jsonpath-plus");
107
112
  var import_csv_parse = require("csv-parse");
@@ -462,6 +467,17 @@ function merge(dataSource, fields2) {
462
467
  }, []);
463
468
  };
464
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
+ }
465
481
  function index() {
466
482
  return (state) => {
467
483
  return state.index;
@@ -474,6 +490,9 @@ function toArray(arg) {
474
490
  return new Array().concat(arg);
475
491
  }
476
492
  function composeNextState(state, response) {
493
+ if (!state.references) {
494
+ state.references = [];
495
+ }
477
496
  return {
478
497
  ...state,
479
498
  data: response,
@@ -708,6 +727,7 @@ var src_default = Adaptor_exports;
708
727
  fields,
709
728
  fn,
710
729
  fnIf,
730
+ group,
711
731
  http,
712
732
  humanProper,
713
733
  index,
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ __export(Adaptor_exports, {
25
25
  fields: () => fields,
26
26
  fn: () => fn,
27
27
  fnIf: () => fnIf,
28
+ group: () => group,
28
29
  http: () => http_deprecated_exports,
29
30
  humanProper: () => humanProper,
30
31
  index: () => index,
@@ -42,7 +43,10 @@ __export(Adaptor_exports, {
42
43
  toArray: () => toArray,
43
44
  validate: () => validate
44
45
  });
46
+ import get2 from "lodash/get.js";
47
+ import omit from "lodash/omit.js";
45
48
  import curry from "lodash/fp/curry.js";
49
+ import groupBy from "lodash/groupBy.js";
46
50
  import fromPairs from "lodash/fp/fromPairs.js";
47
51
  import { JSONPath } from "jsonpath-plus";
48
52
  import { parse as parse2 } from "csv-parse";
@@ -401,6 +405,17 @@ function merge(dataSource, fields2) {
401
405
  }, []);
402
406
  };
403
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
+ }
404
419
  function index() {
405
420
  return (state) => {
406
421
  return state.index;
@@ -413,6 +428,9 @@ function toArray(arg) {
413
428
  return new Array().concat(arg);
414
429
  }
415
430
  function composeNextState(state, response) {
431
+ if (!state.references) {
432
+ state.references = [];
433
+ }
416
434
  return {
417
435
  ...state,
418
436
  data: response,
@@ -647,6 +665,7 @@ export {
647
665
  fields,
648
666
  fn,
649
667
  fnIf,
668
+ group,
650
669
  http_deprecated_exports as http,
651
670
  humanProper,
652
671
  index,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -230,6 +230,25 @@ export function fields(...fields: Fields): any;
230
230
  * @returns {DataSource}
231
231
  */
232
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;
233
252
  /**
234
253
  * Returns the index of the current array being iterated.
235
254
  * To be used with `each` as a data source.