data-structure-typed 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/.idea/data-structure-typed.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/README.md +2 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +6 -0
  6. package/dist/data-structures/binary-tree/avl-tree.js +231 -0
  7. package/dist/data-structures/binary-tree/b-tree.js +6 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
  9. package/dist/data-structures/binary-tree/binary-tree.js +992 -0
  10. package/dist/data-structures/binary-tree/bst.js +431 -0
  11. package/dist/data-structures/binary-tree/index.js +20 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +6 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +151 -0
  14. package/dist/data-structures/binary-tree/splay-tree.js +6 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
  16. package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
  17. package/dist/data-structures/graph/abstract-graph.js +648 -0
  18. package/dist/data-structures/graph/directed-graph.js +268 -0
  19. package/dist/data-structures/graph/index.js +19 -0
  20. package/dist/data-structures/graph/undirected-graph.js +142 -0
  21. package/dist/data-structures/hash/coordinate-map.js +24 -0
  22. package/dist/data-structures/hash/coordinate-set.js +21 -0
  23. package/dist/data-structures/hash/hash-table.js +2 -0
  24. package/dist/data-structures/hash/index.js +17 -0
  25. package/dist/data-structures/hash/pair.js +2 -0
  26. package/dist/data-structures/hash/tree-map.js +2 -0
  27. package/dist/data-structures/hash/tree-set.js +2 -0
  28. package/dist/data-structures/heap/heap.js +114 -0
  29. package/dist/data-structures/heap/index.js +19 -0
  30. package/dist/data-structures/heap/max-heap.js +22 -0
  31. package/dist/data-structures/heap/min-heap.js +22 -0
  32. package/dist/data-structures/index.js +25 -0
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
  34. package/dist/data-structures/linked-list/index.js +18 -0
  35. package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
  37. package/dist/data-structures/matrix/index.js +19 -0
  38. package/dist/data-structures/matrix/matrix.js +14 -0
  39. package/dist/data-structures/matrix/matrix2d.js +119 -0
  40. package/dist/data-structures/matrix/navigator.js +78 -0
  41. package/dist/data-structures/matrix/vector2d.js +161 -0
  42. package/dist/data-structures/priority-queue/index.js +19 -0
  43. package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
  45. package/dist/data-structures/priority-queue/priority-queue.js +174 -0
  46. package/dist/data-structures/queue/deque.js +132 -0
  47. package/dist/data-structures/queue/index.js +17 -0
  48. package/dist/data-structures/queue/queue.js +113 -0
  49. package/dist/data-structures/stack/index.js +17 -0
  50. package/dist/data-structures/stack/stack.js +97 -0
  51. package/dist/data-structures/trampoline.js +52 -0
  52. package/dist/data-structures/trie/index.js +17 -0
  53. package/dist/data-structures/trie/trie.js +141 -0
  54. package/dist/index.js +17 -0
  55. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
  57. package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
  61. package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
  62. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
  64. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
  65. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
  66. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
  69. package/dist/types/data-structures/graph/index.d.ts +3 -0
  70. package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
  71. package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
  72. package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
  73. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  74. package/dist/types/data-structures/hash/index.d.ts +1 -0
  75. package/dist/types/data-structures/hash/pair.d.ts +1 -0
  76. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  77. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  78. package/dist/types/data-structures/heap/heap.d.ts +72 -0
  79. package/dist/types/data-structures/heap/index.d.ts +3 -0
  80. package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
  81. package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
  82. package/dist/types/data-structures/index.d.ts +9 -0
  83. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
  84. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  85. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
  86. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  87. package/dist/types/data-structures/matrix/index.d.ts +3 -0
  88. package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
  89. package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
  90. package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
  91. package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
  92. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  93. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
  94. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
  95. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
  96. package/dist/types/data-structures/queue/deque.d.ts +37 -0
  97. package/dist/types/data-structures/queue/index.d.ts +1 -0
  98. package/dist/types/data-structures/queue/queue.d.ts +76 -0
  99. package/dist/types/data-structures/stack/index.d.ts +1 -0
  100. package/dist/types/data-structures/stack/stack.d.ts +69 -0
  101. package/dist/types/data-structures/trampoline.d.ts +25 -0
  102. package/dist/types/data-structures/trie/index.d.ts +1 -0
  103. package/dist/types/data-structures/trie/trie.d.ts +28 -0
  104. package/dist/types/index.d.ts +1 -0
  105. package/dist/types/index.js +17 -0
  106. package/dist/types/types/index.d.ts +1 -0
  107. package/dist/types/types/utils.d.ts +46 -0
  108. package/dist/types/utils.d.ts +122 -0
  109. package/dist/types/utils.js +53 -0
  110. package/dist/utils.js +569 -0
  111. package/package.json +75 -0
  112. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  113. package/src/data-structures/binary-tree/avl-tree.ts +232 -0
  114. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  115. package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
  117. package/src/data-structures/binary-tree/bst.ts +404 -0
  118. package/src/data-structures/binary-tree/index.ts +4 -0
  119. package/src/data-structures/binary-tree/rb-tree.ts +3 -0
  120. package/src/data-structures/binary-tree/segment-tree.ts +164 -0
  121. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  122. package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
  123. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  124. package/src/data-structures/graph/abstract-graph.ts +789 -0
  125. package/src/data-structures/graph/directed-graph.ts +322 -0
  126. package/src/data-structures/graph/index.ts +3 -0
  127. package/src/data-structures/graph/undirected-graph.ts +154 -0
  128. package/src/data-structures/hash/coordinate-map.ts +24 -0
  129. package/src/data-structures/hash/coordinate-set.ts +20 -0
  130. package/src/data-structures/hash/hash-table.ts +1 -0
  131. package/src/data-structures/hash/index.ts +1 -0
  132. package/src/data-structures/hash/pair.ts +1 -0
  133. package/src/data-structures/hash/tree-map.ts +1 -0
  134. package/src/data-structures/hash/tree-set.ts +1 -0
  135. package/src/data-structures/heap/heap.ts +136 -0
  136. package/src/data-structures/heap/index.ts +3 -0
  137. package/src/data-structures/heap/max-heap.ts +22 -0
  138. package/src/data-structures/heap/min-heap.ts +24 -0
  139. package/src/data-structures/index.ts +10 -0
  140. package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
  141. package/src/data-structures/linked-list/index.ts +2 -0
  142. package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
  143. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  144. package/src/data-structures/matrix/index.ts +3 -0
  145. package/src/data-structures/matrix/matrix.ts +13 -0
  146. package/src/data-structures/matrix/matrix2d.ts +125 -0
  147. package/src/data-structures/matrix/navigator.ts +99 -0
  148. package/src/data-structures/matrix/vector2d.ts +189 -0
  149. package/src/data-structures/priority-queue/index.ts +3 -0
  150. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
  151. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
  152. package/src/data-structures/priority-queue/priority-queue.ts +208 -0
  153. package/src/data-structures/queue/deque.ts +139 -0
  154. package/src/data-structures/queue/index.ts +1 -0
  155. package/src/data-structures/queue/queue.ts +123 -0
  156. package/src/data-structures/stack/index.ts +1 -0
  157. package/src/data-structures/stack/stack.ts +104 -0
  158. package/src/data-structures/trampoline.ts +91 -0
  159. package/src/data-structures/trie/index.ts +1 -0
  160. package/src/data-structures/trie/trie.ts +153 -0
  161. package/src/index.ts +1 -0
  162. package/src/types/index.ts +1 -0
  163. package/src/types/patches/index.d.ts +0 -0
  164. package/src/types/utils.ts +158 -0
  165. package/src/utils.ts +605 -0
  166. package/tsconfig.json +52 -0
package/src/utils.ts ADDED
@@ -0,0 +1,605 @@
1
+ import _ from 'lodash';
2
+ import {AnyFunction} from './types';
3
+
4
+ export type JSONSerializable = {
5
+ [key: string]: any
6
+ }
7
+ export type JSONValue = string | number | boolean | undefined | JSONObject;
8
+
9
+ export interface JSONObject {
10
+ [key: string]: JSONValue;
11
+ }
12
+
13
+ export function randomText(length: number) {
14
+ let result = '';
15
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
16
+ const charactersLength = characters.length;
17
+ for (let i = 0; i < length; i++) {
18
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
19
+ }
20
+ return result;
21
+ }
22
+
23
+ export const uuidV4 = function () {
24
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
25
+ const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
26
+ return v.toString(16);
27
+ });
28
+ };
29
+
30
+ export class IncrementId {
31
+ private _id: string;
32
+ private readonly _prefix: string;
33
+
34
+ constructor(prefix?: string) {
35
+ this._prefix = prefix ? prefix : '';
36
+ this._id = this._prefix + '0';
37
+ }
38
+
39
+ getId() {
40
+ const {_id, _prefix} = this;
41
+ if (!_id) {
42
+ this._id = _prefix + '0';
43
+ } else {
44
+ const idNumStr = _id.substr(_prefix.length, _id.length - _prefix.length);
45
+ const newIdNum = parseInt(idNumStr, 10) + 1;
46
+ this._id = _prefix + newIdNum.toString();
47
+ }
48
+ return this._id;
49
+ }
50
+ }
51
+
52
+ export function incrementId(prefix?: string) {
53
+ const _prefix = prefix ? prefix : '';
54
+ let _id = _prefix + '0';
55
+ return function id() {
56
+ const idNumStr = _id.substr(_prefix.length, _id.length - _prefix.length);
57
+ const newIdNum = parseInt(idNumStr, 10) + 1;
58
+ _id = _prefix + newIdNum.toString();
59
+ return _id;
60
+ };
61
+ }
62
+
63
+ export const getValue = <T, K extends keyof T>(obj: T, names: K[]): Array<T[K]> => {
64
+ return names.map(i => obj[i]);
65
+ };
66
+
67
+ export const isObject = (object: string | JSONObject | boolean | AnyFunction | number) => {
68
+ return object != null && typeof object === 'object';
69
+ };
70
+
71
+ export const looseEqual = (a: any, b: any): boolean => {
72
+ return a == b;
73
+ };
74
+
75
+ export const strictEqual = (a: any, b: any): boolean => {
76
+ return a === b;
77
+ };
78
+
79
+ export const strictObjectIsEqual = (a: any, b: any): boolean => {
80
+ return Object.is(a, b);
81
+ };
82
+
83
+ export const deepObjectStrictEqual = (object1: JSONSerializable, object2: JSONSerializable) => {
84
+ const keys1 = Object.keys(object1);
85
+ const keys2 = Object.keys(object2);
86
+ if (keys1.length !== keys2.length) {
87
+ return false;
88
+ }
89
+ for (const key of keys1) {
90
+ const val1 = object1[key];
91
+ const val2 = object2[key];
92
+ const areObjects = isObject(val1) && isObject(val2);
93
+ if (
94
+ areObjects && !deepObjectStrictEqual(val1, val2) ||
95
+ !areObjects && val1 !== val2
96
+ ) {
97
+ return false;
98
+ }
99
+ }
100
+ return true;
101
+ };
102
+
103
+ export const isTypeEqual = <T>(obj: unknown) => {
104
+ const m = obj as unknown as T;
105
+ };
106
+
107
+ export function reverseColor(oldColor: string) {
108
+ const oldColorTemp = '0x' + oldColor.replace(/#/g, '');
109
+ const str = '000000' + (0xFFFFFF - Number(oldColorTemp)).toString(16);
110
+ return '#' + str.substring(str.length - 6, str.length);
111
+ }
112
+
113
+ export const isSameStructure = (objA: unknown, objB: unknown) => {
114
+ const objATraversable = objA as JSONSerializable;
115
+ const objBTraversable = objB as JSONSerializable;
116
+ const objAKeys = Object.keys(objATraversable);
117
+ const objBKeys = Object.keys(objBTraversable);
118
+ let isSame = true;
119
+ if (objAKeys.length !== objBKeys.length) {
120
+ return isSame = false;
121
+ } else {
122
+ objAKeys.forEach((i) => {
123
+ if (!objBKeys.includes(i)) {
124
+ return isSame = false;
125
+ }
126
+ });
127
+ return isSame;
128
+ }
129
+ };
130
+
131
+ export const isLeafParent = (obj: JSONObject) => {
132
+ let isLeaf = true;
133
+ Object.values(obj).forEach(value => {
134
+ if (typeof value === 'object' && value instanceof Array) {
135
+ value.forEach(item => {
136
+ if (typeof item === 'object') {
137
+ return false;
138
+ }
139
+ });
140
+ return isLeaf = true;
141
+ }
142
+ if (!['string', 'boolean', 'number', 'undefined', 'function'].includes(typeof value) && (value !== null)) {
143
+ return isLeaf = false;
144
+ }
145
+ });
146
+ return isLeaf;
147
+ };
148
+
149
+ export const addDays = (date: Date, days: number): Date => {
150
+ date.setDate(date.getDate() + days);
151
+ return date;
152
+ };
153
+
154
+ export class WaitManager {
155
+ private _time1 = 1000;
156
+ get time1(): number {
157
+ return this._time1 / this._nXSpeed;
158
+ }
159
+
160
+ private _time2 = 2000;
161
+ get time2(): number {
162
+ return this._time2 / this._nXSpeed;
163
+ }
164
+
165
+ private _time3 = 3000;
166
+ get time3(): number {
167
+ return this._time3 / this._nXSpeed;
168
+ }
169
+
170
+ private _time4 = 4000;
171
+ get time4(): number {
172
+ return this._time4 / this._nXSpeed;
173
+ }
174
+
175
+ private _time10 = 10000;
176
+ get time10(): number {
177
+ return this._time10 / this._nXSpeed;
178
+ }
179
+
180
+ private _time20 = 20000;
181
+ get time20(): number {
182
+ return this._time20 / this._nXSpeed;
183
+ }
184
+
185
+ private _time30 = 20000;
186
+
187
+ get time50(): number {
188
+ return this._time30 / this._nXSpeed;
189
+ }
190
+
191
+ private _time60 = 60000;
192
+ get time60(): number {
193
+ return this._time60 / this._nXSpeed;
194
+ }
195
+
196
+ private _cusTime = 1000;
197
+ get cusTime(): number {
198
+ return this._cusTime / this._nXSpeed;
199
+ }
200
+
201
+ set cusTime(v: number) {
202
+ this._cusTime = v;
203
+ }
204
+
205
+ private readonly _nXSpeed: number = 1;
206
+
207
+ constructor(nXSpeed?: number) {
208
+ if (nXSpeed === undefined) nXSpeed = 1;
209
+ this._nXSpeed = nXSpeed;
210
+ }
211
+ }
212
+
213
+ export const wait = async (ms: number, resolveValue?: any) => {
214
+ return new Promise((resolve, reject) => {
215
+ setTimeout(() => {
216
+ const finalResolveValue = resolveValue || true;
217
+ resolve(finalResolveValue);
218
+ }, ms);
219
+ });
220
+ };
221
+
222
+ export class AuthAPIError extends Error {
223
+ protected serverErrorStack;
224
+ protected serverErrorCode;
225
+
226
+ constructor(serverErrorMessage: string, serverErrorCode?: string, serverErrorStack?: string) {
227
+ super(serverErrorMessage);
228
+ if (serverErrorStack) {
229
+ this.serverErrorStack = serverErrorStack;
230
+ }
231
+ if (serverErrorCode) {
232
+ this.serverErrorCode = serverErrorCode;
233
+ }
234
+ this.name = new.target.name;
235
+ if (typeof (Error as any).captureStackTrace === 'function') {
236
+ (Error as any).captureStackTrace(this, new.target);
237
+ }
238
+ if (typeof Object.setPrototypeOf === 'function') {
239
+ Object.setPrototypeOf(this, new.target.prototype);
240
+ } else {
241
+ (this as any).__proto__ = new.target.prototype;
242
+ }
243
+ }
244
+ }
245
+
246
+ export class BunnyAPIError extends Error {
247
+ protected serverErrorStack;
248
+ protected serverErrorCode;
249
+
250
+ constructor(serverErrorMessage: string, serverErrorCode?: string, serverErrorStack?: string) {
251
+ super(serverErrorMessage);
252
+ if (serverErrorStack) {
253
+ this.serverErrorStack = serverErrorStack;
254
+ }
255
+ if (serverErrorCode) {
256
+ this.serverErrorCode = serverErrorCode;
257
+ }
258
+ this.name = new.target.name;
259
+ if (typeof (Error as any).captureStackTrace === 'function') {
260
+ (Error as any).captureStackTrace(this, new.target);
261
+ }
262
+ if (typeof Object.setPrototypeOf === 'function') {
263
+ Object.setPrototypeOf(this, new.target.prototype);
264
+ } else {
265
+ (this as any).__proto__ = new.target.prototype;
266
+ }
267
+ }
268
+ }
269
+
270
+ export class NomicsAPIError extends Error {
271
+ protected serverErrorStack;
272
+ protected serverErrorCode;
273
+
274
+ constructor(serverErrorMessage: string, serverErrorCode?: string, serverErrorStack?: string) {
275
+ super(serverErrorMessage);
276
+ if (serverErrorStack) {
277
+ this.serverErrorStack = serverErrorStack;
278
+ }
279
+ if (serverErrorCode) {
280
+ this.serverErrorCode = serverErrorCode;
281
+ }
282
+ this.name = new.target.name;
283
+ if (typeof (Error as any).captureStackTrace === 'function') {
284
+ (Error as any).captureStackTrace(this, new.target);
285
+ }
286
+ if (typeof Object.setPrototypeOf === 'function') {
287
+ Object.setPrototypeOf(this, new.target.prototype);
288
+ } else {
289
+ (this as any).__proto__ = new.target.prototype;
290
+ }
291
+ }
292
+ }
293
+
294
+ export function extractValue<Item>(data: { key: string, value: Item }[]) {
295
+ let result: Item[] = [];
296
+ if (data && data.length > 0) {
297
+ result = data.map(item => item.value);
298
+ }
299
+ return result;
300
+ }
301
+
302
+ export function keyValueToArray<Item>(data: { [key: string]: Item }) {
303
+ const itemArray: Array<Item> = [];
304
+ const keys = Object.keys(data);
305
+ for (const i of keys) {
306
+ itemArray.push({...data[i], _id: i});
307
+ }
308
+ return itemArray;
309
+ }
310
+
311
+ export function minuted(time: number) {
312
+ const minutes = Math.floor(time / 60000).toString();
313
+ const seconds = Math.floor((time % 60000) / 1000).toString().padStart(2, '0');
314
+ return `${minutes}:${seconds}`;
315
+ }
316
+
317
+ export function randomDate(start?: Date, end?: Date, specificProbabilityStart?: Date, specificProbability?: number) {
318
+ if (!start) start = new Date('1970-1-1');
319
+ if (!end) end = new Date();
320
+
321
+ if (specificProbabilityStart) {
322
+ if (!specificProbability) specificProbability = 0.5;
323
+ if (Math.random() <= specificProbability) {
324
+ return new Date(specificProbabilityStart.getTime() + Math.random() * (end.getTime() - specificProbabilityStart.getTime()));
325
+ }
326
+ }
327
+
328
+ return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
329
+ }
330
+
331
+
332
+ export const capitalizeWords = (str: string) => {
333
+ return str.replace(/(?:^|\s)\S/g, (a: string) => a.toUpperCase());
334
+ };
335
+
336
+ export const capitalizeFirstLetter = (str: string) => {
337
+ return str.charAt(0).toUpperCase() + str.slice(1);
338
+ };
339
+
340
+ export const comparerArray = <T>(otherArray: T[], limitKeys?: string[]) => {
341
+ return function (current: T) {
342
+ return otherArray.filter(function (other: T) {
343
+ if (!limitKeys) {
344
+ return _.isEqual(current, other);
345
+ } else {
346
+ // TODO
347
+ }
348
+ }).length == 0;
349
+ };
350
+ };
351
+
352
+ export const onlyInA = <T>(a: T[], b: T[]) => {
353
+ return a.filter(comparerArray(b));
354
+ };
355
+
356
+ export const onlyInB = <T>(a: T[], b: T[]) => {
357
+ return b.filter(comparerArray(a));
358
+ };
359
+
360
+ export const diffAB = <T>(a: T[], b: T[]) => {
361
+ return onlyInA(a, b).concat(onlyInB(a, b));
362
+ };
363
+
364
+ export class StringUtil {
365
+ // camelCase
366
+ static toCamelCase(str: string) {
367
+ return _.camelCase(str);
368
+ }
369
+
370
+ // snake_case
371
+ static toSnakeCase(str: string) {
372
+ return _.snakeCase(str);
373
+ }
374
+
375
+ // PascalCase
376
+ static toPascalCase(str: string) {
377
+ return _.startCase(_.camelCase(str)).replace(/ /g, '');
378
+ }
379
+
380
+ // CONSTANT_CASE
381
+ static toConstantCase(str: string) {
382
+ return _.upperCase(str).replace(/ /g, '_');
383
+ }
384
+
385
+ // kebab-case
386
+ static toKebabCase(str: string) {
387
+ return _.kebabCase(str);
388
+ }
389
+
390
+ // lowercase
391
+ static toLowerCase(str: string) {
392
+ return _.lowerCase(str).replace(/ /g, '');
393
+ }
394
+
395
+ // Title Case
396
+ static toTitleCase(str: string) {
397
+ return _.startCase(_.camelCase(str));
398
+ }
399
+
400
+ // Sentence case
401
+ static toSentenceCase(str: string) {
402
+ return _.upperFirst(_.lowerCase(str));
403
+ }
404
+
405
+ // path/case
406
+ static toPathCase(str: string) {
407
+ return _.lowerCase(str).replace(/ /g, '/');
408
+ }
409
+
410
+ // dot.case
411
+ static toDotCase(str: string) {
412
+ return _.lowerCase(str).replace(/ /g, '.');
413
+ }
414
+ }
415
+
416
+ type ToCase = 'camel' | 'snake' | 'pascal' | 'constant' | 'kebab' | 'lower' | 'title' | 'sentence' | 'path' | 'dot';
417
+ export const deepKeysConvert = (obj: any, toType?: ToCase): any => {
418
+ const _toType = toType || 'snake';
419
+ if (Array.isArray(obj)) {
420
+ return obj.map(v => deepKeysConvert(v, _toType));
421
+ } else if (obj !== null && obj.constructor === Object) {
422
+ return Object.keys(obj).reduce(
423
+ (result, key) => {
424
+ let newKey = '';
425
+ switch (_toType) {
426
+ case 'camel':
427
+ newKey = StringUtil.toCamelCase(key);
428
+ break;
429
+ case 'snake':
430
+ newKey = StringUtil.toSnakeCase(key);
431
+ break;
432
+ case 'pascal':
433
+ newKey = StringUtil.toPascalCase(key);
434
+ break;
435
+ case 'constant':
436
+ newKey = StringUtil.toConstantCase(key);
437
+ break;
438
+ case 'kebab':
439
+ newKey = StringUtil.toKebabCase(key);
440
+ break;
441
+ case 'lower':
442
+ newKey = StringUtil.toLowerCase(key);
443
+ break;
444
+ case 'title':
445
+ newKey = StringUtil.toTitleCase(key);
446
+ break;
447
+ case 'sentence':
448
+ newKey = StringUtil.toSentenceCase(key);
449
+ break;
450
+ case 'path':
451
+ newKey = StringUtil.toPathCase(key);
452
+ break;
453
+ case 'dot':
454
+ newKey = StringUtil.toDotCase(key);
455
+ break;
456
+ default:
457
+ newKey = StringUtil.toDotCase(key);
458
+ break;
459
+ }
460
+ return {
461
+ ...result,
462
+ [newKey]: deepKeysConvert(obj[key], _toType),
463
+ };
464
+ },
465
+ {},
466
+ );
467
+ }
468
+ return obj;
469
+ };
470
+
471
+ export const deepRemoveByKey = (obj: any, keysToBeRemoved: string[]) => {
472
+ const result = _.transform(obj, function (result: JSONSerializable, value: any, key: string) {
473
+ if (_.isObject(value)) {
474
+ value = deepRemoveByKey(value, keysToBeRemoved);
475
+ }
476
+ if (!keysToBeRemoved.includes(key)) {
477
+ _.isArray(obj) ? result.push(value) : result[key] = value;
478
+ }
479
+ });
480
+ return result as typeof obj;
481
+ };
482
+
483
+ export const deepRenameKeys = (obj: JSONSerializable, keysMap: { [key in string]: string }) => {
484
+ return _.transform(obj, function (result: JSONSerializable, value: any, key: string | number) {
485
+ const currentKey = keysMap[key] || key;
486
+ result[currentKey] = _.isObject(value) ? deepRenameKeys(value, keysMap) : value;
487
+ });
488
+ };
489
+
490
+ export const deepReplaceValues = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }) => {
491
+ const newObject = _.clone(obj) as JSONSerializable;
492
+ _.each(obj, (val: any, key: string) => {
493
+ for (const item in keyReducerMap) {
494
+ if (key === item) {
495
+ newObject[key] = keyReducerMap[item](newObject);
496
+ } else if (typeof (val) === 'object' || val instanceof Array) {
497
+ newObject[key] = deepReplaceValues(val, keyReducerMap);
498
+ }
499
+ }
500
+ });
501
+ return newObject;
502
+ };
503
+
504
+ // function getCallStackSize() {
505
+ // let count = 0, fn = arguments.callee;
506
+ // while ( (fn = fn.caller) ) {
507
+ // count++;
508
+ // }
509
+ // return count;
510
+ // }
511
+ // TODO determine depth and pass root node as a param through callback
512
+ export const deepAdd = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }, isItemRootParent?: boolean) => {
513
+ const newObject = _.clone(obj) as JSONObject | [];
514
+ if (_.isObject(newObject) && !_.isArray(newObject)) {
515
+ for (const item in keyReducerMap) {
516
+ newObject[item] = keyReducerMap[item](newObject);
517
+ }
518
+ }
519
+ _.each(obj, (val: any, key: string | number) => {
520
+ if (_.isObject(val)) {
521
+ for (const item in keyReducerMap) {
522
+ // @ts-ignore
523
+ newObject[key] = deepAdd(val, keyReducerMap, isItemRootParent);
524
+ }
525
+ }
526
+ });
527
+ return newObject;
528
+ };
529
+
530
+ const styleString = (color: string) => `color: ${color}; font-weight: bold`;
531
+
532
+
533
+ const styleHeader = (header: string) => `%c[${header}]`;
534
+
535
+ export const bunnyConsole = {
536
+ log: (headerLog = 'bunny', ...args: any[]) => {
537
+ return console.log(styleHeader(headerLog), styleString('black'), ...args);
538
+ },
539
+ warn: (headerLog = 'bunny', ...args: any[]) => {
540
+ return console.warn(styleHeader(headerLog), styleString('orange'), ...args);
541
+ },
542
+ error: (headerLog = 'bunny', ...args: any[]) => {
543
+ return console.error(styleHeader(headerLog), styleString('red'), ...args);
544
+ }
545
+ };
546
+
547
+
548
+ export const timeStart = () => {
549
+ return performance ? performance.now() : new Date().getTime();
550
+ };
551
+
552
+ export const timeEnd = (startTime: number, headerLog?: string, consoleConditionFn?: (timeSpent: number) => boolean) => {
553
+ const timeSpent = (performance ? performance.now() : new Date().getTime()) - startTime;
554
+ const isPassCondition = consoleConditionFn ? consoleConditionFn(timeSpent) : true;
555
+ if (isPassCondition) {
556
+ bunnyConsole.log(headerLog ? headerLog : 'time spent', timeSpent.toFixed(2));
557
+ }
558
+ };
559
+
560
+ export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
561
+ let i = -1, len = array ? array.length : 0;
562
+ const result = [];
563
+
564
+ while (++i < len) {
565
+ const value = array[i];
566
+ if (predicate(value, i, array)) {
567
+ result.push(value);
568
+ Array.prototype.splice.call(array, i--, 1);
569
+ len--;
570
+ }
571
+ }
572
+
573
+ return result;
574
+ };
575
+
576
+ export function memo() {
577
+ const cache: { [k: string]: any } = {};
578
+ // eslint-disable-next-line @typescript-eslint/ban-types
579
+ return function (target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
580
+ const originalMethod = descriptor.value;
581
+ descriptor.value = function (...args: any[]) {
582
+ const cacheKey = `__cacheKey__${args.toString()}`;
583
+ // eslint-disable-next-line no-prototype-builtins
584
+ if (!cache.hasOwnProperty(cacheKey)) {
585
+ cache[cacheKey] = originalMethod.apply(this, args);
586
+ }
587
+ return cache[cacheKey];
588
+ }
589
+ }
590
+ }
591
+
592
+ export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?: { isToObj: boolean }) {
593
+ const zipped: [T, T1][] = [];
594
+ const zippedObjCoords: { x: T, y: T1 }[] = [];
595
+ const {isToObj} = options ? options : {isToObj: false};
596
+ for (let i = 0; i < array1.length; i++) {
597
+ if (isToObj) {
598
+ zippedObjCoords.push({x: array1[i], y: array2[i]})
599
+ } else {
600
+ zipped.push([array1[i], array2[i]]);
601
+ }
602
+ }
603
+ return isToObj ? zippedObjCoords : zipped;
604
+ }
605
+
package/tsconfig.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "outDir": "./dist",
5
+ "module": "commonjs",
6
+ "target": "es6",
7
+ "lib": [
8
+ "es6",
9
+ "dom",
10
+ "dom.iterable",
11
+ "esnext"
12
+ ],
13
+ "strict": true,
14
+ "esModuleInterop": true,
15
+ "moduleResolution": "node",
16
+ "declarationDir": "./dist/types",
17
+ "skipLibCheck": true
18
+
19
+ // "allowJs": true,
20
+ // "allowSyntheticDefaultImports": true,
21
+ // "forceConsistentCasingInFileNames": true,
22
+ // "noFallthroughCasesInSwitch": true,
23
+ // "resolveJsonModule": true,
24
+ // "isolatedModules": true,
25
+ // "noEmit": true,
26
+ },
27
+ "include": [
28
+ "src"
29
+ ],
30
+ "exclude": [
31
+ "node_modules",
32
+ "dist"
33
+ ]
34
+ }
35
+
36
+
37
+ //{
38
+ // "compilerOptions": {
39
+ // "declaration": true,
40
+ // "outDir": "./dist",
41
+ // "module": "commonjs",
42
+ // "target": "es5",
43
+ // "lib": ["es6", "dom"],
44
+ // "strict": true,
45
+ // "esModuleInterop": true,
46
+ // "moduleResolution": "node",
47
+ // "declarationDir": "./dist/types",
48
+ // "skipLibCheck": true
49
+ // },
50
+ // "include": ["src"],
51
+ // "exclude": ["node_modules", "dist"]
52
+ //}