UNPKG

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