Skip to content

Commit b852d2e

Browse files
authored
add crush function to object module (#228)
1 parent 618cae2 commit b852d2e

File tree

9 files changed

+113
-3
lines changed

9 files changed

+113
-3
lines changed

Diff for: cdn/radash.esm.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,15 @@ const keys = (value) => {
697697
};
698698
return getKeys(value, []);
699699
};
700+
const crush = (value) => {
701+
if (!value)
702+
return {};
703+
return objectify(
704+
keys(value),
705+
(k) => k,
706+
(k) => get(value, k)
707+
);
708+
};
700709

701710
const random = (min, max) => {
702711
return Math.floor(Math.random() * (max - min + 1) + min);
@@ -835,4 +844,4 @@ const trim = (str, charsToTrim = " ") => {
835844
return str.replace(regex, "");
836845
};
837846

838-
export { alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, counting, dash, debounce, defer, diff, draw, first, flat, fork, get, group, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
847+
export { alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };

Diff for: cdn/radash.js

+10
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,15 @@ var radash = (function (exports) {
700700
};
701701
return getKeys(value, []);
702702
};
703+
const crush = (value) => {
704+
if (!value)
705+
return {};
706+
return objectify(
707+
keys(value),
708+
(k) => k,
709+
(k) => get(value, k)
710+
);
711+
};
703712

704713
const random = (min, max) => {
705714
return Math.floor(Math.random() * (max - min + 1) + min);
@@ -849,6 +858,7 @@ var radash = (function (exports) {
849858
exports.cluster = cluster;
850859
exports.compose = compose;
851860
exports.counting = counting;
861+
exports.crush = crush;
852862
exports.dash = dash;
853863
exports.debounce = debounce;
854864
exports.defer = defer;

Diff for: cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/object/crush.mdx

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: crush
3+
description: Flattens a deep object to a single dimension
4+
group: Object
5+
---
6+
7+
## Basic usage
8+
9+
Flattens a deep object to a single dimension. The deep keys will be converted to a dot notation in the new object.
10+
11+
```ts
12+
import { crush } from 'radash'
13+
14+
const ra = {
15+
name: 'ra',
16+
power: 100,
17+
friend: {
18+
name: 'loki',
19+
power: 80
20+
},
21+
enemies: [
22+
{
23+
name: 'hathor',
24+
power: 12
25+
}
26+
]
27+
}
28+
29+
crush(ra)
30+
// {
31+
// name: 'ra',
32+
// power: 100,
33+
// 'friend.name': 'loki',
34+
// 'friend.power': 80,
35+
// 'enemies.0.name': 'hathor',
36+
// 'enemies.0.power': 12
37+
// }
38+
```

Diff for: docs/object/keys.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ objectify(
5656
// 'enemies.0.power': 12
5757
// }
5858
```
59+
60+
_As of v10.5.0+ you can get this behavior via the crush function_

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "10.4.0",
3+
"version": "10.5.0",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",

Diff for: src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export { toFloat, toInt } from './number'
5555
export {
5656
assign,
5757
clone,
58+
crush,
5859
get,
5960
invert,
6061
keys,

Diff for: src/object.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { objectify } from './array'
12
import { isArray, isObject, isPrimitive } from './typed'
23

34
type LowercasedKeys<T extends Record<string, any>> = {
@@ -269,3 +270,20 @@ export const keys = <TValue extends object>(value: TValue): string[] => {
269270
}
270271
return getKeys(value, [])
271272
}
273+
274+
/**
275+
* Flattens a deep object to a single demension, converting
276+
* the keys to dot notation.
277+
*
278+
* @example
279+
* crush({ name: 'ra', children: [{ name: 'hathor' }] })
280+
* // { name: 'ra', 'children.0.name': 'hathor' }
281+
*/
282+
export const crush = <TValue extends object>(value: TValue): object => {
283+
if (!value) return {}
284+
return objectify(
285+
keys(value),
286+
k => k,
287+
k => get(value, k)
288+
)
289+
}

Diff for: src/tests/object.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,36 @@ describe('object module', () => {
415415
])
416416
})
417417
})
418+
419+
describe('crush function', () => {
420+
test('handles bad input', () => {
421+
assert.deepEqual(_.crush({}), {})
422+
assert.deepEqual(_.crush(null as any), {})
423+
assert.deepEqual(_.crush(undefined as any), {})
424+
})
425+
test('returns correctly crushed object', () => {
426+
const ra = {
427+
name: 'ra',
428+
power: 100,
429+
friend: {
430+
name: 'loki',
431+
power: 80
432+
},
433+
enemies: [
434+
{
435+
name: 'hathor',
436+
power: 12
437+
}
438+
]
439+
}
440+
assert.deepEqual(_.crush(ra), {
441+
name: 'ra',
442+
power: 100,
443+
'friend.name': 'loki',
444+
'friend.power': 80,
445+
'enemies.0.name': 'hathor',
446+
'enemies.0.power': 12
447+
})
448+
})
449+
})
418450
})

0 commit comments

Comments
 (0)