1 | /**
|
2 | * Compares a to b and b to a.
|
3 | *
|
4 | * @public
|
5 | */
|
6 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
7 | export function shallowCompare(a, b) {
|
8 | for (var propName in a) {
|
9 | if (a.hasOwnProperty(propName)) {
|
10 | if (!b.hasOwnProperty(propName) || b[propName] !== a[propName]) {
|
11 | return false;
|
12 | }
|
13 | }
|
14 | }
|
15 | for (var propName in b) {
|
16 | if (b.hasOwnProperty(propName)) {
|
17 | if (!a.hasOwnProperty(propName)) {
|
18 | return false;
|
19 | }
|
20 | }
|
21 | }
|
22 | return true;
|
23 | }
|
24 | /**
|
25 | * Makes a resulting merge of a bunch of objects. Pass in the target object followed by 1 or more
|
26 | * objects as arguments and they will be merged sequentially into the target. Note that this will
|
27 | * shallow merge; it will not create new cloned values for target members.
|
28 | *
|
29 | * @public
|
30 | * @param target - Target object to merge following object arguments into.
|
31 | * @param args - One or more objects that will be mixed into the target in the order they are provided.
|
32 | * @returns Resulting merged target.
|
33 | */
|
34 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
35 | export function assign(target) {
|
36 | var args = [];
|
37 | for (var _i = 1; _i < arguments.length; _i++) {
|
38 | args[_i - 1] = arguments[_i];
|
39 | }
|
40 | return filteredAssign.apply(this, [null, target].concat(args));
|
41 | }
|
42 | /**
|
43 | * Makes a resulting merge of a bunch of objects, but allows a filter function to be passed in to filter
|
44 | * the resulting merges. This allows for scenarios where you want to merge "everything except that one thing"
|
45 | * or "properties that start with data-". Note that this will shallow merge; it will not create new cloned
|
46 | * values for target members.
|
47 | *
|
48 | * @public
|
49 | * @param isAllowed - Callback to determine if the given propName is allowed in the result.
|
50 | * @param target - Target object to merge following object arguments into.
|
51 | * @param args - One or more objects that will be mixed into the target in the order they are provided.
|
52 | * @returns Resulting merged target.
|
53 | */
|
54 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
55 | export function filteredAssign(isAllowed, target) {
|
56 | var args = [];
|
57 | for (var _i = 2; _i < arguments.length; _i++) {
|
58 | args[_i - 2] = arguments[_i];
|
59 | }
|
60 | target = target || {};
|
61 | for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
|
62 | var sourceObject = args_1[_a];
|
63 | if (sourceObject) {
|
64 | for (var propName in sourceObject) {
|
65 | if (sourceObject.hasOwnProperty(propName) && (!isAllowed || isAllowed(propName))) {
|
66 | target[propName] = sourceObject[propName];
|
67 | }
|
68 | }
|
69 | }
|
70 | }
|
71 | return target;
|
72 | }
|
73 | /**
|
74 | * Takes an enum and iterates over each value of the enum (as a string), running the callback on each,
|
75 | * returning a mapped array.
|
76 | * @param theEnum - Enum to iterate over
|
77 | * @param callback - The first parameter the name of the entry, and the second parameter is the value
|
78 | * of that entry, which is the value you'd normally use when using the enum (usually a number).
|
79 | */
|
80 | export function mapEnumByName(
|
81 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
82 | theEnum, callback) {
|
83 | // map<any> to satisfy compiler since it doesn't realize we strip out undefineds in the .filter() call
|
84 | return Object.keys(theEnum)
|
85 | .map(function (p) {
|
86 | // map on each property name as a string
|
87 | if (String(Number(p)) !== p) {
|
88 | // if the property is not just a number (because enums in TypeScript will map both ways)
|
89 | return callback(p, theEnum[p]);
|
90 | }
|
91 | return undefined;
|
92 | })
|
93 | .filter(function (v) { return !!v; }); // only return elements with values
|
94 | }
|
95 | /**
|
96 | * Get all values in an object dictionary
|
97 | *
|
98 | * @param obj - The dictionary to get values for
|
99 | */
|
100 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
101 | export function values(obj) {
|
102 | return Object.keys(obj).reduce(function (arr, key) {
|
103 | arr.push(obj[key]);
|
104 | return arr;
|
105 | }, []);
|
106 | }
|
107 | /**
|
108 | * Tiny helper to do the minimal amount of work in duplicating an object but omitting some
|
109 | * props. This ends up faster than using object ...rest or reduce to filter.
|
110 | *
|
111 | * This behaves very much like filteredAssign, but does not merge many objects together,
|
112 | * uses an exclusion object map, and avoids spreads all for optimal performance.
|
113 | *
|
114 | * See perf test for background:
|
115 | * https://jsperf.com/omit-vs-rest-vs-reduce/1
|
116 | *
|
117 | * @param obj - The object to clone
|
118 | * @param exclusions - The array of keys to exclude
|
119 | */
|
120 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
121 | export function omit(obj, exclusions) {
|
122 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
123 | var result = {};
|
124 | for (var key in obj) {
|
125 | if (exclusions.indexOf(key) === -1 && obj.hasOwnProperty(key)) {
|
126 | result[key] = obj[key];
|
127 | }
|
128 | }
|
129 | return result;
|
130 | }
|
131 | //# sourceMappingURL=object.js.map |
\ | No newline at end of file |