UNPKG

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