@openfn/language-common 1.8.1 → 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 = {};
@@ -422,6 +425,56 @@ function chunk(array, chunkSize) {
422
425
  output.push(array.slice(i, i + chunkSize));
423
426
  return output;
424
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
+ }
425
478
 
426
479
  // src/index.js
427
480
  var src_default = Adaptor_exports;
@@ -451,6 +504,7 @@ var src_default = Adaptor_exports;
451
504
  lastReferenceValue,
452
505
  map,
453
506
  merge,
507
+ parseCsv,
454
508
  referencePath,
455
509
  scrubEmojis,
456
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 = {};
@@ -367,6 +369,56 @@ function chunk(array, chunkSize) {
367
369
  output.push(array.slice(i, i + chunkSize));
368
370
  return output;
369
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
+ }
370
422
 
371
423
  // src/index.js
372
424
  var src_default = Adaptor_exports;
@@ -396,6 +448,7 @@ export {
396
448
  lastReferenceValue,
397
449
  map,
398
450
  merge,
451
+ parseCsv,
399
452
  referencePath,
400
453
  scrubEmojis,
401
454
  source,
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.8.1",
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"
@@ -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";