@iwsio/json-csv-core
Advanced tools
Comparing version
@@ -6,9 +6,9 @@ import { ExportOptions, FieldList } from './types.js'; | ||
*/ | ||
export declare function buffered(data: Record<string, any>[], opts: Partial<ExportOptions>): string; | ||
export declare function prepValue(text: string, forceQuoted: boolean, fieldSeparator: string): any; | ||
export declare function buffered(data: Record<string, unknown>[], opts: Partial<ExportOptions>): string; | ||
export declare function prepValue(text: string, forceQuoted: boolean, fieldSeparator: string): unknown; | ||
export declare function getHeaderRow(fields: FieldList, fieldSeparator: string): string; | ||
export declare function getBodyRow(data: Record<string, any> | undefined | null, fields: FieldList, fieldSeparator: string): string; | ||
export declare function getValue(data: Record<string, any>, keyPath: string): any; | ||
export declare function getValueIx(data: Record<string, any> | undefined | null, keys: string[], ix: number): any; | ||
export declare function getBodyRow(data: Record<string, unknown> | undefined | null, fields: FieldList, fieldSeparator: string): string; | ||
export declare function getValue(data: Record<string, unknown>, keyPath: string): unknown; | ||
export declare function getValueIx(data: Record<string, unknown> | undefined | null, keys: string[], ix: number): unknown; | ||
export declare const toCsv: typeof buffered; | ||
export default buffered; |
@@ -61,18 +61,17 @@ "use strict"; | ||
function getHeaderRow(fields, fieldSeparator) { | ||
var fieldKeys = Object.keys(fields); | ||
var header = fieldKeys.reduce(function (line, fieldKey) { | ||
var field = fields[fieldKey]; | ||
var header = ''; | ||
for (var ix = 0; ix < fields.length; ix++) { | ||
var field = fields[ix]; | ||
var label = field.label || field.name; | ||
if (line === 'START') { | ||
line = ''; | ||
if (ix > 0) { | ||
header += fieldSeparator; | ||
} | ||
else { | ||
line += fieldSeparator; | ||
} | ||
line += prepValue(label, field.quoted, fieldSeparator); | ||
return line; | ||
}, 'START'); | ||
header += prepValue(label, field.quoted, fieldSeparator); | ||
} | ||
header += '\r\n'; | ||
return header; | ||
} | ||
var assertString = function (value) { | ||
return typeof value === 'string'; | ||
}; | ||
function getBodyRow(data, fields, fieldSeparator) { | ||
@@ -87,3 +86,3 @@ var reducer = function (line, field) { | ||
var val = getValue(data, field.name); | ||
// vinicioslc support to OR || operator allowing multiples names to the same column | ||
// support to OR || operator allowing multiples names to the same column | ||
// the code will use the last non null and non empty value | ||
@@ -99,3 +98,3 @@ if (field.name.includes('||')) { | ||
// remove whitespaces and check if non null before assign | ||
if (val != null && fieldVal.trim().length > 0 && fieldVal.trim() !== '') { | ||
if (val != null && assertString(fieldVal) && fieldVal.trim().length > 0 && fieldVal.trim() !== '') { | ||
val = fieldVal; | ||
@@ -109,3 +108,3 @@ } | ||
} | ||
else if (typeof field.filter === 'function') { | ||
else if (typeof field.filter === 'function') { // backward compatibility | ||
val = field.filter(val); | ||
@@ -129,2 +128,7 @@ } | ||
} | ||
var assertObject = function (value) { | ||
if (typeof value === 'object') | ||
return true; | ||
return false; | ||
}; | ||
function getValueIx(data, keys, ix) { | ||
@@ -138,6 +142,6 @@ if (data == null) | ||
var val = data[keys[ix]]; | ||
if (typeof val === 'undefined') | ||
if (val == null) | ||
return ''; | ||
// walk the dot-notation recursively to get the remaining values. | ||
if ((keys.length - 1) > ix) | ||
if ((keys.length - 1) > ix && assertObject(val)) | ||
return getValueIx(val, keys, ix + 1); | ||
@@ -144,0 +148,0 @@ return val; |
@@ -18,9 +18,19 @@ "use strict"; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importStar = (this && this.__importStar) || (function () { | ||
var ownKeys = function(o) { | ||
ownKeys = Object.getOwnPropertyNames || function (o) { | ||
var ar = []; | ||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; | ||
return ar; | ||
}; | ||
return ownKeys(o); | ||
}; | ||
return function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,0 +37,0 @@ exports.toCsv = exports.buffered = exports.exporter = void 0; |
export type Field = { | ||
name: string; | ||
label?: string; | ||
transform?: (source: any) => string; | ||
transform?: (source: unknown) => string; | ||
/** | ||
* @deprecated Please use `transform` instead | ||
* @param source unknown value to be transformed | ||
* @returns string transformed value | ||
*/ | ||
filter?: (source: unknown) => string; | ||
quoted?: boolean; | ||
}; | ||
@@ -6,0 +13,0 @@ export type FieldList = Field[]; |
{ | ||
"name": "@iwsio/json-csv-core", | ||
"version": "1.1.7", | ||
"version": "1.2.0", | ||
"description": "Easily convert JSON to CSV. This is a core library built for browser and node to support buffered conversion to CSV.", | ||
@@ -44,7 +44,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/chai": "^4.3.17", | ||
"@types/chai": "^4.3.20", | ||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^6.0.1", | ||
"typescript": "^5.5.4" | ||
"typescript": "^5.7.2" | ||
} | ||
} |
13304
5.59%223
10.4%