Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves issue #365 with crush() not handling dots in object keys #368

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"autoHide.autoHidePanel": false
}
14 changes: 10 additions & 4 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,16 @@ const keys = (value) => {
const crush = (value) => {
if (!value)
return {};
return objectify(
keys(value),
(k) => k,
(k) => get(value, k)
const getKeys = (nested, paths) => {
const inner = (k, v) => {
if (!(isObject(v) || isArray(v)))
return [{ [[...paths, `${k}`].join(".")]: v }];
return getKeys(v, [...paths, `${k}`]);
};
return isArray(nested) ? nested.flatMap((v, i) => inner(`${i}`, v)) : Object.entries(nested).flatMap(([k, v]) => inner(k, v));
};
return Object.fromEntries(
getKeys(value, []).flatMap((obj) => Object.entries(obj))
);
};
const construct = (obj) => {
Expand Down
14 changes: 10 additions & 4 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,16 @@ var radash = (function (exports) {
const crush = (value) => {
if (!value)
return {};
return objectify(
keys(value),
(k) => k,
(k) => get(value, k)
const getKeys = (nested, paths) => {
const inner = (k, v) => {
if (!(isObject(v) || isArray(v)))
return [{ [[...paths, `${k}`].join(".")]: v }];
return getKeys(v, [...paths, `${k}`]);
};
return isArray(nested) ? nested.flatMap((v, i) => inner(`${i}`, v)) : Object.entries(nested).flatMap(([k, v]) => inner(k, v));
};
return Object.fromEntries(
getKeys(value, []).flatMap((obj) => Object.entries(obj))
);
};
const construct = (obj) => {
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export const keys = <TValue extends object>(value: TValue): string[] => {
* crush({ name: 'ra', children: [{ name: 'hathor' }] })
* // { name: 'ra', 'children.0.name': 'hathor' }
*/
export const crush = <TValue extends object>(value: TValue): object => {
export const old_crush = <TValue extends object>(value: TValue): object => {
if (!value) return {}
return objectify(
keys(value),
Expand All @@ -330,6 +330,24 @@ export const crush = <TValue extends object>(value: TValue): object => {
)
}

export const crush = <TValue extends object>(value: TValue): object => {
if (!value) return {}
const getKeys = (nested: any, paths: string[]): object[] => {
const inner = (k: string, v: any) => {
if (!(isObject(v) || isArray(v)))
return [{ [[...paths, `${k}`].join('.')]: v }]
return getKeys(v, [...paths, `${k}`])
}

return isArray(nested)
? nested.flatMap((v, i) => inner(`${i}`, v))
: Object.entries(nested).flatMap(([k, v]) => inner(k, v))
}
return Object.fromEntries(
getKeys(value, []).flatMap(obj => Object.entries(obj))
)
}

/**
* The opposite of crush, given an object that was
* crushed into key paths and values will return
Expand Down
29 changes: 19 additions & 10 deletions src/tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,8 @@ describe('object module', () => {
const ra = {
name: 'ra',
power: 100,
friend: {
name: 'loki',
power: 80
},
enemies: [
{
name: 'hathor',
power: 12
}
],
friend: { name: 'loki', power: 80 },
enemies: [{ name: 'hathor', power: 12 }],
timestamp: now
}
assert.deepEqual(_.crush(ra), {
Expand All @@ -539,6 +531,23 @@ describe('object module', () => {
timestamp: now
})
})
test('handles key in dots correctly', () => {
const now = new Date()
const ra = {
key1: 'value1',
obj1: { x: 'x', y: 10 },
arr1: ['xx', { key2: 'value2' }, { key3: [false, now] }]
}
assert.deepEqual(_.crush(ra), {
key1: 'value1',
'obj1.x': 'x',
'obj1.y': 10,
'arr1.0': 'xx',
'arr1.1.key2': 'value2',
'arr1.2.key3.0': false,
'arr1.2.key3.1': now
})
})
})

describe('construct function', () => {
Expand Down
Loading