@punks/backend-core 0.0.18 → 0.0.20

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.
@@ -1,4 +1,5 @@
1
- export declare const csvParseRaw: (data: string | string[] | Blob, separator: string) => Promise<string[][]>;
1
+ /// <reference types="node" />
2
+ export declare const csvParseRaw: (data: string | string[] | Buffer, separator?: string) => Record<string, any>[];
2
3
  export interface CsvColumnDefinition<T> {
3
4
  name: string;
4
5
  value: (item: T) => any;
package/dist/esm/index.js CHANGED
@@ -201,24 +201,32 @@ const iterate = (values, action) => {
201
201
  };
202
202
 
203
203
  const DEFAULT_DELIMITER = ";";
204
- const splitRows = async (data) => {
204
+ const splitRows = (data) => {
205
205
  if (typeof data === "string") {
206
206
  return data.split("\n");
207
207
  }
208
- if (data instanceof Blob) {
209
- const text = await data.text();
210
- return await splitRows(text);
208
+ if (data instanceof Buffer) {
209
+ const text = data.toString("utf-8");
210
+ return splitRows(text);
211
211
  }
212
212
  return data;
213
213
  };
214
- const csvParseRaw = async (data, separator) => {
215
- const rows = await splitRows(data);
214
+ const csvParseRaw = (data, separator = DEFAULT_DELIMITER) => {
215
+ const rows = splitRows(data);
216
216
  const records = [];
217
- for (const row of rows) {
218
- const record = row.split(separator);
219
- if (record.length > 0) {
220
- records.push(record);
217
+ if (rows.length === 0) {
218
+ return records;
219
+ }
220
+ const headers = rows[0].split(separator);
221
+ for (let i = 1; i < rows.length; i++) {
222
+ const values = rows[i].split(separator);
223
+ const record = {};
224
+ for (let j = 0; j < headers.length; j++) {
225
+ const header = headers[j];
226
+ const value = values[j];
227
+ record[header] = value;
221
228
  }
229
+ records.push(record);
222
230
  }
223
231
  return records;
224
232
  };