UNPKG

9.17 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var OrmUtils = /** @class */ (function () {
5 function OrmUtils() {
6 }
7 // -------------------------------------------------------------------------
8 // Public methods
9 // -------------------------------------------------------------------------
10 /**
11 * Chunks array into peaces.
12 */
13 OrmUtils.chunk = function (array, size) {
14 return Array.from(Array(Math.ceil(array.length / size)), function (_, i) {
15 return array.slice(i * size, i * size + size);
16 });
17 };
18 OrmUtils.splitClassesAndStrings = function (clsesAndStrings) {
19 return [
20 (clsesAndStrings).filter(function (cls) { return typeof cls !== "string"; }),
21 (clsesAndStrings).filter(function (str) { return typeof str === "string"; }),
22 ];
23 };
24 OrmUtils.groupBy = function (array, propertyCallback) {
25 return array.reduce(function (groupedArray, value) {
26 var key = propertyCallback(value);
27 var grouped = groupedArray.find(function (i) { return i.id === key; });
28 if (!grouped) {
29 grouped = { id: key, items: [] };
30 groupedArray.push(grouped);
31 }
32 grouped.items.push(value);
33 return groupedArray;
34 }, []);
35 };
36 OrmUtils.uniq = function (array, criteriaOrProperty) {
37 return array.reduce(function (uniqueArray, item) {
38 var found = false;
39 if (criteriaOrProperty instanceof Function) {
40 var itemValue_1 = criteriaOrProperty(item);
41 found = !!uniqueArray.find(function (uniqueItem) { return criteriaOrProperty(uniqueItem) === itemValue_1; });
42 }
43 else if (typeof criteriaOrProperty === "string") {
44 found = !!uniqueArray.find(function (uniqueItem) { return uniqueItem[criteriaOrProperty] === item[criteriaOrProperty]; });
45 }
46 else {
47 found = uniqueArray.indexOf(item) !== -1;
48 }
49 if (!found)
50 uniqueArray.push(item);
51 return uniqueArray;
52 }, []);
53 };
54 OrmUtils.isObject = function (item) {
55 return (item && typeof item === "object" && !Array.isArray(item));
56 };
57 /**
58 * Deep Object.assign.
59 *
60 * @see http://stackoverflow.com/a/34749873
61 */
62 OrmUtils.mergeDeep = function (target) {
63 var sources = [];
64 for (var _i = 1; _i < arguments.length; _i++) {
65 sources[_i - 1] = arguments[_i];
66 }
67 var _a, _b;
68 if (!sources.length)
69 return target;
70 var source = sources.shift();
71 if (this.isObject(target) && this.isObject(source)) {
72 for (var key in source) {
73 var propertyKey = key;
74 if (source[key] instanceof Promise)
75 continue;
76 // if (source[key] instanceof Promise) {
77 // propertyKey = "__" + key + "__";
78 // }
79 if (this.isObject(source[propertyKey])
80 && !(source[propertyKey] instanceof Map)
81 && !(source[propertyKey] instanceof Set)
82 && !(source[propertyKey] instanceof Date)
83 && !(source[propertyKey] instanceof Buffer)) {
84 if (!target[key])
85 Object.assign(target, (_a = {}, _a[key] = Object.create(Object.getPrototypeOf(source[propertyKey])), _a));
86 this.mergeDeep(target[key], source[propertyKey]);
87 }
88 else {
89 Object.assign(target, (_b = {}, _b[key] = source[propertyKey], _b));
90 }
91 }
92 }
93 return this.mergeDeep.apply(this, tslib_1.__spread([target], sources));
94 };
95 /**
96 * Deep compare objects.
97 *
98 * @see http://stackoverflow.com/a/1144249
99 */
100 OrmUtils.deepCompare = function () {
101 var args = [];
102 for (var _i = 0; _i < arguments.length; _i++) {
103 args[_i] = arguments[_i];
104 }
105 var i, l, leftChain, rightChain;
106 if (arguments.length < 1) {
107 return true; // Die silently? Don't know how to handle such case, please help...
108 // throw "Need two or more arguments to compare";
109 }
110 for (i = 1, l = arguments.length; i < l; i++) {
111 leftChain = []; // Todo: this can be cached
112 rightChain = [];
113 if (!this.compare2Objects(leftChain, rightChain, arguments[0], arguments[i])) {
114 return false;
115 }
116 }
117 return true;
118 };
119 /**
120 * Transforms given value into boolean value.
121 */
122 OrmUtils.toBoolean = function (value) {
123 if (typeof value === "boolean")
124 return value;
125 if (typeof value === "string")
126 return value === "true" || value === "1";
127 if (typeof value === "number")
128 return value > 0;
129 return false;
130 };
131 /**
132 * Composes an object from the given array of keys and values.
133 */
134 OrmUtils.zipObject = function (keys, values) {
135 return keys.reduce(function (object, column, index) {
136 object[column] = values[index];
137 return object;
138 }, {});
139 };
140 /**
141 * Compares two arrays.
142 */
143 OrmUtils.isArraysEqual = function (arr1, arr2) {
144 if (arr1.length !== arr2.length)
145 return false;
146 return arr1.every(function (element) {
147 return arr2.indexOf(element) !== -1;
148 });
149 };
150 // -------------------------------------------------------------------------
151 // Private methods
152 // -------------------------------------------------------------------------
153 OrmUtils.compare2Objects = function (leftChain, rightChain, x, y) {
154 var p;
155 // remember that NaN === NaN returns false
156 // and isNaN(undefined) returns true
157 if (isNaN(x) && isNaN(y) && typeof x === "number" && typeof y === "number")
158 return true;
159 // Compare primitives and functions.
160 // Check if both arguments link to the same object.
161 // Especially useful on the step where we compare prototypes
162 if (x === y)
163 return true;
164 // Unequal, but either is null or undefined (use case: jsonb comparasion)
165 // PR #3776, todo: add tests
166 if (x === null || y === null || x === undefined || y === undefined)
167 return false;
168 // Fix the buffer compare bug.
169 // See: https://github.com/typeorm/typeorm/issues/3654
170 if ((typeof x.equals === "function" || x.equals instanceof Function) && x.equals(y))
171 return true;
172 // Works in case when functions are created in constructor.
173 // Comparing dates is a common scenario. Another built-ins?
174 // We can even handle functions passed across iframes
175 if ((typeof x === "function" && typeof y === "function") ||
176 (x instanceof Date && y instanceof Date) ||
177 (x instanceof RegExp && y instanceof RegExp) ||
178 (x instanceof String && y instanceof String) ||
179 (x instanceof Number && y instanceof Number))
180 return x.toString() === y.toString();
181 // At last checking prototypes as good as we can
182 if (!(x instanceof Object && y instanceof Object))
183 return false;
184 if (x.isPrototypeOf(y) || y.isPrototypeOf(x))
185 return false;
186 if (x.constructor !== y.constructor)
187 return false;
188 if (x.prototype !== y.prototype)
189 return false;
190 // Check for infinitive linking loops
191 if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1)
192 return false;
193 // Quick checking of one object being a subset of another.
194 // todo: cache the structure of arguments[0] for performance
195 for (p in y) {
196 if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
197 return false;
198 }
199 else if (typeof y[p] !== typeof x[p]) {
200 return false;
201 }
202 }
203 for (p in x) {
204 if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
205 return false;
206 }
207 else if (typeof y[p] !== typeof x[p]) {
208 return false;
209 }
210 switch (typeof (x[p])) {
211 case "object":
212 case "function":
213 leftChain.push(x);
214 rightChain.push(y);
215 if (!this.compare2Objects(leftChain, rightChain, x[p], y[p])) {
216 return false;
217 }
218 leftChain.pop();
219 rightChain.pop();
220 break;
221 default:
222 if (x[p] !== y[p]) {
223 return false;
224 }
225 break;
226 }
227 }
228 return true;
229 };
230 return OrmUtils;
231}());
232exports.OrmUtils = OrmUtils;
233
234//# sourceMappingURL=OrmUtils.js.map