Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@iwsio/json-csv-core

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iwsio/json-csv-core - npm Package Compare versions

Comparing version 0.2.13 to 0.2.14-alpha.1

dist/types.js

29

dist/exporter.d.ts

@@ -1,24 +0,13 @@

/** @type { import("./types").CheckOptions } */
export function checkOptions(opts: import("./types").ExportOptionsParam): import("./types").ExportOptions;
import { ExportOptions, FieldList } from "./types";
export declare function checkOptions(opts?: Partial<ExportOptions>): ExportOptions;
/**
* Main entry point. Convert a buffered array of data to a CSV string.
* @type { import("./types").Buffered }
*/
export function buffered(data: Record<string, any>[], opts: import("./types").ExportOptionsParam): string;
/** @type { import("./types").PrepValue } */
export function prepValue(text: string, forceQuoted: boolean, fieldSeparator: string): any;
/** @type { import("./types").GetHeaderRow } */
export function getHeaderRow(fields: import("./types").FieldList, fieldSeparator: string): any;
/** @type { import("./types").GetBodyRow } */
export function getBodyRow(data: Record<string, any>, fields: import("./types").FieldList, fieldSeparator: string): any;
/** @type { import("./types").GetValue } */
export function getValue(data: Record<string, any>, keyPath: string): any;
/** @type { import("./types").GetValueIx } */
export function getValueIx(data: Record<string, any>, keys: string[], ix: number): any;
/**
* Main entry point. Convert a buffered array of data to a CSV string.
* @type { import("./types").Buffered }
*/
export function toCsv(data: Record<string, any>[], opts: import("./types").ExportOptionsParam): string;
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 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 const toCsv: typeof buffered;
export default buffered;
//# sourceMappingURL=exporter.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buffered = buffered;
exports.checkOptions = checkOptions;
exports.default = void 0;
exports.getBodyRow = getBodyRow;
exports.getHeaderRow = getHeaderRow;
exports.getValue = getValue;
exports.getValueIx = getValueIx;
exports.prepValue = prepValue;
exports.toCsv = void 0;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/** @type { import("./types").CheckOptions } */
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toCsv = exports.getValueIx = exports.getValue = exports.getBodyRow = exports.getHeaderRow = exports.prepValue = exports.buffered = exports.checkOptions = void 0;
function checkOptions(opts) {
var options = opts == null ? {} : _objectSpread({}, opts);
if (options.fieldSeparator == null) options.fieldSeparator = ',';
if (options.ignoreHeader == null) options.ignoreHeader = false;
if (options.buffered == null) options.buffered = true;
if (options.fields == null) options.fields = [];
if (options.encoding == null) options.encoding = 'utf8';
return options;
var options = opts == null ? {} : __assign({}, opts);
if (options.fieldSeparator == null)
options.fieldSeparator = ',';
if (options.ignoreHeader == null)
options.ignoreHeader = false;
if (options.fields == null)
options.fields = [];
return options;
}
exports.checkOptions = checkOptions;
/**
* Main entry point. Convert a buffered array of data to a CSV string.
* @type { import("./types").Buffered }
*/
function buffered(data, opts) {
var options = checkOptions(opts);
var output = '';
var writtenHeader = false;
var ignoreHeader = options.ignoreHeader,
fields = options.fields;
data.forEach(function (dataItem) {
if (!writtenHeader && !ignoreHeader) {
writtenHeader = true;
var header = getHeaderRow(fields, options.fieldSeparator);
output += header;
}
var line = getBodyRow(dataItem, fields, options.fieldSeparator);
output += line;
});
return output;
var options = checkOptions(opts);
var output = '';
var writtenHeader = false;
var ignoreHeader = options.ignoreHeader, fields = options.fields;
data.forEach(function (dataItem) {
if (!writtenHeader && !ignoreHeader) {
writtenHeader = true;
var header = getHeaderRow(fields, options.fieldSeparator);
output += header;
}
var line = getBodyRow(dataItem, fields, options.fieldSeparator);
output += line;
});
return output;
}
/** @type { import("./types").PrepValue } */
exports.buffered = buffered;
function prepValue(text, forceQuoted, fieldSeparator) {
if (text == null) text = '';
var quoted = forceQuoted || text.indexOf('"') >= 0 || text.indexOf(fieldSeparator) >= 0 || text.indexOf('\n') >= 0;
var result = text.replace(/"/g, '""');
if (quoted) {
result = '"' + result + '"';
}
return result;
if (text == null)
text = '';
var quoted = forceQuoted || text.indexOf('"') >= 0 || text.indexOf(fieldSeparator) >= 0 || text.indexOf('\n') >= 0;
var result = text.replace(/"/g, '""');
if (quoted) {
result = '"' + result + '"';
}
return result;
}
/** @type { import("./types").GetHeaderRow } */
exports.prepValue = prepValue;
function getHeaderRow(fields, fieldSeparator) {
var fieldKeys = Object.keys(fields);
var header = fieldKeys.reduce(function (line, fieldKey) {
var field = fields[fieldKey];
var label = field.label || field.name;
if (line === 'START') {
line = '';
} else {
line += fieldSeparator;
}
line += prepValue(label, field.quoted, fieldSeparator);
return line;
}, 'START');
header += '\r\n';
return header;
var fieldKeys = Object.keys(fields);
var header = fieldKeys.reduce(function (line, fieldKey) {
var field = fields[fieldKey];
var label = field.label || field.name;
if (line === 'START') {
line = '';
}
else {
line += fieldSeparator;
}
line += prepValue(label, field.quoted, fieldSeparator);
return line;
}, 'START');
header += '\r\n';
return header;
}
/** @type { import("./types").GetBodyRow } */
exports.getHeaderRow = getHeaderRow;
function getBodyRow(data, fields, fieldSeparator) {
var reducer = function reducer(line, field) {
if (line === 'START') {
line = '';
} else {
line += fieldSeparator;
}
var val = getValue(data, field.name);
// vinicioslc support to OR || operator allowing multiples names to the same column
// the code will use the last non null and non empty value
if (field.name.includes('||')) {
// by default column is empty
val = '';
var _fields = field.name.split('||');
// for each alternative
_fields.forEach(function (field) {
// get value and associate
var fieldVal = getValue(data, field);
// remove whitespaces and check if non null before assign
if (val != null && fieldVal.trim().length > 0 && fieldVal.trim() !== '') {
val = fieldVal;
var reducer = function (line, field) {
if (line === 'START') {
line = '';
}
// do this for every field
});
}
if (typeof field.transform === 'function') {
val = field.transform(val);
} else if (typeof field.filter === 'function') {
val = field.filter(val);
}
if (typeof val !== 'undefined' && val !== null) {
var quoted = typeof field.quoted !== 'undefined' && field.quoted;
line += prepValue(val.toString(), quoted, fieldSeparator);
}
return line;
};
var row = fields.reduce(reducer, 'START');
row += '\r\n';
return row;
else {
line += fieldSeparator;
}
var val = getValue(data, field.name);
// vinicioslc support to OR || operator allowing multiples names to the same column
// the code will use the last non null and non empty value
if (field.name.includes('||')) {
// by default column is empty
val = '';
var fields_1 = field.name.split('||');
// for each alternative
fields_1.forEach(function (field) {
// get value and associate
var fieldVal = getValue(data, field);
// remove whitespaces and check if non null before assign
if (val != null && fieldVal.trim().length > 0 && fieldVal.trim() !== '') {
val = fieldVal;
}
// do this for every field
});
}
if (typeof field.transform === 'function') {
val = field.transform(val);
}
else if (typeof field.filter === 'function') {
val = field.filter(val);
}
if (typeof val !== 'undefined' && val !== null) {
var quoted = typeof field.quoted !== 'undefined' && field.quoted;
line += prepValue(val.toString(), quoted, fieldSeparator);
}
return line;
};
var row = fields.reduce(reducer, 'START');
row += '\r\n';
return row;
}
/** @type { import("./types").GetValue } */
exports.getBodyRow = getBodyRow;
function getValue(data, keyPath) {
var keys = keyPath.split('.');
if (keys.length > 0) return getValueIx(data, keys, 0);
return '';
var keys = keyPath.split('.');
if (keys.length > 0)
return getValueIx(data, keys, 0);
return '';
}
/** @type { import("./types").GetValueIx } */
exports.getValue = getValue;
function getValueIx(data, keys, ix) {
if (data == null) {
return '';
}
// for filtered fields using the whole row as a source.
// `this` is a keyword here; hoping not to conflict with existing fields.
if (keys[0] === 'this') {
return data;
}
var val = data[keys[ix]];
if (typeof val === 'undefined') {
return '';
}
// walk the dot-notation recursively to get the remaining values.
if (keys.length - 1 > ix) {
return getValueIx(val, keys, ix + 1);
}
return val;
if (data == null)
return '';
// for filtered fields using the whole row as a source.
// `this` is a keyword here; hoping not to conflict with existing fields.
if (keys[0] === 'this')
return data;
var val = data[keys[ix]];
if (typeof val === 'undefined')
return '';
// walk the dot-notation recursively to get the remaining values.
if ((keys.length - 1) > ix)
return getValueIx(val, keys, ix + 1);
return val;
}
var toCsv = buffered;
exports.toCsv = toCsv;
var _default = buffered;
exports.default = _default;
//# sourceMappingURL=exporter.js.map
exports.getValueIx = getValueIx;
exports.toCsv = buffered;
exports.default = buffered;

@@ -0,11 +1,3 @@

import { buffered } from './exporter';
export { buffered, buffered as toCsv };
export default buffered;
import { buffered } from "./exporter";
import { toCsv } from "./exporter";
import { checkOptions } from "./exporter";
import { prepValue } from "./exporter";
import { getBodyRow } from "./exporter";
import { getHeaderRow } from "./exporter";
import { getValue } from "./exporter";
import { getValueIx } from "./exporter";
export { buffered, toCsv, checkOptions, prepValue, getBodyRow, getHeaderRow, getValue, getValueIx };
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "buffered", {
enumerable: true,
get: function get() {
return _exporter.buffered;
}
});
Object.defineProperty(exports, "checkOptions", {
enumerable: true,
get: function get() {
return _exporter.checkOptions;
}
});
exports.default = void 0;
Object.defineProperty(exports, "getBodyRow", {
enumerable: true,
get: function get() {
return _exporter.getBodyRow;
}
});
Object.defineProperty(exports, "getHeaderRow", {
enumerable: true,
get: function get() {
return _exporter.getHeaderRow;
}
});
Object.defineProperty(exports, "getValue", {
enumerable: true,
get: function get() {
return _exporter.getValue;
}
});
Object.defineProperty(exports, "getValueIx", {
enumerable: true,
get: function get() {
return _exporter.getValueIx;
}
});
Object.defineProperty(exports, "prepValue", {
enumerable: true,
get: function get() {
return _exporter.prepValue;
}
});
Object.defineProperty(exports, "toCsv", {
enumerable: true,
get: function get() {
return _exporter.toCsv;
}
});
var _exporter = require("./exporter");
var _default = _exporter.buffered;
exports.default = _default;
//# sourceMappingURL=index.js.map
Object.defineProperty(exports, "__esModule", { value: true });
exports.toCsv = exports.buffered = void 0;
var exporter_1 = require("./exporter");
Object.defineProperty(exports, "buffered", { enumerable: true, get: function () { return exporter_1.buffered; } });
Object.defineProperty(exports, "toCsv", { enumerable: true, get: function () { return exporter_1.buffered; } });
exports.default = exporter_1.buffered;

@@ -1,2 +0,2 @@

export declare type Field = {
export type Field = {
name: string;

@@ -6,21 +6,7 @@ label?: string;

};
export declare type SourceData = any[];
export declare type FieldList = Field[];
export declare type ExportOptionsParam = {
export type FieldList = Field[];
export type ExportOptions = {
fieldSeparator?: string;
ignoreHeader?: boolean;
fields: FieldList;
};
export declare type ExportOptions = {
fieldSeparator?: string;
fields: FieldList;
ignoreHeader: boolean;
};
export declare type Buffered = (data: Record<string, any>[], opts?: ExportOptionsParam) => string;
export declare type GetValueIx = (data: Record<string, any> | undefined | null, keys: string[], ix: number) => any;
export declare type GetValue = (data: Record<string, any>, keyPath: string) => any;
export declare type GetBodyRow = (data: Record<string, any> | undefined | null, fields: FieldList, fieldSeparator: string) => any;
export declare type GetHeaderRow = (fields: FieldList, fieldSeparator: string) => any;
export declare type PrepValue = (text: string, forceQuoted: boolean, fieldSeparator: string) => any;
export declare type CheckOptions = (opts?: ExportOptionsParam) => ExportOptions;
//# sourceMappingURL=types.d.ts.map

@@ -1,2 +0,2 @@

Copyright (c) 2022, Nathan Bridgewater / Integrated Web Systems, LLC
Copyright (c) 2023, Nathan Bridgewater / Integrated Web Systems, LLC

@@ -3,0 +3,0 @@ All rights reserved.

{
"name": "@iwsio/json-csv-core",
"version": "0.2.13",
"version": "0.2.14-alpha.1",
"description": "Easily convert JSON to CSV. This is a core library built for browser and node to support buffered conversion to CSV.",
"engines": {
"node": ">=10"
},
"main": "dist/index.js",

@@ -12,15 +15,7 @@ "types": "./dist/index.d.ts",

".": "./dist/index.js",
"./index": "./dist/index.js",
"./exporter": "./dist/exporter.js"
"./*": "./dist/*.js"
},
"scripts": {
"test": "npm-run-all test:*",
"test:js": "mocha $@",
"test:ts": "mocha --config .mocharc-ts.json $@",
"prebuild": "rimraf dist samples/dist",
"build:js": "babel src --out-dir dist --source-maps",
"build:sample": "webpack --mode production",
"build:ts": "tsc",
"build": "npm-run-all build:*",
"serve": "serve -l 3000 -s ./samples"
"prebuild": "rimraf dist && tsc --build tsconfig.build.json --clean",
"build": "tsc --build tsconfig.build.json"
},

@@ -42,6 +37,2 @@ "repository": {

],
"browserslist": [
"defaults",
"IE 11"
],
"author": "Nathan Bridgewater <info@iws.io> (https://iws.io/)",

@@ -54,23 +45,14 @@ "license": "BSD-3-Clause",

"devDependencies": {
"@babel/cli": "^7.17.0",
"@babel/core": "^7.17.2",
"@babel/preset-env": "^7.16.11",
"@babel/register": "^7.17.0",
"@types/chai": "^4.3.4",
"@types/mocha": "^9.1.1",
"babel-loader": "^9.1.0",
"chai": "^4.3.7",
"eslint": "^8.8.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-promise": "^6.0.0",
"mocha": "^9.2.2",
"@typescript-eslint/eslint-plugin": "^5.39.0",
"@typescript-eslint/parser": "^5.39.0",
"eslint": "^8.41.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.1.0",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"serve": "^14.1.1",
"ts-node": "^10.9.1",
"typescript": "^4.5.5",
"webpack": "^5.68.0",
"webpack-cli": "^4.9.2"
"typescript": "^5.1.3"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc