|
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:
1.0 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const isPlainObject = require('../predicate/isPlainObject.js');
|
|---|
| 6 |
|
|---|
| 7 | function flattenObject(object, { delimiter = '.' } = {}) {
|
|---|
| 8 | return flattenObjectImpl(object, '', delimiter);
|
|---|
| 9 | }
|
|---|
| 10 | function flattenObjectImpl(object, prefix, delimiter) {
|
|---|
| 11 | const result = {};
|
|---|
| 12 | const keys = Object.keys(object);
|
|---|
| 13 | for (let i = 0; i < keys.length; i++) {
|
|---|
| 14 | const key = keys[i];
|
|---|
| 15 | const value = object[key];
|
|---|
| 16 | const prefixedKey = prefix ? `${prefix}${delimiter}${key}` : key;
|
|---|
| 17 | if (isPlainObject.isPlainObject(value) && Object.keys(value).length > 0) {
|
|---|
| 18 | Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
|
|---|
| 19 | continue;
|
|---|
| 20 | }
|
|---|
| 21 | if (Array.isArray(value) && value.length > 0) {
|
|---|
| 22 | Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
|
|---|
| 23 | continue;
|
|---|
| 24 | }
|
|---|
| 25 | result[prefixedKey] = value;
|
|---|
| 26 | }
|
|---|
| 27 | return result;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | exports.flattenObject = flattenObject;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.