source: node_modules/es-toolkit/dist/compat/object/omit.mjs

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[a762898]1import { cloneDeepWith } from './cloneDeepWith.mjs';
2import { keysIn } from './keysIn.mjs';
3import { unset } from './unset.mjs';
4import { getSymbolsIn } from '../_internal/getSymbolsIn.mjs';
5import { isDeepKey } from '../_internal/isDeepKey.mjs';
6import { flatten } from '../array/flatten.mjs';
7import { isPlainObject } from '../predicate/isPlainObject.mjs';
8
9function omit(obj, ...keysArr) {
10 if (obj == null) {
11 return {};
12 }
13 keysArr = flatten(keysArr);
14 const result = cloneInOmit(obj, keysArr);
15 for (let i = 0; i < keysArr.length; i++) {
16 let keys = keysArr[i];
17 switch (typeof keys) {
18 case 'object': {
19 if (!Array.isArray(keys)) {
20 keys = Array.from(keys);
21 }
22 for (let j = 0; j < keys.length; j++) {
23 const key = keys[j];
24 unset(result, key);
25 }
26 break;
27 }
28 case 'string':
29 case 'symbol':
30 case 'number': {
31 unset(result, keys);
32 break;
33 }
34 }
35 }
36 return result;
37}
38function cloneInOmit(obj, keys) {
39 const hasDeepKey = keys.some(key => Array.isArray(key) || isDeepKey(key));
40 if (hasDeepKey) {
41 return deepCloneInOmit(obj);
42 }
43 return shallowCloneInOmit(obj);
44}
45function shallowCloneInOmit(obj) {
46 const result = {};
47 const keysToCopy = [...keysIn(obj), ...getSymbolsIn(obj)];
48 for (let i = 0; i < keysToCopy.length; i++) {
49 const key = keysToCopy[i];
50 result[key] = obj[key];
51 }
52 return result;
53}
54function deepCloneInOmit(obj) {
55 const result = {};
56 const keysToCopy = [...keysIn(obj), ...getSymbolsIn(obj)];
57 for (let i = 0; i < keysToCopy.length; i++) {
58 const key = keysToCopy[i];
59 result[key] = cloneDeepWith(obj[key], valueToClone => {
60 if (isPlainObject(valueToClone)) {
61 return undefined;
62 }
63 return valueToClone;
64 });
65 }
66 return result;
67}
68
69export { omit };
Note: See TracBrowser for help on using the repository browser.