@openfn/language-common 1.7.7 → 1.9.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/dist/index.cjs CHANGED
@@ -50,6 +50,7 @@ __export(src_exports, {
50
50
  lastReferenceValue: () => lastReferenceValue,
51
51
  map: () => map,
52
52
  merge: () => merge,
53
+ parseCsv: () => parseCsv,
53
54
  referencePath: () => referencePath,
54
55
  scrubEmojis: () => scrubEmojis,
55
56
  source: () => source,
@@ -86,6 +87,7 @@ __export(Adaptor_exports, {
86
87
  lastReferenceValue: () => lastReferenceValue,
87
88
  map: () => map,
88
89
  merge: () => merge,
90
+ parseCsv: () => parseCsv,
89
91
  referencePath: () => referencePath,
90
92
  scrubEmojis: () => scrubEmojis,
91
93
  source: () => source,
@@ -96,6 +98,7 @@ __export(Adaptor_exports, {
96
98
  var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
97
99
  var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
98
100
  var import_jsonpath_plus = require("jsonpath-plus");
101
+ var import_csv_parse = require("csv-parse");
99
102
 
100
103
  // src/beta.js
101
104
  var beta_exports = {};
@@ -147,6 +150,7 @@ function expandRequestReferences(params) {
147
150
  const [toKeep, toExpand] = splitKeys(params || {}, [
148
151
  "agentOptions",
149
152
  "body",
153
+ "json",
150
154
  "data",
151
155
  "form",
152
156
  "formData",
@@ -410,10 +414,10 @@ function scrubEmojis(text, replacementChars) {
410
414
  "It's better to replace than remove.",
411
415
  "See https://www.unicode.org/reports/tr36/#Deletion_of_Noncharacters"
412
416
  );
413
- } else if (!replacementChars)
414
- replacementChars = "\uFFFD";
417
+ }
418
+ const newChars = replacementChars || replacementChars == "" ? replacementChars : "\uFFFD";
415
419
  const emojisPattern = /(\uFE0F|\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;
416
- return text.replace(emojisPattern, replacementChars);
420
+ return text.replace(emojisPattern, newChars);
417
421
  }
418
422
  function chunk(array, chunkSize) {
419
423
  const output = [];
@@ -421,6 +425,56 @@ function chunk(array, chunkSize) {
421
425
  output.push(array.slice(i, i + chunkSize));
422
426
  return output;
423
427
  }
428
+ function parseCsv(csvData, parsingOptions = {}, callback) {
429
+ const defaultOptions = {
430
+ delimiter: ",",
431
+ quote: '"',
432
+ escape: '"',
433
+ columns: true,
434
+ bom: true,
435
+ trim: true,
436
+ ltrim: true,
437
+ rtrim: true,
438
+ chunkSize: Infinity,
439
+ skip_empty_lines: true
440
+ };
441
+ const filteredOptions = Object.fromEntries(
442
+ Object.entries(parsingOptions).filter(([key]) => key in defaultOptions)
443
+ );
444
+ const options2 = { ...defaultOptions, ...filteredOptions };
445
+ if (options2.chunkSize < 1) {
446
+ throw new Error("chunkSize must be at least 1");
447
+ }
448
+ return (state) => {
449
+ return new Promise((resolve, reject) => {
450
+ let buffer = [];
451
+ const parser = typeof csvData === "string" ? (0, import_csv_parse.parse)(csvData, options2) : csvData.pipe((0, import_csv_parse.parse)(options2));
452
+ const flushBuffer = (currentState) => {
453
+ const nextState = callback ? callback(currentState, buffer) : composeNextState(currentState, buffer);
454
+ buffer = [];
455
+ return [nextState, buffer];
456
+ };
457
+ parser.on("readable", function() {
458
+ let chunk2;
459
+ while ((chunk2 = parser.read()) !== null) {
460
+ buffer.push(chunk2);
461
+ if (buffer.length >= options2.chunkSize) {
462
+ const [nextState, nextBuffer] = flushBuffer(state);
463
+ state = nextState;
464
+ buffer = nextBuffer;
465
+ }
466
+ }
467
+ });
468
+ parser.on("error", function(error) {
469
+ reject(error);
470
+ });
471
+ parser.on("end", function() {
472
+ const [finalState] = flushBuffer(state);
473
+ resolve(finalState);
474
+ });
475
+ });
476
+ };
477
+ }
424
478
 
425
479
  // src/index.js
426
480
  var src_default = Adaptor_exports;
@@ -450,6 +504,7 @@ var src_default = Adaptor_exports;
450
504
  lastReferenceValue,
451
505
  map,
452
506
  merge,
507
+ parseCsv,
453
508
  referencePath,
454
509
  scrubEmojis,
455
510
  source,
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ __export(Adaptor_exports, {
31
31
  lastReferenceValue: () => lastReferenceValue,
32
32
  map: () => map,
33
33
  merge: () => merge,
34
+ parseCsv: () => parseCsv,
34
35
  referencePath: () => referencePath,
35
36
  scrubEmojis: () => scrubEmojis,
36
37
  source: () => source,
@@ -41,6 +42,7 @@ __export(Adaptor_exports, {
41
42
  import curry from "lodash/fp/curry.js";
42
43
  import fromPairs from "lodash/fp/fromPairs.js";
43
44
  import { JSONPath } from "jsonpath-plus";
45
+ import { parse as parse2 } from "csv-parse";
44
46
 
45
47
  // src/beta.js
46
48
  var beta_exports = {};
@@ -92,6 +94,7 @@ function expandRequestReferences(params) {
92
94
  const [toKeep, toExpand] = splitKeys(params || {}, [
93
95
  "agentOptions",
94
96
  "body",
97
+ "json",
95
98
  "data",
96
99
  "form",
97
100
  "formData",
@@ -355,10 +358,10 @@ function scrubEmojis(text, replacementChars) {
355
358
  "It's better to replace than remove.",
356
359
  "See https://www.unicode.org/reports/tr36/#Deletion_of_Noncharacters"
357
360
  );
358
- } else if (!replacementChars)
359
- replacementChars = "\uFFFD";
361
+ }
362
+ const newChars = replacementChars || replacementChars == "" ? replacementChars : "\uFFFD";
360
363
  const emojisPattern = /(\uFE0F|\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;
361
- return text.replace(emojisPattern, replacementChars);
364
+ return text.replace(emojisPattern, newChars);
362
365
  }
363
366
  function chunk(array, chunkSize) {
364
367
  const output = [];
@@ -366,6 +369,56 @@ function chunk(array, chunkSize) {
366
369
  output.push(array.slice(i, i + chunkSize));
367
370
  return output;
368
371
  }
372
+ function parseCsv(csvData, parsingOptions = {}, callback) {
373
+ const defaultOptions = {
374
+ delimiter: ",",
375
+ quote: '"',
376
+ escape: '"',
377
+ columns: true,
378
+ bom: true,
379
+ trim: true,
380
+ ltrim: true,
381
+ rtrim: true,
382
+ chunkSize: Infinity,
383
+ skip_empty_lines: true
384
+ };
385
+ const filteredOptions = Object.fromEntries(
386
+ Object.entries(parsingOptions).filter(([key]) => key in defaultOptions)
387
+ );
388
+ const options2 = { ...defaultOptions, ...filteredOptions };
389
+ if (options2.chunkSize < 1) {
390
+ throw new Error("chunkSize must be at least 1");
391
+ }
392
+ return (state) => {
393
+ return new Promise((resolve, reject) => {
394
+ let buffer = [];
395
+ const parser = typeof csvData === "string" ? parse2(csvData, options2) : csvData.pipe(parse2(options2));
396
+ const flushBuffer = (currentState) => {
397
+ const nextState = callback ? callback(currentState, buffer) : composeNextState(currentState, buffer);
398
+ buffer = [];
399
+ return [nextState, buffer];
400
+ };
401
+ parser.on("readable", function() {
402
+ let chunk2;
403
+ while ((chunk2 = parser.read()) !== null) {
404
+ buffer.push(chunk2);
405
+ if (buffer.length >= options2.chunkSize) {
406
+ const [nextState, nextBuffer] = flushBuffer(state);
407
+ state = nextState;
408
+ buffer = nextBuffer;
409
+ }
410
+ }
411
+ });
412
+ parser.on("error", function(error) {
413
+ reject(error);
414
+ });
415
+ parser.on("end", function() {
416
+ const [finalState] = flushBuffer(state);
417
+ resolve(finalState);
418
+ });
419
+ });
420
+ };
421
+ }
369
422
 
370
423
  // src/index.js
371
424
  var src_default = Adaptor_exports;
@@ -395,6 +448,7 @@ export {
395
448
  lastReferenceValue,
396
449
  map,
397
450
  merge,
451
+ parseCsv,
398
452
  referencePath,
399
453
  scrubEmojis,
400
454
  source,
package/dist/metadata.cjs CHANGED
@@ -23,7 +23,7 @@ __export(metadata_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(metadata_exports);
25
25
  var createEntity = (name, type, props = {}, children) => {
26
- const { label, datatype, value, ...meta } = props;
26
+ const { label, datatype, ...meta } = props;
27
27
  const e = {
28
28
  name,
29
29
  type,
package/dist/metadata.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/metadata.js
2
2
  var createEntity = (name, type, props = {}, children) => {
3
- const { label, datatype, value, ...meta } = props;
3
+ const { label, datatype, ...meta } = props;
4
4
  const e = {
5
5
  name,
6
6
  type,
package/dist/util.cjs ADDED
@@ -0,0 +1,45 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/util.js
20
+ var util_exports = {};
21
+ __export(util_exports, {
22
+ expandReferences: () => expandReferences
23
+ });
24
+ module.exports = __toCommonJS(util_exports);
25
+ function expandReferences(state, ...args) {
26
+ return args.map((value) => expandReference(state, value));
27
+ }
28
+ function expandReference(state, value) {
29
+ if (Array.isArray(value)) {
30
+ return value.map((v) => expandReference(state, v));
31
+ }
32
+ if (typeof value == "object" && !!value) {
33
+ return Object.keys(value).reduce((acc, key) => {
34
+ return { ...acc, [key]: expandReference(state, value[key]) };
35
+ }, {});
36
+ }
37
+ if (typeof value == "function") {
38
+ return expandReference(state, value(state));
39
+ }
40
+ return value;
41
+ }
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ expandReferences
45
+ });
package/dist/util.js ADDED
@@ -0,0 +1,21 @@
1
+ // src/util.js
2
+ function expandReferences(state, ...args) {
3
+ return args.map((value) => expandReference(state, value));
4
+ }
5
+ function expandReference(state, value) {
6
+ if (Array.isArray(value)) {
7
+ return value.map((v) => expandReference(state, v));
8
+ }
9
+ if (typeof value == "object" && !!value) {
10
+ return Object.keys(value).reduce((acc, key) => {
11
+ return { ...acc, [key]: expandReference(state, value[key]) };
12
+ }, {});
13
+ }
14
+ if (typeof value == "function") {
15
+ return expandReference(state, value(state));
16
+ }
17
+ return value;
18
+ }
19
+ export {
20
+ expandReferences
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.7.7",
3
+ "version": "1.9.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -16,6 +16,10 @@
16
16
  "./metadata": {
17
17
  "import": "./dist/metadata.js",
18
18
  "require": "./dist/metadata.cjs"
19
+ },
20
+ "./util": {
21
+ "import": "./dist/util.js",
22
+ "require": "./dist/util.cjs"
19
23
  }
20
24
  },
21
25
  "author": "Open Function Group",
@@ -30,6 +34,8 @@
30
34
  "types": "types/index.d.ts",
31
35
  "dependencies": {
32
36
  "axios": "1.1.3",
37
+ "csv-parse": "^5.4.0",
38
+ "csvtojson": "^2.0.10",
33
39
  "date-fns": "^2.25.0",
34
40
  "jsonpath-plus": "^4.0.0",
35
41
  "lodash": "^4.17.19"
@@ -50,6 +56,7 @@
50
56
  "test": "mocha --experimental-specifier-resolution=node --no-warnings",
51
57
  "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
52
58
  "clean": "rimraf dist types docs",
53
- "pack": "pnpm pack --pack-destination ../../dist"
59
+ "pack": "pnpm pack --pack-destination ../../dist",
60
+ "lint": "eslint src"
54
61
  }
55
62
  }
@@ -299,6 +299,26 @@ export function scrubEmojis(text: string, replacementChars: string): string;
299
299
  * @returns {Object}
300
300
  */
301
301
  export function chunk(array: any, chunkSize: Integer): any;
302
+ /**
303
+ * The function `parseCsv` takes a CSV file string or stream and parsing options as input, and returns a promise that
304
+ * resolves to the parsed CSV data.
305
+ * @param {String | Stream} csvData - A CSV string or a readable stream
306
+ * @param {Object} [parsingOptions] - Optional. Parsing options for converting CSV to JSON.
307
+ * Possible options:
308
+ * - `delimiter` {string|Buffer|[string|Buffer]} - Defines the character(s) used to delimitate the fields inside a record. Default: `','`
309
+ * - `quote` {string|Buffer|[string|Buffer]} - Defines the characters used to surround a field. Default: `'"'`
310
+ * - `escape` {Buffer|string|null|boolean} - Set the escape character as one character/byte only. Default: `"`
311
+ * - `columns` {boolean | array | function} - Generates record in the form of object literals. Default: `true`
312
+ * - `bom` {boolean} - Strips the {@link https://en.wikipedia.org/wiki/Byte_order_mark byte order mark (BOM)} from the input string or buffer. Default: `true`
313
+ * - `trim` {boolean} - Ignore whitespace characters immediately around the `delimiter`. Default: `true`
314
+ * - `ltrim` {boolean} - Ignore whitespace characters from the left side of a CSV field. Default: `true`
315
+ * - `rtrim` {boolean} - Ignore whitespace characters from the right side of a CSV field. Default: `true`
316
+ * - `chunkSize` {number} - The size of each chunk of CSV data. Default: `Infinity`
317
+ * - `skip_empty_lines` {boolean} - The `skip_empty_lines` skips any line which is empty. Default: `true`
318
+ * @param {function} callback - (Optional) callback function. If used it will take state and csvRows
319
+ * @returns {Promise} The function returns a Promise that resolves to the result of parsing a CSV `stringOrStream`.
320
+ */
321
+ export function parseCsv(csvData: string | Stream, parsingOptions?: any, callback: Function): Promise<any>;
302
322
  export * as beta from "./beta";
303
323
  export * as http from "./http";
304
324
  export * as dateFns from "./dateFns";