Skip to content

Commit

Permalink
add crush function to object module (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray committed Jan 7, 2023
1 parent 618cae2 commit b852d2e
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 3 deletions.
11 changes: 10 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,15 @@ const keys = (value) => {
};
return getKeys(value, []);
};
const crush = (value) => {
if (!value)
return {};
return objectify(
keys(value),
(k) => k,
(k) => get(value, k)
);
};

const random = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
Expand Down Expand Up @@ -835,4 +844,4 @@ const trim = (str, charsToTrim = " ") => {
return str.replace(regex, "");
};

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 };
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 };
10 changes: 10 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,15 @@ var radash = (function (exports) {
};
return getKeys(value, []);
};
const crush = (value) => {
if (!value)
return {};
return objectify(
keys(value),
(k) => k,
(k) => get(value, k)
);
};

const random = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
Expand Down Expand Up @@ -849,6 +858,7 @@ var radash = (function (exports) {
exports.cluster = cluster;
exports.compose = compose;
exports.counting = counting;
exports.crush = crush;
exports.dash = dash;
exports.debounce = debounce;
exports.defer = defer;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/object/crush.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: crush
description: Flattens a deep object to a single dimension
group: Object
---

## Basic usage

Flattens a deep object to a single dimension. The deep keys will be converted to a dot notation in the new object.

```ts
import { crush } from 'radash'

const ra = {
name: 'ra',
power: 100,
friend: {
name: 'loki',
power: 80
},
enemies: [
{
name: 'hathor',
power: 12
}
]
}

crush(ra)
// {
// name: 'ra',
// power: 100,
// 'friend.name': 'loki',
// 'friend.power': 80,
// 'enemies.0.name': 'hathor',
// 'enemies.0.power': 12
// }
```
2 changes: 2 additions & 0 deletions docs/object/keys.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ objectify(
// 'enemies.0.power': 12
// }
```

_As of v10.5.0+ you can get this behavior via the crush function_
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "10.4.0",
"version": "10.5.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export { toFloat, toInt } from './number'
export {
assign,
clone,
crush,
get,
invert,
keys,
Expand Down
18 changes: 18 additions & 0 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { objectify } from './array'
import { isArray, isObject, isPrimitive } from './typed'

type LowercasedKeys<T extends Record<string, any>> = {
Expand Down Expand Up @@ -269,3 +270,20 @@ export const keys = <TValue extends object>(value: TValue): string[] => {
}
return getKeys(value, [])
}

/**
* Flattens a deep object to a single demension, converting
* the keys to dot notation.
*
* @example
* crush({ name: 'ra', children: [{ name: 'hathor' }] })
* // { name: 'ra', 'children.0.name': 'hathor' }
*/
export const crush = <TValue extends object>(value: TValue): object => {
if (!value) return {}
return objectify(
keys(value),
k => k,
k => get(value, k)
)
}
32 changes: 32 additions & 0 deletions src/tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,36 @@ describe('object module', () => {
])
})
})

describe('crush function', () => {
test('handles bad input', () => {
assert.deepEqual(_.crush({}), {})
assert.deepEqual(_.crush(null as any), {})
assert.deepEqual(_.crush(undefined as any), {})
})
test('returns correctly crushed object', () => {
const ra = {
name: 'ra',
power: 100,
friend: {
name: 'loki',
power: 80
},
enemies: [
{
name: 'hathor',
power: 12
}
]
}
assert.deepEqual(_.crush(ra), {
name: 'ra',
power: 100,
'friend.name': 'loki',
'friend.power': 80,
'enemies.0.name': 'hathor',
'enemies.0.power': 12
})
})
})
})

1 comment on commit b852d2e

@vercel
Copy link

@vercel vercel bot commented on b852d2e Jan 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.