1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var OrmUtils = (function () {
|
5 | function OrmUtils() {
|
6 | }
|
7 |
|
8 |
|
9 |
|
10 | |
11 |
|
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 |
|
59 |
|
60 |
|
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 |
|
77 |
|
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 |
|
97 |
|
98 |
|
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;
|
108 |
|
109 | }
|
110 | for (i = 1, l = arguments.length; i < l; i++) {
|
111 | leftChain = [];
|
112 | rightChain = [];
|
113 | if (!this.compare2Objects(leftChain, rightChain, arguments[0], arguments[i])) {
|
114 | return false;
|
115 | }
|
116 | }
|
117 | return true;
|
118 | };
|
119 | |
120 |
|
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 |
|
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 |
|
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 |
|
152 |
|
153 | OrmUtils.compare2Objects = function (leftChain, rightChain, x, y) {
|
154 | var p;
|
155 |
|
156 |
|
157 | if (isNaN(x) && isNaN(y) && typeof x === "number" && typeof y === "number")
|
158 | return true;
|
159 |
|
160 |
|
161 |
|
162 | if (x === y)
|
163 | return true;
|
164 |
|
165 |
|
166 | if (x === null || y === null || x === undefined || y === undefined)
|
167 | return false;
|
168 |
|
169 |
|
170 | if ((typeof x.equals === "function" || x.equals instanceof Function) && x.equals(y))
|
171 | return true;
|
172 |
|
173 |
|
174 |
|
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 |
|
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 |
|
191 | if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1)
|
192 | return false;
|
193 |
|
194 |
|
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 | }());
|
232 | exports.OrmUtils = OrmUtils;
|
233 |
|
234 |
|