@openfn/language-common 2.2.1 → 2.3.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
@@ -1544,6 +1544,59 @@
1544
1544
  },
1545
1545
  "valid": false
1546
1546
  },
1547
+ {
1548
+ "name": "assert",
1549
+ "params": [
1550
+ "expression",
1551
+ "errorMessage"
1552
+ ],
1553
+ "docs": {
1554
+ "description": "Asserts the given expression or function resolves to `true`, or else throws an exception. Optionally accepts and error message.",
1555
+ "tags": [
1556
+ {
1557
+ "title": "public",
1558
+ "description": null,
1559
+ "type": null
1560
+ },
1561
+ {
1562
+ "title": "function",
1563
+ "description": null,
1564
+ "name": null
1565
+ },
1566
+ {
1567
+ "title": "example",
1568
+ "description": "assert('a' === 'b', '\"a\" is not equal to \"b\"')"
1569
+ },
1570
+ {
1571
+ "title": "param",
1572
+ "description": "The expression or function to be evaluated.",
1573
+ "type": {
1574
+ "type": "NameExpression",
1575
+ "name": "any"
1576
+ },
1577
+ "name": "expression"
1578
+ },
1579
+ {
1580
+ "title": "param",
1581
+ "description": "The error message thrown in case of a failed state.",
1582
+ "type": {
1583
+ "type": "NameExpression",
1584
+ "name": "string"
1585
+ },
1586
+ "name": "errorMessage"
1587
+ },
1588
+ {
1589
+ "title": "returns",
1590
+ "description": null,
1591
+ "type": {
1592
+ "type": "NameExpression",
1593
+ "name": "operation"
1594
+ }
1595
+ }
1596
+ ]
1597
+ },
1598
+ "valid": true
1599
+ },
1547
1600
  {
1548
1601
  "name": "map",
1549
1602
  "params": [
package/dist/index.cjs CHANGED
@@ -28,6 +28,7 @@ __export(src_exports, {
28
28
  alterState: () => alterState,
29
29
  arrayToString: () => arrayToString,
30
30
  asData: () => asData,
31
+ assert: () => assert,
31
32
  beta: () => beta_exports,
32
33
  chunk: () => chunk,
33
34
  combine: () => combine,
@@ -71,6 +72,7 @@ __export(Adaptor_exports, {
71
72
  alterState: () => alterState,
72
73
  arrayToString: () => arrayToString,
73
74
  asData: () => asData,
75
+ assert: () => assert,
74
76
  chunk: () => chunk,
75
77
  combine: () => combine,
76
78
  composeNextState: () => composeNextState,
@@ -369,7 +371,8 @@ var isStream = (value) => {
369
371
  return false;
370
372
  };
371
373
  function expandReference(state, value) {
372
- if (Buffer.isBuffer(value)) {
374
+ var _a;
375
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
373
376
  return value;
374
377
  }
375
378
  if (Array.isArray(value)) {
@@ -429,8 +432,29 @@ var parse_date_default = (d, startDate) => {
429
432
  };
430
433
 
431
434
  // src/util/base64.js
432
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
433
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
435
+ var import_lodash = __toESM(require("lodash"), 1);
436
+ var encode = (data, options2 = { parseJson: true }) => {
437
+ let str = data;
438
+ if (typeof data !== "string" && options2.parseJson) {
439
+ try {
440
+ str = JSON.stringify(str);
441
+ } catch (e) {
442
+ console.log(e.message);
443
+ }
444
+ }
445
+ return Buffer.from(str, "utf-8").toString("base64");
446
+ };
447
+ var decode = (base64Data, options2 = { parseJson: true }) => {
448
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
449
+ if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options2.parseJson) {
450
+ try {
451
+ decodedValue = JSON.parse(decodedValue);
452
+ } catch (e) {
453
+ console.log(e.message);
454
+ }
455
+ }
456
+ return decodedValue;
457
+ };
434
458
 
435
459
  // src/util/uuid.js
436
460
  var import_node_crypto = require("crypto");
@@ -818,6 +842,21 @@ function cursor(value, options2 = {}) {
818
842
  return state;
819
843
  };
820
844
  }
845
+ function assert(expression, errorMessage) {
846
+ return (state) => {
847
+ const [resolvedValue, resolvedErrorMessage] = expandReferences(
848
+ state,
849
+ expression,
850
+ errorMessage
851
+ );
852
+ if (!resolvedValue) {
853
+ throw new Error(
854
+ resolvedErrorMessage || `assertion statement failed with ${resolvedValue}`
855
+ );
856
+ }
857
+ return state;
858
+ };
859
+ }
821
860
 
822
861
  // src/beta.js
823
862
  var beta_exports = {};
@@ -930,6 +969,7 @@ var src_default = Adaptor_exports;
930
969
  alterState,
931
970
  arrayToString,
932
971
  asData,
972
+ assert,
933
973
  beta,
934
974
  chunk,
935
975
  combine,
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ __export(Adaptor_exports, {
10
10
  alterState: () => alterState,
11
11
  arrayToString: () => arrayToString,
12
12
  asData: () => asData,
13
+ assert: () => assert,
13
14
  chunk: () => chunk,
14
15
  combine: () => combine,
15
16
  composeNextState: () => composeNextState,
@@ -308,7 +309,8 @@ var isStream = (value) => {
308
309
  return false;
309
310
  };
310
311
  function expandReference(state, value) {
311
- if (Buffer.isBuffer(value)) {
312
+ var _a;
313
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
312
314
  return value;
313
315
  }
314
316
  if (Array.isArray(value)) {
@@ -368,8 +370,29 @@ var parse_date_default = (d, startDate) => {
368
370
  };
369
371
 
370
372
  // src/util/base64.js
371
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
372
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
373
+ import _ from "lodash";
374
+ var encode = (data, options2 = { parseJson: true }) => {
375
+ let str = data;
376
+ if (typeof data !== "string" && options2.parseJson) {
377
+ try {
378
+ str = JSON.stringify(str);
379
+ } catch (e) {
380
+ console.log(e.message);
381
+ }
382
+ }
383
+ return Buffer.from(str, "utf-8").toString("base64");
384
+ };
385
+ var decode = (base64Data, options2 = { parseJson: true }) => {
386
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
387
+ if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options2.parseJson) {
388
+ try {
389
+ decodedValue = JSON.parse(decodedValue);
390
+ } catch (e) {
391
+ console.log(e.message);
392
+ }
393
+ }
394
+ return decodedValue;
395
+ };
373
396
 
374
397
  // src/util/uuid.js
375
398
  import { randomUUID } from "crypto";
@@ -757,6 +780,21 @@ function cursor(value, options2 = {}) {
757
780
  return state;
758
781
  };
759
782
  }
783
+ function assert(expression, errorMessage) {
784
+ return (state) => {
785
+ const [resolvedValue, resolvedErrorMessage] = expandReferences(
786
+ state,
787
+ expression,
788
+ errorMessage
789
+ );
790
+ if (!resolvedValue) {
791
+ throw new Error(
792
+ resolvedErrorMessage || `assertion statement failed with ${resolvedValue}`
793
+ );
794
+ }
795
+ return state;
796
+ };
797
+ }
760
798
 
761
799
  // src/beta.js
762
800
  var beta_exports = {};
@@ -871,6 +909,7 @@ export {
871
909
  alterState,
872
910
  arrayToString,
873
911
  asData,
912
+ assert,
874
913
  beta_exports as beta,
875
914
  chunk,
876
915
  combine,
package/dist/util.cjs CHANGED
@@ -280,7 +280,8 @@ var isStream = (value) => {
280
280
  return false;
281
281
  };
282
282
  function expandReference(state, value) {
283
- if (Buffer.isBuffer(value)) {
283
+ var _a;
284
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
284
285
  return value;
285
286
  }
286
287
  if (Array.isArray(value)) {
@@ -340,8 +341,29 @@ var parse_date_default = (d, startDate) => {
340
341
  };
341
342
 
342
343
  // src/util/base64.js
343
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
344
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
344
+ var import_lodash = __toESM(require("lodash"), 1);
345
+ var encode = (data, options = { parseJson: true }) => {
346
+ let str = data;
347
+ if (typeof data !== "string" && options.parseJson) {
348
+ try {
349
+ str = JSON.stringify(str);
350
+ } catch (e) {
351
+ console.log(e.message);
352
+ }
353
+ }
354
+ return Buffer.from(str, "utf-8").toString("base64");
355
+ };
356
+ var decode = (base64Data, options = { parseJson: true }) => {
357
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
358
+ if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options.parseJson) {
359
+ try {
360
+ decodedValue = JSON.parse(decodedValue);
361
+ } catch (e) {
362
+ console.log(e.message);
363
+ }
364
+ }
365
+ return decodedValue;
366
+ };
345
367
 
346
368
  // src/util/uuid.js
347
369
  var import_node_crypto = require("crypto");
package/dist/util.js CHANGED
@@ -231,7 +231,8 @@ var isStream = (value) => {
231
231
  return false;
232
232
  };
233
233
  function expandReference(state, value) {
234
- if (Buffer.isBuffer(value)) {
234
+ var _a;
235
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
235
236
  return value;
236
237
  }
237
238
  if (Array.isArray(value)) {
@@ -291,8 +292,29 @@ var parse_date_default = (d, startDate) => {
291
292
  };
292
293
 
293
294
  // src/util/base64.js
294
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
295
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
295
+ import _ from "lodash";
296
+ var encode = (data, options = { parseJson: true }) => {
297
+ let str = data;
298
+ if (typeof data !== "string" && options.parseJson) {
299
+ try {
300
+ str = JSON.stringify(str);
301
+ } catch (e) {
302
+ console.log(e.message);
303
+ }
304
+ }
305
+ return Buffer.from(str, "utf-8").toString("base64");
306
+ };
307
+ var decode = (base64Data, options = { parseJson: true }) => {
308
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
309
+ if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options.parseJson) {
310
+ try {
311
+ decodedValue = JSON.parse(decodedValue);
312
+ } catch (e) {
313
+ console.log(e.message);
314
+ }
315
+ }
316
+ return decodedValue;
317
+ };
296
318
 
297
319
  // src/util/uuid.js
298
320
  import { randomUUID } from "crypto";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -423,6 +423,17 @@ export function cursor(value: any, options?: {
423
423
  defaultValue: any;
424
424
  format: Function;
425
425
  }): Operation;
426
+ /**
427
+ * Asserts the given expression or function resolves to `true`, or else throws an exception. Optionally accepts and error message.
428
+ * @public
429
+ * @function
430
+ * @example
431
+ * assert('a' === 'b', '"a" is not equal to "b"')
432
+ * @param {any} expression - The expression or function to be evaluated.
433
+ * @param {string} errorMessage - The error message thrown in case of a failed state.
434
+ * @returns {operation}
435
+ */
436
+ export function assert(expression: any, errorMessage: string): operation;
426
437
  /**
427
438
  * Scopes an array of data based on a JSONPath.
428
439
  * Useful when the source data has `n` items you would like to map to
@@ -1,2 +1,2 @@
1
- export function encode(data: string): string;
2
- export function decode(base64Data: string): string;
1
+ export function encode(data: string | object, options?: object): string;
2
+ export function decode(base64Data: string, options?: object): string | object;