@homer0/object-utils 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +228 -0
- package/dist/esm/index.js +363 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.ts +403 -0
- package/dist/index.js +407 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Leonardo Apiwan (@homer0)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# 🧰 Object utils
|
|
2
|
+
|
|
3
|
+
A small collection of utility methods to work with objects. It relies on [`extend`](https://www.npmjs.com/package/extend) for deep merge and copy.
|
|
4
|
+
|
|
5
|
+
## 🍿 Usage
|
|
6
|
+
|
|
7
|
+
> If you are wondering why I built this, go to the [Motivation](#motivation) section.
|
|
8
|
+
|
|
9
|
+
- ⚙️ [Functions](#%EF%B8%8F-functions)
|
|
10
|
+
- 🤘 [Development](#-development)
|
|
11
|
+
|
|
12
|
+
### ⚙️ Functions
|
|
13
|
+
|
|
14
|
+
- [`merge`](#merge)
|
|
15
|
+
- [`copy`](#copy)
|
|
16
|
+
- [`get`](#get)
|
|
17
|
+
- [`set`](#set)
|
|
18
|
+
- [`extract`](#extract)
|
|
19
|
+
- [`remove`](#remove)
|
|
20
|
+
- [`flat`](#flat)
|
|
21
|
+
- [`unflat`](#unflat)
|
|
22
|
+
- [`formatKeys`](#formatKeys)
|
|
23
|
+
|
|
24
|
+
#### `merge`
|
|
25
|
+
|
|
26
|
+
Makes a deep merge of a list of objects.
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import { merge } from '@homer0/object-utils';
|
|
30
|
+
|
|
31
|
+
const objA = { a: 'first' };
|
|
32
|
+
const objB = { b: 'second' };
|
|
33
|
+
|
|
34
|
+
console.log(merge(objA, objB));
|
|
35
|
+
// Will output { a: 'first', b: 'second' }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### `copy`
|
|
39
|
+
|
|
40
|
+
Makes a deep copy of an object.
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
import { copy } from '@homer0/object-utils';
|
|
44
|
+
|
|
45
|
+
const objA = { a: 'first' };
|
|
46
|
+
const objB = copy(objA);
|
|
47
|
+
objA.b = 'second';
|
|
48
|
+
|
|
49
|
+
console.log(objB);
|
|
50
|
+
// Will output { a: 'first' }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### `get`
|
|
54
|
+
|
|
55
|
+
Reads a property from an object using a path:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { get } from '@homer0/object-utils';
|
|
59
|
+
|
|
60
|
+
const obj = {
|
|
61
|
+
propOne: {
|
|
62
|
+
propOneSub: 'Charito!',
|
|
63
|
+
},
|
|
64
|
+
propTwo: '!!!',
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
console.log(
|
|
68
|
+
get({
|
|
69
|
+
target: obj,
|
|
70
|
+
path: 'propOne.propOneSub',
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
// Will output 'Charito!'
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### `set`
|
|
77
|
+
|
|
78
|
+
Sets a property on an object using a path. If the path doesn't exist, it will be created.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { set } from '@homer0/object-utils';
|
|
82
|
+
|
|
83
|
+
const target = {};
|
|
84
|
+
|
|
85
|
+
console.log(
|
|
86
|
+
set({
|
|
87
|
+
target,
|
|
88
|
+
path: 'some.prop.path',
|
|
89
|
+
value: 'some-value',
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
// Will output { some: { prop: { path: 'some-value' } } }
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### `extract`
|
|
96
|
+
|
|
97
|
+
Extracts a property or properties from an object in order to create a new one.
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
import { extract } from '@homer0/object-utils';
|
|
101
|
+
|
|
102
|
+
const target = {
|
|
103
|
+
name: {
|
|
104
|
+
first: 'Pilar',
|
|
105
|
+
},
|
|
106
|
+
age: 2,
|
|
107
|
+
address: {
|
|
108
|
+
planet: 'earth',
|
|
109
|
+
something: 'else',
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
console.log(
|
|
114
|
+
set({
|
|
115
|
+
target: obj,
|
|
116
|
+
paths: [{ name: 'name.first' }, 'age', 'address.planet'],
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
// Will output { name: 'Pilar', age: 2, address: { planet: 'earth' } }
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### `remove`
|
|
123
|
+
|
|
124
|
+
Deletes a property of an object using a path. If by removing a property of a sub object, the object has no more keys, it also removes it.
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
import { remove } from '@homer0/object-utils';
|
|
128
|
+
|
|
129
|
+
const target = {
|
|
130
|
+
propOne: {
|
|
131
|
+
propOneSub: 'Charito!',
|
|
132
|
+
},
|
|
133
|
+
propTwo: '!!!',
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
console.log(
|
|
137
|
+
remove({
|
|
138
|
+
target,
|
|
139
|
+
path: 'propOne.propOneSub',
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
// Will output { propTwo: '!!!' }
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### `flat`
|
|
146
|
+
|
|
147
|
+
Flattens an object properties into a single level dictionary.
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
import { flat } from '@homer0/object-utils';
|
|
151
|
+
|
|
152
|
+
const target = {
|
|
153
|
+
propOne: {
|
|
154
|
+
propOneSub: 'Charito!',
|
|
155
|
+
},
|
|
156
|
+
propTwo: '!!!',
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
console.log(flat({ target }));
|
|
160
|
+
// Will output { 'propOne.propOneSub': 'Charito!', propTwo: '!!!' }
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### `unflat`
|
|
164
|
+
|
|
165
|
+
This method does the exact opposite from `flat`: It takes an already flatten object and restores its structure.
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
import { unflat } from '@homer0/object-utils';
|
|
169
|
+
|
|
170
|
+
const target = {
|
|
171
|
+
'propOne.propOneSub': 'Charito!
|
|
172
|
+
propTwo: '!!!',
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
console.log(unflat({ target }));
|
|
176
|
+
// Will output { propOne: { propOneSub: 'Charito!' }, 'propTwo': '!!!' }
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### `formatKeys`
|
|
180
|
+
|
|
181
|
+
Formats all the keys on an object using a way similar to `.replace(regexp, ...)` but that also works recursively and with _"object paths"_.
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
import { formatKeys } from '@homer0/object-utils';
|
|
185
|
+
|
|
186
|
+
const target = {
|
|
187
|
+
prop_one: 'Charito!',
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
console.log(
|
|
191
|
+
formatKeys({
|
|
192
|
+
target,
|
|
193
|
+
// Find all the keys with snake case.
|
|
194
|
+
search: /([a-z])_([a-z])/g,
|
|
195
|
+
// Using the same .replace style callback, replace it with lower camel case.
|
|
196
|
+
replace: (_, firstLetter, secondLetter) => {
|
|
197
|
+
const newSecondLetter = secondLetter.toUpperCase();
|
|
198
|
+
return `${firstLetter}${newSecondLetter}`;
|
|
199
|
+
},
|
|
200
|
+
}),
|
|
201
|
+
);
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
There are also a few "shorthand implementations" of `formatKeys`:
|
|
205
|
+
|
|
206
|
+
- `lowerCamelToSnakeKeys(...)`
|
|
207
|
+
- `lowerCamelToDashKeys(...)`
|
|
208
|
+
- `snakeToLowerCamelKeys(...)`
|
|
209
|
+
- `snakeToDashKeys(...)`
|
|
210
|
+
- `dashToLowerCamelKeys(...)`
|
|
211
|
+
- `dashToSnakeKeys(...)`
|
|
212
|
+
|
|
213
|
+
### 🤘 Development
|
|
214
|
+
|
|
215
|
+
As this project is part of the `packages` monorepo, it requires Yarn, and some of the tooling, like ESLint and Husky, are installed on the root's `package.json`.
|
|
216
|
+
|
|
217
|
+
#### Yarn tasks
|
|
218
|
+
|
|
219
|
+
| Task | Description |
|
|
220
|
+
| ------- | -------------------- |
|
|
221
|
+
| `test` | Runs the unit tests. |
|
|
222
|
+
| `build` | Bundles the project. |
|
|
223
|
+
|
|
224
|
+
## Motivation
|
|
225
|
+
|
|
226
|
+
This used to be part of the [`wootils`](https://www.npmjs.com/package/wootils) package, my personal lib of utilities, but I decided to extract them into individual packages, as part of the `packages` monorepo, and take the oportunity to migrate them to TypeScript.
|
|
227
|
+
|
|
228
|
+
What I like most about this library is that it's VERY small, and it only has one single dependency.
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
// src/fns/merge.ts
|
|
2
|
+
import extend from "extend";
|
|
3
|
+
var merge = (...targets) => {
|
|
4
|
+
const [firstTarget] = targets;
|
|
5
|
+
const base = Array.isArray(firstTarget) ? [] : {};
|
|
6
|
+
return extend(true, base, ...targets);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/fns/copy.ts
|
|
10
|
+
var copy = (target) => merge(target);
|
|
11
|
+
|
|
12
|
+
// src/fns/formatKeys.ts
|
|
13
|
+
var formatKeys = (options) => {
|
|
14
|
+
const {
|
|
15
|
+
target,
|
|
16
|
+
search,
|
|
17
|
+
replace,
|
|
18
|
+
include = [],
|
|
19
|
+
exclude = [],
|
|
20
|
+
pathDelimiter = "."
|
|
21
|
+
} = options;
|
|
22
|
+
const useTarget = target;
|
|
23
|
+
const keys = Object.keys(target);
|
|
24
|
+
const hasChildrenByKey = {};
|
|
25
|
+
keys.forEach((key) => {
|
|
26
|
+
const value = useTarget[key];
|
|
27
|
+
hasChildrenByKey[key] = !!(value && typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value));
|
|
28
|
+
});
|
|
29
|
+
const escapedPathDelimiter = pathDelimiter.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
|
30
|
+
const cleanPathStartExpression = new RegExp(`^${escapedPathDelimiter}`, "i");
|
|
31
|
+
const cleanPathEndExpression = new RegExp(`${escapedPathDelimiter}$`, "i");
|
|
32
|
+
const subIncludeByKey = {};
|
|
33
|
+
let keysToFormat;
|
|
34
|
+
if (include.length) {
|
|
35
|
+
keysToFormat = include.map((includePath) => {
|
|
36
|
+
const useIncludePath = includePath.replace(cleanPathStartExpression, "").replace(cleanPathEndExpression, "");
|
|
37
|
+
let key;
|
|
38
|
+
if (useIncludePath.includes(pathDelimiter)) {
|
|
39
|
+
const pathParts = useIncludePath.split(pathDelimiter);
|
|
40
|
+
const pathKey = pathParts.shift();
|
|
41
|
+
key = "";
|
|
42
|
+
if (!subIncludeByKey[pathKey]) {
|
|
43
|
+
subIncludeByKey[pathKey] = [];
|
|
44
|
+
}
|
|
45
|
+
subIncludeByKey[pathKey].push(pathParts.join(pathDelimiter));
|
|
46
|
+
} else {
|
|
47
|
+
key = useIncludePath;
|
|
48
|
+
}
|
|
49
|
+
return key;
|
|
50
|
+
}).filter((key) => key);
|
|
51
|
+
} else {
|
|
52
|
+
keysToFormat = keys;
|
|
53
|
+
}
|
|
54
|
+
const subExcludeByKey = {};
|
|
55
|
+
if (exclude.length) {
|
|
56
|
+
const keysToRemove = [];
|
|
57
|
+
exclude.forEach((excludePath) => {
|
|
58
|
+
const useExcludePath = excludePath.replace(cleanPathStartExpression, "").replace(cleanPathEndExpression, "");
|
|
59
|
+
if (useExcludePath.includes(pathDelimiter)) {
|
|
60
|
+
const pathParts = useExcludePath.split(pathDelimiter);
|
|
61
|
+
const pathKey = pathParts.shift();
|
|
62
|
+
if (!subExcludeByKey[pathKey]) {
|
|
63
|
+
subExcludeByKey[pathKey] = [];
|
|
64
|
+
}
|
|
65
|
+
subExcludeByKey[pathKey].push(pathParts.join(pathDelimiter));
|
|
66
|
+
} else {
|
|
67
|
+
hasChildrenByKey[useExcludePath] = false;
|
|
68
|
+
keysToRemove.push(useExcludePath);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
keysToFormat = keysToFormat.filter((key) => key && !keysToRemove.includes(key));
|
|
72
|
+
}
|
|
73
|
+
return keys.reduce((newObj, key) => {
|
|
74
|
+
let newKey;
|
|
75
|
+
let newValue;
|
|
76
|
+
const value = useTarget[key];
|
|
77
|
+
if (keysToFormat.includes(key)) {
|
|
78
|
+
newKey = key.replace(search, replace);
|
|
79
|
+
} else {
|
|
80
|
+
newKey = key;
|
|
81
|
+
}
|
|
82
|
+
if (hasChildrenByKey[key]) {
|
|
83
|
+
newValue = formatKeys({
|
|
84
|
+
target: value,
|
|
85
|
+
search,
|
|
86
|
+
replace,
|
|
87
|
+
include: subIncludeByKey[key] || [],
|
|
88
|
+
exclude: subExcludeByKey[key] || [],
|
|
89
|
+
pathDelimiter
|
|
90
|
+
});
|
|
91
|
+
} else {
|
|
92
|
+
newValue = value;
|
|
93
|
+
}
|
|
94
|
+
return { ...newObj, [newKey]: newValue };
|
|
95
|
+
}, {});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/fns/dashToLowerCamelKeys.ts
|
|
99
|
+
var dashToLowerCamelKeys = (options) => formatKeys({
|
|
100
|
+
...options,
|
|
101
|
+
search: /([a-z])-([a-z])/g,
|
|
102
|
+
replace: (_, firstLetter, secondLetter) => {
|
|
103
|
+
const newSecondLetter = secondLetter.toUpperCase();
|
|
104
|
+
return `${firstLetter}${newSecondLetter}`;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// src/fns/dashToSnakeKeys.ts
|
|
109
|
+
var dashToSnakeKeys = (options) => formatKeys({
|
|
110
|
+
...options,
|
|
111
|
+
search: /([a-z])-([a-z])/g,
|
|
112
|
+
replace: (_, firstLetter, secondLetter) => `${firstLetter}_${secondLetter}`
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// src/fns/get.ts
|
|
116
|
+
var get = (options) => {
|
|
117
|
+
const { target, path, pathDelimiter = ".", failWithError = false } = options;
|
|
118
|
+
const useTarget = target;
|
|
119
|
+
const parts = path.split(pathDelimiter);
|
|
120
|
+
const first = parts.shift();
|
|
121
|
+
let currentElement = useTarget[first];
|
|
122
|
+
if (typeof currentElement === "undefined") {
|
|
123
|
+
if (failWithError) {
|
|
124
|
+
throw new Error(`There's nothing on '${path}'`);
|
|
125
|
+
}
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
if (!parts.length) {
|
|
129
|
+
return currentElement;
|
|
130
|
+
}
|
|
131
|
+
let currentPath = first;
|
|
132
|
+
parts.some((currentPart) => {
|
|
133
|
+
let breakLoop = false;
|
|
134
|
+
currentPath += `${pathDelimiter}${currentPart}`;
|
|
135
|
+
currentElement = currentElement[currentPart];
|
|
136
|
+
if (typeof currentElement === "undefined") {
|
|
137
|
+
if (failWithError) {
|
|
138
|
+
throw new Error(`There's nothing on '${currentPath}'`);
|
|
139
|
+
} else {
|
|
140
|
+
breakLoop = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return breakLoop;
|
|
144
|
+
});
|
|
145
|
+
return currentElement;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// src/fns/set.ts
|
|
149
|
+
var set = (options) => {
|
|
150
|
+
const { target, path, value, pathDelimiter = ".", failWithError = false } = options;
|
|
151
|
+
const result = copy(target);
|
|
152
|
+
if (!path.includes(pathDelimiter)) {
|
|
153
|
+
result[path] = value;
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
const parts = path.split(pathDelimiter);
|
|
157
|
+
const last = parts.pop();
|
|
158
|
+
let currentElement = result;
|
|
159
|
+
let currentPath = "";
|
|
160
|
+
let foundInvalid = false;
|
|
161
|
+
parts.forEach((part) => {
|
|
162
|
+
currentPath += `${pathDelimiter}${part}`;
|
|
163
|
+
const element = currentElement[part];
|
|
164
|
+
const elementType = typeof element;
|
|
165
|
+
if (elementType === "undefined") {
|
|
166
|
+
currentElement[part] = {};
|
|
167
|
+
currentElement = currentElement[part];
|
|
168
|
+
} else if (elementType === "object") {
|
|
169
|
+
currentElement = currentElement[part];
|
|
170
|
+
} else {
|
|
171
|
+
const errorPath = currentPath.substring(pathDelimiter.length);
|
|
172
|
+
if (failWithError) {
|
|
173
|
+
throw new Error(`There's already an element of type '${elementType}' on '${errorPath}'`);
|
|
174
|
+
} else {
|
|
175
|
+
foundInvalid = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
if (foundInvalid)
|
|
180
|
+
return void 0;
|
|
181
|
+
if (result && last) {
|
|
182
|
+
currentElement[last] = value;
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/fns/extract.ts
|
|
188
|
+
var extract = (options) => {
|
|
189
|
+
const { target, paths, pathDelimiter = ".", failWithError = false } = options;
|
|
190
|
+
const copied = copy(target);
|
|
191
|
+
let result = {};
|
|
192
|
+
let failed = false;
|
|
193
|
+
(Array.isArray(paths) ? paths : [paths]).reduce((acc, objPath) => {
|
|
194
|
+
let destPath;
|
|
195
|
+
let originPath;
|
|
196
|
+
if (typeof objPath === "string") {
|
|
197
|
+
destPath = objPath;
|
|
198
|
+
originPath = objPath;
|
|
199
|
+
} else {
|
|
200
|
+
const [firstKey] = Object.keys(objPath);
|
|
201
|
+
destPath = firstKey;
|
|
202
|
+
originPath = objPath[destPath];
|
|
203
|
+
}
|
|
204
|
+
acc.push({
|
|
205
|
+
origin: originPath,
|
|
206
|
+
customDest: destPath.includes(pathDelimiter),
|
|
207
|
+
dest: destPath
|
|
208
|
+
});
|
|
209
|
+
return acc;
|
|
210
|
+
}, []).some((pathInfo) => {
|
|
211
|
+
let breakLoop = false;
|
|
212
|
+
const value = get({
|
|
213
|
+
target: copied,
|
|
214
|
+
path: pathInfo.origin,
|
|
215
|
+
pathDelimiter,
|
|
216
|
+
failWithError
|
|
217
|
+
});
|
|
218
|
+
if (typeof value !== "undefined") {
|
|
219
|
+
if (pathInfo.customDest) {
|
|
220
|
+
const withCustomProp = set({
|
|
221
|
+
target: result,
|
|
222
|
+
path: pathInfo.dest,
|
|
223
|
+
value,
|
|
224
|
+
pathDelimiter,
|
|
225
|
+
failWithError
|
|
226
|
+
});
|
|
227
|
+
if (typeof withCustomProp === "undefined") {
|
|
228
|
+
breakLoop = true;
|
|
229
|
+
failed = true;
|
|
230
|
+
} else {
|
|
231
|
+
result = withCustomProp;
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
result[pathInfo.dest] = value;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return breakLoop;
|
|
238
|
+
});
|
|
239
|
+
if (failed)
|
|
240
|
+
return void 0;
|
|
241
|
+
return result;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// src/fns/flat.ts
|
|
245
|
+
var flat = (options) => {
|
|
246
|
+
const { target, shouldFlatten, pathDelimiter = ".", prefix = "" } = options;
|
|
247
|
+
const useTarget = target;
|
|
248
|
+
let result = {};
|
|
249
|
+
const namePrefix = prefix ? `${prefix}${pathDelimiter}` : "";
|
|
250
|
+
Object.keys(useTarget).forEach((key) => {
|
|
251
|
+
const name = `${namePrefix}${key}`;
|
|
252
|
+
const value = useTarget[key];
|
|
253
|
+
const valueType = typeof value;
|
|
254
|
+
const isObject = valueType === "object" && value !== null;
|
|
255
|
+
if (isObject && (!shouldFlatten || shouldFlatten(key, value))) {
|
|
256
|
+
result = merge(result, flat({ target: value, pathDelimiter, prefix: name, shouldFlatten }));
|
|
257
|
+
} else {
|
|
258
|
+
result[name] = isObject ? copy(value) : value;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
return result;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// src/fns/lowerCamelToDashKeys.ts
|
|
265
|
+
var lowerCamelToDashKeys = (options) => formatKeys({
|
|
266
|
+
...options,
|
|
267
|
+
search: /([a-z])([A-Z])/g,
|
|
268
|
+
replace: (_, firstLetter, secondLetter) => {
|
|
269
|
+
const newSecondLetter = secondLetter.toLowerCase();
|
|
270
|
+
return `${firstLetter}-${newSecondLetter}`;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// src/fns/lowerCamelToSnakeKeys.ts
|
|
275
|
+
var lowerCamelToSnakeKeys = (options) => formatKeys({
|
|
276
|
+
...options,
|
|
277
|
+
search: /([a-z])([A-Z])/g,
|
|
278
|
+
replace: (_, firstLetter, secondLetter) => {
|
|
279
|
+
const newSecondLetter = secondLetter.toLowerCase();
|
|
280
|
+
return `${firstLetter}_${newSecondLetter}`;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// src/fns/remove.ts
|
|
285
|
+
var remove = (options) => {
|
|
286
|
+
const {
|
|
287
|
+
target,
|
|
288
|
+
path,
|
|
289
|
+
pathDelimiter = ".",
|
|
290
|
+
cleanEmptyProperties = true,
|
|
291
|
+
failWithError = false
|
|
292
|
+
} = options;
|
|
293
|
+
const parts = path.split(pathDelimiter);
|
|
294
|
+
const last = parts.pop();
|
|
295
|
+
const result = copy(target);
|
|
296
|
+
if (!parts.length) {
|
|
297
|
+
delete result[last];
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
const parentPath = parts.join(pathDelimiter);
|
|
301
|
+
const parentObj = get({
|
|
302
|
+
target: result,
|
|
303
|
+
path: parentPath,
|
|
304
|
+
pathDelimiter,
|
|
305
|
+
failWithError
|
|
306
|
+
});
|
|
307
|
+
if (typeof parentObj === "undefined") {
|
|
308
|
+
return void 0;
|
|
309
|
+
}
|
|
310
|
+
delete parentObj[last];
|
|
311
|
+
if (cleanEmptyProperties && !Object.keys(parentObj).length) {
|
|
312
|
+
return remove({
|
|
313
|
+
target: result,
|
|
314
|
+
path: parentPath,
|
|
315
|
+
pathDelimiter,
|
|
316
|
+
failWithError,
|
|
317
|
+
cleanEmptyProperties
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
// src/fns/snakeToDashKeys.ts
|
|
324
|
+
var snakeToDashKeys = (options) => formatKeys({
|
|
325
|
+
...options,
|
|
326
|
+
search: /([a-z])_([a-z])/g,
|
|
327
|
+
replace: (_, firstLetter, secondLetter) => `${firstLetter}-${secondLetter}`
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// src/fns/snakeToLowerCamelKeys.ts
|
|
331
|
+
var snakeToLowerCamelKeys = (options) => formatKeys({
|
|
332
|
+
...options,
|
|
333
|
+
search: /([a-z])_([a-z])/g,
|
|
334
|
+
replace: (_, firstLetter, secondLetter) => {
|
|
335
|
+
const newSecondLetter = secondLetter.toUpperCase();
|
|
336
|
+
return `${firstLetter}${newSecondLetter}`;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// src/fns/unflat.ts
|
|
341
|
+
var unflat = (options) => {
|
|
342
|
+
const { target, pathDelimiter = "." } = options;
|
|
343
|
+
const useTarget = target;
|
|
344
|
+
return Object.keys(useTarget).reduce((current, key) => set({ target: current, path: key, value: useTarget[key], pathDelimiter }), {});
|
|
345
|
+
};
|
|
346
|
+
export {
|
|
347
|
+
copy,
|
|
348
|
+
dashToLowerCamelKeys,
|
|
349
|
+
dashToSnakeKeys,
|
|
350
|
+
extract,
|
|
351
|
+
flat,
|
|
352
|
+
formatKeys,
|
|
353
|
+
get,
|
|
354
|
+
lowerCamelToDashKeys,
|
|
355
|
+
lowerCamelToSnakeKeys,
|
|
356
|
+
merge,
|
|
357
|
+
remove,
|
|
358
|
+
set,
|
|
359
|
+
snakeToDashKeys,
|
|
360
|
+
snakeToLowerCamelKeys,
|
|
361
|
+
unflat
|
|
362
|
+
};
|
|
363
|
+
//# sourceMappingURL=index.js.map
|