Skip to content

Commit

Permalink
refactor: remove lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Oct 12, 2023
1 parent 3f075bd commit 8b8ba69
Show file tree
Hide file tree
Showing 26 changed files with 551 additions and 73 deletions.
3 changes: 1 addition & 2 deletions build-cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const replaceDependenciesByJsdelivr = {
});

const dependencies = {
'@streamparser/json': `https://cdn.jsdelivr.net/npm/@streamparser/json@${pkg.dependencies['@streamparser/json']}/dist/mjs/index.js`,
'lodash.get': 'https://cdn.jsdelivr.net/gh/lodash/lodash@master/get.js'
'@streamparser/json': `https://cdn.jsdelivr.net/npm/@streamparser/json@${pkg.dependencies['@streamparser/json']}/dist/mjs/index.js`
};

build.onResolve({ namespace: 'file', filter: new RegExp(`(?:${Object.keys(dependencies).join('|')})`) }, (args) => {
Expand Down
5 changes: 2 additions & 3 deletions dist/cdn/plainjs/BaseParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// packages/plainjs/src/BaseParser.ts
import lodashGet from "https://cdn.jsdelivr.net/gh/lodash/lodash@master/get.js";
import {
default as defaultFormatter,
number as numberFormatterCtor,
Expand Down Expand Up @@ -78,7 +77,7 @@ var JSON2CSVBase = class {
if (typeof fieldInfo === "string") {
return {
label: fieldInfo,
value: fieldInfo.includes(".") || fieldInfo.includes("[") ? (row) => lodashGet(row, fieldInfo, globalDefaultValue) : (row) => getProp(row, fieldInfo, globalDefaultValue)
value: (row) => getProp(row, fieldInfo, globalDefaultValue)
};
}
if (typeof fieldInfo === "object") {
Expand All @@ -87,7 +86,7 @@ var JSON2CSVBase = class {
const fieldPath = fieldInfo.value;
return {
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)
};
}
if (typeof fieldInfo.value === "function") {
Expand Down
2 changes: 1 addition & 1 deletion dist/cdn/plainjs/StreamParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TokenParser,
TokenType,
TokenizerError
} from "https://cdn.jsdelivr.net/npm/@streamparser/json@^0.0.16/dist/mjs/index.js";
} from "https://cdn.jsdelivr.net/npm/@streamparser/json@^0.0.17/dist/mjs/index.js";
import JSON2CSVBase from "./BaseParser.js";
var JSON2CSVStreamParser = class extends JSON2CSVBase {
constructor(opts, asyncOpts) {
Expand Down
28 changes: 26 additions & 2 deletions dist/cdn/plainjs/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
// packages/plainjs/src/utils.ts
var rePropName = RegExp(
// Match anything that isn't a dot or bracket.
`[^.[\\]]+|\\[(?:([^"'][^[]*)|(["'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]|(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))`,
"g"
);
function castPath(value, object) {
var _a, _b, _c;
if (Array.isArray(value))
return value;
if (value in object)
return [value];
const result = [];
let match;
while (match = rePropName.exec(value)) {
result.push((_c = (_b = match[3]) != null ? _b : (_a = match[1]) == null ? void 0 : _a.trim()) != null ? _c : match[0]);
}
return result;
}
function getProp(obj, path, defaultValue) {
const value = obj[path];
return value === void 0 ? defaultValue : value;
const processedPath = castPath(path, obj);
let currentValue = obj;
for (const key of processedPath) {
currentValue = currentValue == null ? void 0 : currentValue[key];
if (currentValue === void 0)
return defaultValue;
}
return currentValue;
}
function flattenReducer(acc, arr) {
try {
Expand Down
5 changes: 2 additions & 3 deletions dist/cdn/transforms/unwind.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// packages/transforms/src/unwind.ts
import lodashGet from "https://cdn.jsdelivr.net/gh/lodash/lodash@master/get.js";
import { setProp, unsetProp, flattenReducer } from "./utils.js";
import { getProp, setProp, unsetProp, flattenReducer } from "./utils.js";
function getUnwindablePaths(obj, currentPath) {
return Object.keys(obj).reduce(
(unwindablePaths, key) => {
Expand All @@ -24,7 +23,7 @@ function getUnwindablePaths(obj, currentPath) {
function unwind(opts = {}) {
function unwindReducer(rows, unwindPath) {
return rows.flatMap((row) => {
const unwindArray = lodashGet(row, unwindPath);
const unwindArray = getProp(row, unwindPath);
if (!Array.isArray(unwindArray)) {
return row;
}
Expand Down
29 changes: 29 additions & 0 deletions dist/cdn/transforms/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
// packages/transforms/src/utils.ts
var rePropName = RegExp(
// Match anything that isn't a dot or bracket.
`[^.[\\]]+|\\[(?:([^"'][^[]*)|(["'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]|(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))`,
"g"
);
function castPath(value, object) {
var _a, _b, _c;
if (Array.isArray(value))
return value;
if (value in object)
return [value];
const result = [];
let match;
while (match = rePropName.exec(value)) {
result.push((_c = (_b = match[3]) != null ? _b : (_a = match[1]) == null ? void 0 : _a.trim()) != null ? _c : match[0]);
}
return result;
}
function getProp(obj, path, defaultValue) {
const processedPath = castPath(path, obj);
let currentValue = obj;
for (const key of processedPath) {
currentValue = currentValue == null ? void 0 : currentValue[key];
if (currentValue === void 0)
return defaultValue;
}
return currentValue;
}
function propertyPathToString(path) {
if (typeof path === "string")
return path.split(".");
Expand Down Expand Up @@ -42,6 +70,7 @@ function flattenReducer(acc, arr) {
}
export {
flattenReducer,
getProp,
setProp,
unsetProp
};
31 changes: 3 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"changelog:update": "conventional-changelog-cli -p conventionalcommits -i CHANGELOG.md -s && cp CHANGELOG.md docs/others/CHANGELOG.md"
},
"devDependencies": {
"@types/lodash.get": "^4.4.7",
"@types/tape": "^5.6.0",
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/test/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ export default function (
t.equal(csv, csvFixtures.nested);
});

testRunner.add('should support nested properties selectors using braket notation', async (t) => {

Check failure on line 304 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Replace `'should·support·nested·properties·selectors·using·braket·notation',` with `⏎····'should·support·nested·properties·selectors·using·braket·notation',⏎···`

Check failure on line 304 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Replace `'should·support·nested·properties·selectors·using·braket·notation',` with `⏎····'should·support·nested·properties·selectors·using·braket·notation',⏎···`
const opts = `--config "${getFixturePath('/fields/nestedWithBrackets.json')}"`;

Check failure on line 305 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Replace `····const·opts·=·`--config·"${getFixturePath('/fields/nestedWithBrackets.json'` with `······const·opts·=·`--config·"${getFixturePath(⏎········'/fields/nestedWithBrackets.json',⏎······`

Check failure on line 305 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Replace `····const·opts·=·`--config·"${getFixturePath('/fields/nestedWithBrackets.json'` with `······const·opts·=·`--config·"${getFixturePath(⏎········'/fields/nestedWithBrackets.json',⏎······`

const { stdout: csv } = await execAsync(

Check failure on line 307 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Insert `··`

Check failure on line 307 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Insert `··`
`${cli} -i "${getFixturePath('/json/nested.json')}" ${opts}`,

Check failure on line 308 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Insert `··`

Check failure on line 308 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Insert `··`
);

Check failure on line 309 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Insert `··`

Check failure on line 309 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Insert `··`

t.equal(csv, csvFixtures.nested);

Check failure on line 311 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Insert `··`

Check failure on line 311 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Insert `··`
});

Check failure on line 312 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Replace `}` with `··},⏎··`

Check failure on line 312 in packages/cli/test/CLI.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Replace `}` with `··},⏎··`

testRunner.add(
'field.value function should receive a valid field object',
async (t) => {
Expand Down
34 changes: 33 additions & 1 deletion packages/node/test/AsyncParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export default function (
},
{
label: 'Price',
value: 'price',
value: 'prices[0]',
},
{
label: 'Color',
Expand All @@ -441,6 +441,38 @@ export default function (
t.equal(csv, csvFixtures.nested);
});

testRunner.add('should support nested properties selectors using braket notation', async (t) => {

Check failure on line 444 in packages/node/test/AsyncParser.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Replace `'should·support·nested·properties·selectors·using·braket·notation',` with `⏎····'should·support·nested·properties·selectors·using·braket·notation',⏎···`

Check failure on line 444 in packages/node/test/AsyncParser.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Replace `'should·support·nested·properties·selectors·using·braket·notation',` with `⏎····'should·support·nested·properties·selectors·using·braket·notation',⏎···`
const opts: ParserOptions = {

Check failure on line 445 in packages/node/test/AsyncParser.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Replace `····` with `······`

Check failure on line 445 in packages/node/test/AsyncParser.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Replace `····` with `······`
fields: [

Check failure on line 446 in packages/node/test/AsyncParser.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18.x)

Insert `··`

Check failure on line 446 in packages/node/test/AsyncParser.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Insert `··`
{
label: 'Make',
value: 'car[make]',
},
{
label: 'Model',
value: 'car["model"]',
},
{
label: 'Price',
value: 'prices[0]',
},
{
label: 'Color',
value: 'color',
},
{
label: 'Year',
value: 'car[\'ye\'][ar]',
},
],
};

const parser = new Parser(opts);
const csv = await parseInput(parser, jsonFixtures.nested());

t.equal(csv, csvFixtures.nested);
});

testRunner.add(
'field.value function should receive a valid field object',
async (t) => {
Expand Down
34 changes: 33 additions & 1 deletion packages/node/test/AsyncParserInMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export default function (
},
{
label: 'Price',
value: 'price',
value: 'prices[0]',
},
{
label: 'Color',
Expand All @@ -441,6 +441,38 @@ export default function (
t.equal(csv, csvFixtures.nested);
});

testRunner.add('should support nested properties selectors using braket notation', async (t) => {
const opts: ParserOptions = {
fields: [
{
label: 'Make',
value: 'car[make]',
},
{
label: 'Model',
value: 'car["model"]',
},
{
label: 'Price',
value: 'prices[0]',
},
{
label: 'Color',
value: 'color',
},
{
label: 'Year',
value: 'car[\'ye\'][ar]',
},
],
};

const parser = new Parser(opts);
const csv = await parseInput(parser, jsonFixtures.nested());

t.equal(csv, csvFixtures.nested);
});

testRunner.add(
'field.value function should receive a valid field object',
async (t) => {
Expand Down
34 changes: 33 additions & 1 deletion packages/node/test/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export default function (
},
{
label: 'Price',
value: 'price',
value: 'prices[0]',
},
{
label: 'Color',
Expand All @@ -427,6 +427,38 @@ export default function (
t.equal(csv, csvFixtures.nested);
});

testRunner.add('should support nested properties selectors using braket notation', async (t) => {
const opts: ParserOptions = {
fields: [
{
label: 'Make',
value: 'car[make]',
},
{
label: 'Model',
value: 'car["model"]',
},
{
label: 'Price',
value: 'prices[0]',
},
{
label: 'Color',
value: 'color',
},
{
label: 'Year',
value: 'car[\'ye\'][ar]',
},
],
};

const parser = new Parser(opts);
const csv = await parseInput(parser, jsonFixtures.nested());

t.equal(csv, csvFixtures.nested);
});

testRunner.add(
'field.value function should receive a valid field object',
async (t) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/plainjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
},
"dependencies": {
"@json2csv/formatters": "^7.0.3",
"@streamparser/json": "^0.0.17",
"lodash.get": "^4.4.2"
"@streamparser/json": "^0.0.17"
}
}
Loading

0 comments on commit 8b8ba69

Please sign in to comment.