es-toolkit 1.34.1-dev.1158 → 1.34.1-dev.1159

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.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const isPlainObject = require('./isPlainObject-DwqRvh.js');
3
+ const isPlainObject = require('./isPlainObject-Xaozpc.js');
4
4
  const noop = require('./noop-2IwLUk.js');
5
5
 
6
6
  function isLength(value) {
@@ -1,11 +1,48 @@
1
1
  'use strict';
2
2
 
3
- const isPlainObject$1 = require('./isPlainObject-DwqRvh.js');
3
+ const isPlainObject$1 = require('./isPlainObject-Xaozpc.js');
4
4
  require('./partialRight-B0_CSB.js');
5
5
  const snakeCase = require('./snakeCase-BtVEeB.js');
6
6
 
7
- function isObjectLike(value) {
8
- return typeof value === 'object' && value !== null;
7
+ function clone(obj) {
8
+ if (isPlainObject$1.isPrimitive(obj)) {
9
+ return obj;
10
+ }
11
+ if (Array.isArray(obj) ||
12
+ isPlainObject$1.isTypedArray(obj) ||
13
+ obj instanceof ArrayBuffer ||
14
+ (typeof SharedArrayBuffer !== 'undefined' && obj instanceof SharedArrayBuffer)) {
15
+ return obj.slice(0);
16
+ }
17
+ const prototype = Object.getPrototypeOf(obj);
18
+ const Constructor = prototype.constructor;
19
+ if (obj instanceof Date || obj instanceof Map || obj instanceof Set) {
20
+ return new Constructor(obj);
21
+ }
22
+ if (obj instanceof RegExp) {
23
+ const newRegExp = new Constructor(obj);
24
+ newRegExp.lastIndex = obj.lastIndex;
25
+ return newRegExp;
26
+ }
27
+ if (obj instanceof DataView) {
28
+ return new Constructor(obj.buffer.slice(0));
29
+ }
30
+ if (obj instanceof Error) {
31
+ const newError = new Constructor(obj.message);
32
+ newError.stack = obj.stack;
33
+ newError.name = obj.name;
34
+ newError.cause = obj.cause;
35
+ return newError;
36
+ }
37
+ if (typeof File !== 'undefined' && obj instanceof File) {
38
+ const newFile = new Constructor([obj], obj.name, { type: obj.type, lastModified: obj.lastModified });
39
+ return newFile;
40
+ }
41
+ if (typeof obj === 'object') {
42
+ const newObject = Object.create(prototype);
43
+ return Object.assign(newObject, obj);
44
+ }
45
+ return obj;
9
46
  }
10
47
 
11
48
  function cloneDeepWith(obj, cloneValue) {
@@ -159,6 +196,113 @@ function cloneDeep(obj) {
159
196
  return cloneDeepWithImpl(obj, undefined, obj, new Map(), undefined);
160
197
  }
161
198
 
199
+ function findKey(obj, predicate) {
200
+ const keys = Object.keys(obj);
201
+ return keys.find(key => predicate(obj[key], key, obj));
202
+ }
203
+
204
+ function flattenObject(object, { delimiter = '.' } = {}) {
205
+ return flattenObjectImpl(object, '', delimiter);
206
+ }
207
+ function flattenObjectImpl(object, prefix = '', delimiter = '.') {
208
+ const result = {};
209
+ const keys = Object.keys(object);
210
+ for (let i = 0; i < keys.length; i++) {
211
+ const key = keys[i];
212
+ const value = object[key];
213
+ const prefixedKey = prefix ? `${prefix}${delimiter}${key}` : key;
214
+ if (isPlainObject$1.isPlainObject(value) && Object.keys(value).length > 0) {
215
+ Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
216
+ continue;
217
+ }
218
+ if (Array.isArray(value)) {
219
+ Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
220
+ continue;
221
+ }
222
+ result[prefixedKey] = value;
223
+ }
224
+ return result;
225
+ }
226
+
227
+ function invert(obj) {
228
+ const result = {};
229
+ const keys = Object.keys(obj);
230
+ for (let i = 0; i < keys.length; i++) {
231
+ const key = keys[i];
232
+ const value = obj[key];
233
+ result[value] = key;
234
+ }
235
+ return result;
236
+ }
237
+
238
+ function mapKeys(object, getNewKey) {
239
+ const result = {};
240
+ const keys = Object.keys(object);
241
+ for (let i = 0; i < keys.length; i++) {
242
+ const key = keys[i];
243
+ const value = object[key];
244
+ result[getNewKey(value, key, object)] = value;
245
+ }
246
+ return result;
247
+ }
248
+
249
+ function mapValues(object, getNewValue) {
250
+ const result = {};
251
+ const keys = Object.keys(object);
252
+ for (let i = 0; i < keys.length; i++) {
253
+ const key = keys[i];
254
+ const value = object[key];
255
+ result[key] = getNewValue(value, key, object);
256
+ }
257
+ return result;
258
+ }
259
+
260
+ function merge(target, source) {
261
+ const sourceKeys = Object.keys(source);
262
+ for (let i = 0; i < sourceKeys.length; i++) {
263
+ const key = sourceKeys[i];
264
+ const sourceValue = source[key];
265
+ const targetValue = target[key];
266
+ if (Array.isArray(sourceValue)) {
267
+ if (Array.isArray(targetValue)) {
268
+ target[key] = merge(targetValue, sourceValue);
269
+ }
270
+ else {
271
+ target[key] = merge([], sourceValue);
272
+ }
273
+ }
274
+ else if (isPlainObject$1.isPlainObject(sourceValue)) {
275
+ if (isPlainObject$1.isPlainObject(targetValue)) {
276
+ target[key] = merge(targetValue, sourceValue);
277
+ }
278
+ else {
279
+ target[key] = merge({}, sourceValue);
280
+ }
281
+ }
282
+ else if (targetValue === undefined || sourceValue !== undefined) {
283
+ target[key] = sourceValue;
284
+ }
285
+ }
286
+ return target;
287
+ }
288
+
289
+ function isObjectLike(value) {
290
+ return typeof value === 'object' && value !== null;
291
+ }
292
+
293
+ function omitBy(obj, shouldOmit) {
294
+ const result = {};
295
+ const keys = Object.keys(obj);
296
+ for (let i = 0; i < keys.length; i++) {
297
+ const key = keys[i];
298
+ const value = obj[key];
299
+ if (!shouldOmit(value, key)) {
300
+ result[key] = value;
301
+ }
302
+ }
303
+ return result;
304
+ }
305
+
162
306
  function isArray(value) {
163
307
  return Array.isArray(value);
164
308
  }
@@ -334,74 +478,6 @@ function composeArgs(providedArgs, partialArgs) {
334
478
  const curryRightPlaceholder = Symbol('curryRight.placeholder');
335
479
  curryRight.placeholder = curryRightPlaceholder;
336
480
 
337
- function findKey(obj, predicate) {
338
- const keys = Object.keys(obj);
339
- return keys.find(key => predicate(obj[key], key, obj));
340
- }
341
-
342
- function mapKeys(object, getNewKey) {
343
- const result = {};
344
- const keys = Object.keys(object);
345
- for (let i = 0; i < keys.length; i++) {
346
- const key = keys[i];
347
- const value = object[key];
348
- result[getNewKey(value, key, object)] = value;
349
- }
350
- return result;
351
- }
352
-
353
- function mapValues(object, getNewValue) {
354
- const result = {};
355
- const keys = Object.keys(object);
356
- for (let i = 0; i < keys.length; i++) {
357
- const key = keys[i];
358
- const value = object[key];
359
- result[key] = getNewValue(value, key, object);
360
- }
361
- return result;
362
- }
363
-
364
- function clone(obj) {
365
- if (isPlainObject$1.isPrimitive(obj)) {
366
- return obj;
367
- }
368
- if (Array.isArray(obj) ||
369
- isPlainObject$1.isTypedArray(obj) ||
370
- obj instanceof ArrayBuffer ||
371
- (typeof SharedArrayBuffer !== 'undefined' && obj instanceof SharedArrayBuffer)) {
372
- return obj.slice(0);
373
- }
374
- const prototype = Object.getPrototypeOf(obj);
375
- const Constructor = prototype.constructor;
376
- if (obj instanceof Date || obj instanceof Map || obj instanceof Set) {
377
- return new Constructor(obj);
378
- }
379
- if (obj instanceof RegExp) {
380
- const newRegExp = new Constructor(obj);
381
- newRegExp.lastIndex = obj.lastIndex;
382
- return newRegExp;
383
- }
384
- if (obj instanceof DataView) {
385
- return new Constructor(obj.buffer.slice(0));
386
- }
387
- if (obj instanceof Error) {
388
- const newError = new Constructor(obj.message);
389
- newError.stack = obj.stack;
390
- newError.name = obj.name;
391
- newError.cause = obj.cause;
392
- return newError;
393
- }
394
- if (typeof File !== 'undefined' && obj instanceof File) {
395
- const newFile = new Constructor([obj], obj.name, { type: obj.type, lastModified: obj.lastModified });
396
- return newFile;
397
- }
398
- if (typeof obj === 'object') {
399
- const newObject = Object.create(prototype);
400
- return Object.assign(newObject, obj);
401
- }
402
- return obj;
403
- }
404
-
405
481
  function isPlainObject(object) {
406
482
  if (typeof object !== 'object') {
407
483
  return false;
@@ -430,135 +506,48 @@ function isPlainObject(object) {
430
506
  return Object.getPrototypeOf(object) === proto;
431
507
  }
432
508
 
433
- function camelizeKeys(obj) {
509
+ function toCamelCaseKeys(obj) {
434
510
  if (isArray(obj)) {
435
- return obj.map(item => camelizeKeys(item));
511
+ return obj.map(item => toCamelCaseKeys(item));
436
512
  }
437
513
  else if (isPlainObject(obj)) {
438
514
  const result = {};
439
- const defaultObjectKeys = new Set(Object.getOwnPropertyNames(Object.prototype));
440
- const plainObject = obj;
441
- const keys = Object.keys(plainObject);
515
+ const keys = Object.keys(obj);
442
516
  for (let i = 0; i < keys.length; i++) {
443
517
  const key = keys[i];
444
- if (defaultObjectKeys.has(key)) {
445
- result[key] = plainObject[key];
446
- continue;
447
- }
448
518
  const camelKey = snakeCase.camelCase(key);
449
- result[camelKey] = camelizeKeys(plainObject[key]);
519
+ const camelCaseKeys = toCamelCaseKeys(obj[key]);
520
+ result[camelKey] = camelCaseKeys;
450
521
  }
451
522
  return result;
452
523
  }
453
524
  return obj;
454
525
  }
455
526
 
456
- function flattenObject(object, { delimiter = '.' } = {}) {
457
- return flattenObjectImpl(object, '', delimiter);
458
- }
459
- function flattenObjectImpl(object, prefix = '', delimiter = '.') {
460
- const result = {};
461
- const keys = Object.keys(object);
462
- for (let i = 0; i < keys.length; i++) {
463
- const key = keys[i];
464
- const value = object[key];
465
- const prefixedKey = prefix ? `${prefix}${delimiter}${key}` : key;
466
- if (isPlainObject$1.isPlainObject(value) && Object.keys(value).length > 0) {
467
- Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
468
- continue;
469
- }
470
- if (Array.isArray(value)) {
471
- Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
472
- continue;
473
- }
474
- result[prefixedKey] = value;
475
- }
476
- return result;
477
- }
478
-
479
- function invert(obj) {
480
- const result = {};
481
- const keys = Object.keys(obj);
482
- for (let i = 0; i < keys.length; i++) {
483
- const key = keys[i];
484
- const value = obj[key];
485
- result[value] = key;
486
- }
487
- return result;
488
- }
489
-
490
- function merge(target, source) {
491
- const sourceKeys = Object.keys(source);
492
- for (let i = 0; i < sourceKeys.length; i++) {
493
- const key = sourceKeys[i];
494
- const sourceValue = source[key];
495
- const targetValue = target[key];
496
- if (Array.isArray(sourceValue)) {
497
- if (Array.isArray(targetValue)) {
498
- target[key] = merge(targetValue, sourceValue);
499
- }
500
- else {
501
- target[key] = merge([], sourceValue);
502
- }
503
- }
504
- else if (isPlainObject$1.isPlainObject(sourceValue)) {
505
- if (isPlainObject$1.isPlainObject(targetValue)) {
506
- target[key] = merge(targetValue, sourceValue);
507
- }
508
- else {
509
- target[key] = merge({}, sourceValue);
510
- }
511
- }
512
- else if (targetValue === undefined || sourceValue !== undefined) {
513
- target[key] = sourceValue;
514
- }
515
- }
516
- return target;
517
- }
518
-
519
- function omitBy(obj, shouldOmit) {
520
- const result = {};
521
- const keys = Object.keys(obj);
522
- for (let i = 0; i < keys.length; i++) {
523
- const key = keys[i];
524
- const value = obj[key];
525
- if (!shouldOmit(value, key)) {
526
- result[key] = value;
527
- }
528
- }
529
- return result;
527
+ function toMerged(target, source) {
528
+ return merge(cloneDeep(target), source);
530
529
  }
531
530
 
532
- function snakeizeKeys(obj) {
531
+ function toSnakeCaseKeys(obj) {
533
532
  if (isArray(obj)) {
534
- return obj.map(item => snakeizeKeys(item));
533
+ return obj.map(item => toSnakeCaseKeys(item));
535
534
  }
536
535
  else if (isPlainObject(obj)) {
537
536
  const result = {};
538
- const plainObject = obj;
539
- const defaultObjectKeys = new Set(Object.getOwnPropertyNames(Object.prototype));
540
- const keys = Object.keys(plainObject);
537
+ const keys = Object.keys(obj);
541
538
  for (let i = 0; i < keys.length; i++) {
542
539
  const key = keys[i];
543
- if (defaultObjectKeys.has(key)) {
544
- result[key] = plainObject[key];
545
- continue;
546
- }
547
540
  const snakeKey = snakeCase.snakeCase(key);
548
- result[snakeKey] = snakeizeKeys(plainObject[key]);
541
+ const snakeCaseKeys = toSnakeCaseKeys(obj[key]);
542
+ result[snakeKey] = snakeCaseKeys;
549
543
  }
550
544
  return result;
551
545
  }
552
546
  return obj;
553
547
  }
554
548
 
555
- function toMerged(target, source) {
556
- return merge(cloneDeep(target), source);
557
- }
558
-
559
549
  exports.bind = bind;
560
550
  exports.bindKey = bindKey;
561
- exports.camelizeKeys = camelizeKeys;
562
551
  exports.clone = clone;
563
552
  exports.cloneDeep = cloneDeep;
564
553
  exports.cloneDeepWith = cloneDeepWith;
@@ -575,5 +564,6 @@ exports.mapKeys = mapKeys;
575
564
  exports.mapValues = mapValues;
576
565
  exports.merge = merge;
577
566
  exports.omitBy = omitBy;
578
- exports.snakeizeKeys = snakeizeKeys;
567
+ exports.toCamelCaseKeys = toCamelCaseKeys;
579
568
  exports.toMerged = toMerged;
569
+ exports.toSnakeCaseKeys = toSnakeCaseKeys;