|
Last change
on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
928 bytes
|
| Rev | Line | |
|---|
| [a762898] | 1 | import { isPlainObject } from '../predicate/isPlainObject.mjs';
|
|---|
| 2 |
|
|---|
| 3 | function flattenObject(object, { delimiter = '.' } = {}) {
|
|---|
| 4 | return flattenObjectImpl(object, '', delimiter);
|
|---|
| 5 | }
|
|---|
| 6 | function flattenObjectImpl(object, prefix, delimiter) {
|
|---|
| 7 | const result = {};
|
|---|
| 8 | const keys = Object.keys(object);
|
|---|
| 9 | for (let i = 0; i < keys.length; i++) {
|
|---|
| 10 | const key = keys[i];
|
|---|
| 11 | const value = object[key];
|
|---|
| 12 | const prefixedKey = prefix ? `${prefix}${delimiter}${key}` : key;
|
|---|
| 13 | if (isPlainObject(value) && Object.keys(value).length > 0) {
|
|---|
| 14 | Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
|
|---|
| 15 | continue;
|
|---|
| 16 | }
|
|---|
| 17 | if (Array.isArray(value) && value.length > 0) {
|
|---|
| 18 | Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
|
|---|
| 19 | continue;
|
|---|
| 20 | }
|
|---|
| 21 | result[prefixedKey] = value;
|
|---|
| 22 | }
|
|---|
| 23 | return result;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | export { flattenObject };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.