@json2csv/plainjs
Advanced tools
Comparing version 7.0.3 to 7.0.4
@@ -25,8 +25,4 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FormatterTypes = void 0; | ||
const lodash_get_1 = __importDefault(require("lodash.get")); | ||
const formatters_1 = __importStar(require("@json2csv/formatters")); | ||
@@ -96,5 +92,3 @@ const utils_js_1 = require("./utils.js"); | ||
label: fieldInfo, | ||
value: fieldInfo.includes('.') || fieldInfo.includes('[') | ||
? (row) => (0, lodash_get_1.default)(row, fieldInfo, globalDefaultValue) | ||
: (row) => (0, utils_js_1.getProp)(row, fieldInfo, globalDefaultValue), | ||
value: (row) => (0, utils_js_1.getProp)(row, fieldInfo, globalDefaultValue), | ||
}; | ||
@@ -108,5 +102,3 @@ } | ||
label: fieldInfo.label || fieldInfo.value, | ||
value: fieldInfo.value.includes('.') || fieldInfo.value.includes('[') | ||
? (row) => (0, lodash_get_1.default)(row, fieldPath, defaultValue) | ||
: (row) => (0, utils_js_1.getProp)(row, fieldPath, defaultValue), | ||
value: (row) => (0, utils_js_1.getProp)(row, fieldPath, defaultValue), | ||
}; | ||
@@ -113,0 +105,0 @@ } |
@@ -6,2 +6,3 @@ type GetIndexedField<T, K> = K extends keyof T ? T[K] : K extends `${number}` ? 'length' extends keyof T ? number extends T['length'] ? number extends keyof T ? T[number] : undefined : undefined : undefined : undefined; | ||
export declare function getProp<TObject extends object, TKey extends keyof TObject>(obj: TObject, path: TKey, defaultValue: TObject[TKey]): TObject[TKey]; | ||
export declare function getProp<TObject, TPath extends string>(obj: TObject, path: TPath): Exclude<GetFieldType<TObject, TPath>, null | undefined>; | ||
export declare function getProp<TObject, TPath extends string, TDefault = GetFieldType<TObject, TPath>>(obj: TObject, path: TPath, defaultValue: TDefault): Exclude<GetFieldType<TObject, TPath>, null | undefined> | TDefault; | ||
@@ -8,0 +9,0 @@ export declare function flattenReducer<T>(acc: Array<T>, arr: Array<T> | T): Array<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fastJoin = exports.flattenReducer = exports.getProp = void 0; | ||
const rePropName = RegExp( | ||
// Match anything that isn't a dot or bracket. | ||
'[^.[\\]]+' + | ||
'|' + | ||
// Or match property names within brackets. | ||
'\\[(?:' + | ||
// Match a non-string expression. | ||
'([^"\'][^[]*)' + | ||
'|' + | ||
// Or match strings (supports escaping characters). | ||
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' + | ||
')\\]' + | ||
'|' + | ||
// Or match "" as the space between consecutive dots or empty brackets. | ||
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))', 'g'); | ||
function castPath(value) { | ||
var _a, _b, _c; | ||
const result = []; | ||
let match; | ||
while ((match = rePropName.exec(value))) { | ||
result.push((_c = (_a = match[3]) !== null && _a !== void 0 ? _a : (_b = match[1]) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : match[0]); | ||
} | ||
return result; | ||
} | ||
function getProp(obj, path, defaultValue) { | ||
const value = obj[path]; | ||
return value === undefined ? defaultValue : value; | ||
if (path in obj) { | ||
const value = obj[path]; | ||
return value === undefined ? defaultValue : value; | ||
} | ||
const processedPath = Array.isArray(path) ? path : castPath(path, obj); | ||
let currentValue = obj; | ||
for (const key of processedPath) { | ||
currentValue = currentValue === null || currentValue === void 0 ? void 0 : currentValue[key]; | ||
if (currentValue === undefined) | ||
return defaultValue; | ||
} | ||
return currentValue; | ||
} | ||
@@ -8,0 +42,0 @@ exports.getProp = getProp; |
@@ -1,2 +0,1 @@ | ||
import lodashGet from 'lodash.get'; | ||
import { default as defaultFormatter, number as numberFormatterCtor, string as stringFormatterCtor, symbol as symbolFormatterCtor, object as objectFormatterCtor, } from '@json2csv/formatters'; | ||
@@ -66,5 +65,3 @@ import { getProp, flattenReducer, fastJoin } from './utils.js'; | ||
label: fieldInfo, | ||
value: fieldInfo.includes('.') || fieldInfo.includes('[') | ||
? (row) => lodashGet(row, fieldInfo, globalDefaultValue) | ||
: (row) => getProp(row, fieldInfo, globalDefaultValue), | ||
value: (row) => getProp(row, fieldInfo, globalDefaultValue), | ||
}; | ||
@@ -78,5 +75,3 @@ } | ||
label: fieldInfo.label || fieldInfo.value, | ||
value: fieldInfo.value.includes('.') || fieldInfo.value.includes('[') | ||
? (row) => lodashGet(row, fieldPath, defaultValue) | ||
: (row) => getProp(row, fieldPath, defaultValue), | ||
value: (row) => getProp(row, fieldPath, defaultValue), | ||
}; | ||
@@ -83,0 +78,0 @@ } |
@@ -6,2 +6,3 @@ type GetIndexedField<T, K> = K extends keyof T ? T[K] : K extends `${number}` ? 'length' extends keyof T ? number extends T['length'] ? number extends keyof T ? T[number] : undefined : undefined : undefined : undefined; | ||
export declare function getProp<TObject extends object, TKey extends keyof TObject>(obj: TObject, path: TKey, defaultValue: TObject[TKey]): TObject[TKey]; | ||
export declare function getProp<TObject, TPath extends string>(obj: TObject, path: TPath): Exclude<GetFieldType<TObject, TPath>, null | undefined>; | ||
export declare function getProp<TObject, TPath extends string, TDefault = GetFieldType<TObject, TPath>>(obj: TObject, path: TPath, defaultValue: TDefault): Exclude<GetFieldType<TObject, TPath>, null | undefined> | TDefault; | ||
@@ -8,0 +9,0 @@ export declare function flattenReducer<T>(acc: Array<T>, arr: Array<T> | T): Array<T>; |
@@ -0,4 +1,38 @@ | ||
const rePropName = RegExp( | ||
// Match anything that isn't a dot or bracket. | ||
'[^.[\\]]+' + | ||
'|' + | ||
// Or match property names within brackets. | ||
'\\[(?:' + | ||
// Match a non-string expression. | ||
'([^"\'][^[]*)' + | ||
'|' + | ||
// Or match strings (supports escaping characters). | ||
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' + | ||
')\\]' + | ||
'|' + | ||
// Or match "" as the space between consecutive dots or empty brackets. | ||
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))', 'g'); | ||
function castPath(value) { | ||
var _a, _b, _c; | ||
const result = []; | ||
let match; | ||
while ((match = rePropName.exec(value))) { | ||
result.push((_c = (_a = match[3]) !== null && _a !== void 0 ? _a : (_b = match[1]) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : match[0]); | ||
} | ||
return result; | ||
} | ||
export function getProp(obj, path, defaultValue) { | ||
const value = obj[path]; | ||
return value === undefined ? defaultValue : value; | ||
if (path in obj) { | ||
const value = obj[path]; | ||
return value === undefined ? defaultValue : value; | ||
} | ||
const processedPath = Array.isArray(path) ? path : castPath(path, obj); | ||
let currentValue = obj; | ||
for (const key of processedPath) { | ||
currentValue = currentValue === null || currentValue === void 0 ? void 0 : currentValue[key]; | ||
if (currentValue === undefined) | ||
return defaultValue; | ||
} | ||
return currentValue; | ||
} | ||
@@ -5,0 +39,0 @@ export function flattenReducer(acc, arr) { |
{ | ||
"name": "@json2csv/plainjs", | ||
"version": "7.0.3", | ||
"version": "7.0.4", | ||
"description": "Pure Javascript JSON to CSV converter.", | ||
@@ -51,6 +51,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@json2csv/formatters": "^7.0.3", | ||
"@streamparser/json": "^0.0.17", | ||
"lodash.get": "^4.4.2" | ||
"@json2csv/formatters": "^7.0.4", | ||
"@streamparser/json": "^0.0.17" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import lodashGet from 'lodash.get'; | ||
import { | ||
@@ -170,6 +169,3 @@ type Formatter, | ||
label: fieldInfo, | ||
value: | ||
fieldInfo.includes('.') || fieldInfo.includes('[') | ||
? (row) => lodashGet(row, fieldInfo, globalDefaultValue) | ||
: (row) => getProp(row, fieldInfo, globalDefaultValue as unknown), | ||
value: (row) => getProp(row, fieldInfo, globalDefaultValue), | ||
}; | ||
@@ -186,6 +182,3 @@ } | ||
label: fieldInfo.label || fieldInfo.value, | ||
value: | ||
fieldInfo.value.includes('.') || fieldInfo.value.includes('[') | ||
? (row) => lodashGet(row, fieldPath, defaultValue) | ||
: (row) => getProp(row, fieldPath, defaultValue), | ||
value: (row) => getProp(row, fieldPath, defaultValue), | ||
}; | ||
@@ -192,0 +185,0 @@ } |
@@ -44,2 +44,45 @@ type GetIndexedField<T, K> = K extends keyof T | ||
const rePropName = RegExp( | ||
// Match anything that isn't a dot or bracket. | ||
'[^.[\\]]+' + | ||
'|' + | ||
// Or match property names within brackets. | ||
'\\[(?:' + | ||
// Match a non-string expression. | ||
'([^"\'][^[]*)' + | ||
'|' + | ||
// Or match strings (supports escaping characters). | ||
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' + | ||
')\\]' + | ||
'|' + | ||
// Or match "" as the space between consecutive dots or empty brackets. | ||
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))', | ||
'g', | ||
); | ||
/** | ||
* Casts `value` to a path array if it's not one. | ||
* | ||
* @private | ||
* @param {*} value The value to inspect. | ||
* @param {Object} [object] The object to query keys on. | ||
* @returns {Array} Returns the cast property path array. | ||
*/ | ||
function castPath<TKey extends keyof TObject, TObject extends object>( | ||
path: TKey, | ||
obj: TObject, | ||
): [TKey]; | ||
function castPath<TPath extends string, TObject>( | ||
path: TPath, | ||
obj: TObject, | ||
): Exclude<GetFieldType<TObject, TPath>, null | undefined>; | ||
function castPath(value: string): string[] { | ||
const result: string[] = []; | ||
let match: RegExpExecArray | null; | ||
while ((match = rePropName.exec(value))) { | ||
result.push(match[3] ?? match[1]?.trim() ?? match[0]); | ||
} | ||
return result; | ||
} | ||
export function getProp<TObject extends object, TKey extends keyof TObject>( | ||
@@ -50,2 +93,6 @@ obj: TObject, | ||
): TObject[TKey]; | ||
export function getProp<TObject, TPath extends string>( | ||
obj: TObject, | ||
path: TPath, | ||
): Exclude<GetFieldType<TObject, TPath>, null | undefined>; | ||
export function getProp< | ||
@@ -60,5 +107,19 @@ TObject, | ||
): Exclude<GetFieldType<TObject, TPath>, null | undefined> | TDefault; | ||
export function getProp<T>(obj: any, path: PropertyName, defaultValue?: T): T { | ||
const value = obj[path]; | ||
return value === undefined ? defaultValue : value; | ||
export function getProp<T>( | ||
obj: any, | ||
path: PropertyName, | ||
defaultValue?: T, | ||
): T | undefined { | ||
if (path in obj) { | ||
const value = obj[path]; | ||
return value === undefined ? defaultValue : value; | ||
} | ||
const processedPath = Array.isArray(path) ? path : castPath(path, obj); | ||
let currentValue = obj; | ||
for (const key of processedPath) { | ||
currentValue = currentValue?.[key]; | ||
if (currentValue === undefined) return defaultValue; | ||
} | ||
return currentValue; | ||
} | ||
@@ -65,0 +126,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
212737
2
2022
- Removedlodash.get@^4.4.2
- Removedlodash.get@4.4.2(transitive)
Updated@json2csv/formatters@^7.0.4