@openfn/language-common 1.13.5 → 1.14.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": [
@@ -1319,7 +1372,7 @@
1319
1372
  "options"
1320
1373
  ],
1321
1374
  "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}",
1375
+ "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
1376
  "tags": [
1324
1377
  {
1325
1378
  "title": "public",
package/dist/index.cjs CHANGED
@@ -43,6 +43,7 @@ __export(src_exports, {
43
43
  field: () => field,
44
44
  fields: () => fields,
45
45
  fn: () => fn,
46
+ fnIf: () => fnIf,
46
47
  http: () => http_deprecated_exports,
47
48
  humanProper: () => humanProper,
48
49
  index: () => index,
@@ -82,6 +83,7 @@ __export(Adaptor_exports, {
82
83
  field: () => field,
83
84
  fields: () => fields,
84
85
  fn: () => fn,
86
+ fnIf: () => fnIf,
85
87
  http: () => http_deprecated_exports,
86
88
  humanProper: () => humanProper,
87
89
  index: () => index,
@@ -333,6 +335,12 @@ function fn(func) {
333
335
  return func(state);
334
336
  };
335
337
  }
338
+ function fnIf(condition, operation) {
339
+ return (state) => {
340
+ const [resolvedCondition] = expandReferences(state, condition);
341
+ return resolvedCondition ? operation(state) : state;
342
+ };
343
+ }
336
344
  function jsonValue(obj, path) {
337
345
  return (0, import_jsonpath_plus.JSONPath)({ path, json: obj })[0];
338
346
  }
@@ -647,7 +655,11 @@ var cursorKey = "cursor";
647
655
  function cursor(value, options2 = {}) {
648
656
  return (state) => {
649
657
  const { format: format2, ...optionsWithoutFormat } = options2;
650
- const [resolvedValue, resolvedOptions] = expandReferences(state, value, optionsWithoutFormat);
658
+ const [resolvedValue, resolvedOptions] = expandReferences(
659
+ state,
660
+ value,
661
+ optionsWithoutFormat
662
+ );
651
663
  const {
652
664
  defaultValue,
653
665
  key
@@ -695,6 +707,7 @@ var src_default = Adaptor_exports;
695
707
  field,
696
708
  fields,
697
709
  fn,
710
+ fnIf,
698
711
  http,
699
712
  humanProper,
700
713
  index,
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ __export(Adaptor_exports, {
24
24
  field: () => field,
25
25
  fields: () => fields,
26
26
  fn: () => fn,
27
+ fnIf: () => fnIf,
27
28
  http: () => http_deprecated_exports,
28
29
  humanProper: () => humanProper,
29
30
  index: () => index,
@@ -273,6 +274,12 @@ function fn(func) {
273
274
  return func(state);
274
275
  };
275
276
  }
277
+ function fnIf(condition, operation) {
278
+ return (state) => {
279
+ const [resolvedCondition] = expandReferences(state, condition);
280
+ return resolvedCondition ? operation(state) : state;
281
+ };
282
+ }
276
283
  function jsonValue(obj, path) {
277
284
  return JSONPath({ path, json: obj })[0];
278
285
  }
@@ -587,7 +594,11 @@ var cursorKey = "cursor";
587
594
  function cursor(value, options2 = {}) {
588
595
  return (state) => {
589
596
  const { format: format2, ...optionsWithoutFormat } = options2;
590
- const [resolvedValue, resolvedOptions] = expandReferences(state, value, optionsWithoutFormat);
597
+ const [resolvedValue, resolvedOptions] = expandReferences(
598
+ state,
599
+ value,
600
+ optionsWithoutFormat
601
+ );
591
602
  const {
592
603
  defaultValue,
593
604
  key
@@ -635,6 +646,7 @@ export {
635
646
  field,
636
647
  fields,
637
648
  fn,
649
+ fnIf,
638
650
  http_deprecated_exports as http,
639
651
  humanProper,
640
652
  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.14.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