data-structure-typed 0.9.16 → 1.3.0

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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +96 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,505 +0,0 @@
1
- import * as _ from 'lodash';
2
- import {AnyFunction} from '../data-structures/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-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/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]> => names.map(i => obj[i]);
64
-
65
- export const isObject = (object: string | JSONObject | boolean | AnyFunction | number) => object != null && typeof object === 'object';
66
-
67
- export const looseEqual = (a: any, b: any): boolean => a == b;
68
-
69
- export const strictEqual = (a: any, b: any): boolean => a === b;
70
-
71
- export const strictObjectIsEqual = (a: any, b: any): boolean => Object.is(a, b);
72
-
73
- export const deepObjectStrictEqual = (object1: JSONSerializable, object2: JSONSerializable) => {
74
- const keys1 = Object.keys(object1);
75
- const keys2 = Object.keys(object2);
76
- if (keys1.length !== keys2.length) {
77
- return false;
78
- }
79
- for (const key of keys1) {
80
- const val1 = object1[key];
81
- const val2 = object2[key];
82
- const areObjects = isObject(val1) && isObject(val2);
83
- if (
84
- areObjects && !deepObjectStrictEqual(val1, val2) ||
85
- !areObjects && val1 !== val2
86
- ) {
87
- return false;
88
- }
89
- }
90
- return true;
91
- };
92
-
93
- export function reverseColor(oldColor: string) {
94
- const oldColorTemp = '0x' + oldColor.replace(/#/g, '');
95
- const str = '000000' + (0xFFFFFF - Number(oldColorTemp)).toString(16);
96
- return '#' + str.substring(str.length - 6, str.length);
97
- }
98
-
99
- export const isSameStructure = (objA: unknown, objB: unknown) => {
100
- const objATraversable = objA as JSONSerializable;
101
- const objBTraversable = objB as JSONSerializable;
102
- const objAKeys = Object.keys(objATraversable);
103
- const objBKeys = Object.keys(objBTraversable);
104
- let isSame = true;
105
- if (objAKeys.length !== objBKeys.length) {
106
- return isSame = false;
107
- } else {
108
- objAKeys.forEach((i) => {
109
- if (!objBKeys.includes(i)) {
110
- return isSame = false;
111
- }
112
- });
113
- return isSame;
114
- }
115
- };
116
-
117
- export const isLeafParent = (obj: JSONObject) => {
118
- let isLeaf = true;
119
- Object.values(obj).forEach(value => {
120
- if (typeof value === 'object' && value instanceof Array) {
121
- value.forEach(item => {
122
- if (typeof item === 'object') {
123
- return false;
124
- }
125
- });
126
- return isLeaf = true;
127
- }
128
- if (!['string', 'boolean', 'number', 'undefined', 'function'].includes(typeof value) && (value !== null)) {
129
- return isLeaf = false;
130
- }
131
- });
132
- return isLeaf;
133
- };
134
-
135
- export const addDays = (date: Date, days: number): Date => {
136
- date.setDate(date.getDate() + days);
137
- return date;
138
- };
139
-
140
- export class WaitManager {
141
- private _time30 = 20000;
142
- private readonly _nXSpeed: number = 1;
143
-
144
- constructor(nXSpeed?: number) {
145
- if (nXSpeed === undefined) nXSpeed = 1;
146
- this._nXSpeed = nXSpeed;
147
- }
148
-
149
- private _time1 = 1000;
150
-
151
- get time1(): number {
152
- return this._time1 / this._nXSpeed;
153
- }
154
-
155
- private _time2 = 2000;
156
-
157
- get time2(): number {
158
- return this._time2 / this._nXSpeed;
159
- }
160
-
161
- private _time3 = 3000;
162
-
163
- get time3(): number {
164
- return this._time3 / this._nXSpeed;
165
- }
166
-
167
- private _time4 = 4000;
168
-
169
- get time4(): number {
170
- return this._time4 / this._nXSpeed;
171
- }
172
-
173
- private _time10 = 10000;
174
-
175
- get time10(): number {
176
- return this._time10 / this._nXSpeed;
177
- }
178
-
179
- private _time20 = 20000;
180
-
181
- get time20(): number {
182
- return this._time20 / this._nXSpeed;
183
- }
184
-
185
- get time50(): number {
186
- return this._time30 / this._nXSpeed;
187
- }
188
-
189
- private _time60 = 60000;
190
-
191
- get time60(): number {
192
- return this._time60 / this._nXSpeed;
193
- }
194
-
195
- private _cusTime = 1000;
196
-
197
- get cusTime(): number {
198
- return this._cusTime / this._nXSpeed;
199
- }
200
-
201
- set cusTime(v: number) {
202
- this._cusTime = v;
203
- }
204
- }
205
-
206
- export const wait = async (ms: number, resolveValue?: any) => {
207
- return new Promise((resolve, reject) => {
208
- setTimeout(() => {
209
- const finalResolveValue = resolveValue || true;
210
- resolve(finalResolveValue);
211
- }, ms);
212
- });
213
- };
214
-
215
- export function extractValue<Item>(data: { key: string, value: Item }[]) {
216
- let result: Item[] = [];
217
- if (data && data.length > 0) {
218
- result = data.map(item => item.value);
219
- }
220
- return result;
221
- }
222
-
223
- export function keyValueToArray<Item>(data: { [key: string]: Item }) {
224
- const itemArray: Array<Item> = [];
225
- const keys = Object.keys(data);
226
- for (const i of keys) {
227
- itemArray.push({...data[i], _id: i});
228
- }
229
- return itemArray;
230
- }
231
-
232
- export function minuted(time: number) {
233
- const minutes = Math.floor(time / 60000).toString();
234
- const seconds = Math.floor((time % 60000) / 1000).toString().padStart(2, '0');
235
- return `${minutes}:${seconds}`;
236
- }
237
-
238
- export function randomDate(start?: Date, end?: Date, specificProbabilityStart?: Date, specificProbability?: number) {
239
- if (!start) start = new Date('1970-1-1');
240
- if (!end) end = new Date();
241
-
242
- if (specificProbabilityStart) {
243
- if (!specificProbability) specificProbability = 0.5;
244
- if (Math.random() <= specificProbability) {
245
- return new Date(specificProbabilityStart.getTime() + Math.random() * (end.getTime() - specificProbabilityStart.getTime()));
246
- }
247
- }
248
-
249
- return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
250
- }
251
-
252
- export const capitalizeWords = (str: string) => str.replace(/(?:^|\s)\S/g, (a: string) => a.toUpperCase());
253
-
254
- export const capitalizeFirstLetter = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
255
-
256
- export const comparerArray = <T>(otherArray: T[], limitKeys?: string[]) => {
257
- return function (current: T) {
258
- return otherArray.filter(function (other: T) {
259
- if (!limitKeys) {
260
- return _.isEqual(current, other);
261
- } else {
262
- // TODO
263
- }
264
- }).length == 0;
265
- };
266
- };
267
-
268
- export const onlyInA = <T>(a: T[], b: T[]) => a.filter(comparerArray(b));
269
-
270
- export const onlyInB = <T>(a: T[], b: T[]) => b.filter(comparerArray(a));
271
-
272
- export const diffAB = <T>(a: T[], b: T[]) => onlyInA(a, b).concat(onlyInB(a, b));
273
-
274
- export class StringUtil {
275
- // camelCase
276
- static toCamelCase(str: string) {
277
- return _.camelCase(str);
278
- }
279
-
280
- // snake_case
281
- static toSnakeCase(str: string) {
282
- return _.snakeCase(str);
283
- }
284
-
285
- // PascalCase
286
- static toPascalCase(str: string) {
287
- return _.startCase(_.camelCase(str)).replace(/ /g, '');
288
- }
289
-
290
- // CONSTANT_CASE
291
- static toConstantCase(str: string) {
292
- return _.upperCase(str).replace(/ /g, '_');
293
- }
294
-
295
- // kebab-case
296
- static toKebabCase(str: string) {
297
- return _.kebabCase(str);
298
- }
299
-
300
- // lowercase
301
- static toLowerCase(str: string) {
302
- return _.lowerCase(str).replace(/ /g, '');
303
- }
304
-
305
- // Title Case
306
- static toTitleCase(str: string) {
307
- return _.startCase(_.camelCase(str));
308
- }
309
-
310
- // Sentence case
311
- static toSentenceCase(str: string) {
312
- return _.upperFirst(_.lowerCase(str));
313
- }
314
-
315
- // path/case
316
- static toPathCase(str: string) {
317
- return _.lowerCase(str).replace(/ /g, '/');
318
- }
319
-
320
- // dot.case
321
- static toDotCase(str: string) {
322
- return _.lowerCase(str).replace(/ /g, '.');
323
- }
324
- }
325
-
326
- export type CaseType = 'camel' | 'snake' | 'pascal' | 'constant' | 'kebab' | 'lower' | 'title' | 'sentence' | 'path' | 'dot';
327
- export const deepKeysConvert = (obj: any, toType?: CaseType): any => {
328
- const _toType = toType || 'snake';
329
- if (Array.isArray(obj)) {
330
- return obj.map(v => deepKeysConvert(v, _toType));
331
- } else if (obj !== null && obj.constructor === Object) {
332
- return Object.keys(obj).reduce(
333
- (result, key) => {
334
- let newKey = '';
335
- switch (_toType) {
336
- case 'camel':
337
- newKey = StringUtil.toCamelCase(key);
338
- break;
339
- case 'snake':
340
- newKey = StringUtil.toSnakeCase(key);
341
- break;
342
- case 'pascal':
343
- newKey = StringUtil.toPascalCase(key);
344
- break;
345
- case 'constant':
346
- newKey = StringUtil.toConstantCase(key);
347
- break;
348
- case 'kebab':
349
- newKey = StringUtil.toKebabCase(key);
350
- break;
351
- case 'lower':
352
- newKey = StringUtil.toLowerCase(key);
353
- break;
354
- case 'title':
355
- newKey = StringUtil.toTitleCase(key);
356
- break;
357
- case 'sentence':
358
- newKey = StringUtil.toSentenceCase(key);
359
- break;
360
- case 'path':
361
- newKey = StringUtil.toPathCase(key);
362
- break;
363
- case 'dot':
364
- newKey = StringUtil.toDotCase(key);
365
- break;
366
- default:
367
- newKey = StringUtil.toDotCase(key);
368
- break;
369
- }
370
- return {
371
- ...result,
372
- [newKey]: deepKeysConvert(obj[key], _toType),
373
- };
374
- },
375
- {},
376
- );
377
- }
378
- return obj;
379
- };
380
-
381
- export const deepRemoveByKey = (obj: any, keysToBeRemoved: string[]) => {
382
- const result = _.transform(obj, function (result: JSONSerializable, value: any, key: string) {
383
- if (_.isObject(value)) {
384
- value = deepRemoveByKey(value, keysToBeRemoved);
385
- }
386
- if (!keysToBeRemoved.includes(key)) {
387
- _.isArray(obj) ? result.push(value) : result[key] = value;
388
- }
389
- });
390
- return result as typeof obj;
391
- };
392
-
393
- export const deepRenameKeys = (obj: JSONSerializable, keysMap: { [key in string]: string }) => {
394
- return _.transform(obj, function (result: JSONSerializable, value: any, key: string | number) {
395
- const currentKey = keysMap[key] || key;
396
- result[currentKey] = _.isObject(value) ? deepRenameKeys(value, keysMap) : value;
397
- });
398
- };
399
-
400
- export const deepReplaceValues = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }) => {
401
- const newObject = _.clone(obj) as JSONSerializable;
402
- _.each(obj, (val: any, key: string) => {
403
- for (const item in keyReducerMap) {
404
- if (key === item) {
405
- newObject[key] = keyReducerMap[item](newObject);
406
- } else if (typeof (val) === 'object' || val instanceof Array) {
407
- newObject[key] = deepReplaceValues(val, keyReducerMap);
408
- }
409
- }
410
- });
411
- return newObject;
412
- };
413
-
414
- // TODO determine depth and pass root node as a param through callback
415
- export const deepAdd = (obj: JSONSerializable, keyReducerMap: { [key in string]: (item: JSONSerializable) => any }, isItemRootParent?: boolean) => {
416
- const newObject = _.clone(obj) as JSONObject | [];
417
- if (_.isObject(newObject) && !_.isArray(newObject)) {
418
- for (const item in keyReducerMap) {
419
- newObject[item] = keyReducerMap[item](newObject);
420
- }
421
- }
422
- _.each(obj, (val: any, key: string | number) => {
423
- if (_.isObject(val)) {
424
- for (const item in keyReducerMap) {
425
- // @ts-ignore
426
- newObject[key] = deepAdd(val, keyReducerMap, isItemRootParent);
427
- }
428
- }
429
- });
430
- return newObject;
431
- };
432
-
433
- const styleString = (color: string) => `color: ${color}; font-weight: bold`;
434
-
435
- const styleHeader = (header: string) => `%c[${header}]`;
436
-
437
- export const bunnyConsole = {
438
- log: (headerLog = 'bunny', ...args: any[]) => {
439
- return console.log(styleHeader(headerLog), styleString('black'), ...args);
440
- },
441
- warn: (headerLog = 'bunny', ...args: any[]) => {
442
- return console.warn(styleHeader(headerLog), styleString('orange'), ...args);
443
- },
444
- error: (headerLog = 'bunny', ...args: any[]) => {
445
- return console.error(styleHeader(headerLog), styleString('red'), ...args);
446
- }
447
- };
448
-
449
- export const timeStart = () => {
450
- return performance ? performance.now() : new Date().getTime();
451
- };
452
-
453
- export const timeEnd = (startTime: number, headerLog?: string, consoleConditionFn?: (timeSpent: number) => boolean) => {
454
- const timeSpent = (performance ? performance.now() : new Date().getTime()) - startTime;
455
- const isPassCondition = consoleConditionFn ? consoleConditionFn(timeSpent) : true;
456
- if (isPassCondition) {
457
- bunnyConsole.log(headerLog ? headerLog : 'time spent', timeSpent.toFixed(2));
458
- }
459
- };
460
-
461
- export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
462
- let i = -1, len = array ? array.length : 0;
463
- const result = [];
464
-
465
- while (++i < len) {
466
- const value = array[i];
467
- if (predicate(value, i, array)) {
468
- result.push(value);
469
- Array.prototype.splice.call(array, i--, 1);
470
- len--;
471
- }
472
- }
473
-
474
- return result;
475
- };
476
-
477
- export function memo() {
478
- const cache: { [k: string]: any } = {};
479
- // eslint-disable-next-line @typescript-eslint/ban-types
480
- return function (target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
481
- const originalMethod = descriptor.value;
482
- descriptor.value = function (...args: any[]) {
483
- const cacheKey = `__cacheKey__${args.toString()}`;
484
- // eslint-disable-next-line no-prototype-builtins
485
- if (!cache.hasOwnProperty(cacheKey)) {
486
- cache[cacheKey] = originalMethod.apply(this, args);
487
- }
488
- return cache[cacheKey];
489
- }
490
- }
491
- }
492
-
493
- export function zip<T = number, T1 = number>(array1: T[], array2: T1[], options?: { isToObj: boolean }) {
494
- const zipped: [T, T1][] = [];
495
- const zippedObjCoords: { x: T, y: T1 }[] = [];
496
- const {isToObj} = options ? options : {isToObj: false};
497
- for (let i = 0; i < array1.length; i++) {
498
- if (isToObj) {
499
- zippedObjCoords.push({x: array1[i], y: array2[i]})
500
- } else {
501
- zipped.push([array1[i], array2[i]]);
502
- }
503
- }
504
- return isToObj ? zippedObjCoords : zipped;
505
- }
package/tsconfig.json DELETED
@@ -1,56 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "declaration": true,
4
- "outDir": "./dist",
5
- "module": "commonjs",
6
- "target": "es5",
7
- "lib": [
8
- // "es2015",
9
- "esnext"
10
- ],
11
- "strict": true,
12
- "esModuleInterop": true,
13
- "moduleResolution": "node",
14
- "declarationDir": "./dist",
15
- "skipLibCheck": true,
16
- "downlevelIteration": true,
17
-
18
- // "allowJs": true,
19
- // "allowSyntheticDefaultImports": true,
20
- // "forceConsistentCasingInFileNames": true,
21
- // "noFallthroughCasesInSwitch": true,
22
- // "resolveJsonModule": true,
23
- // "isolatedModules": true,
24
- // "noEmit": true,
25
- "typeRoots": [
26
- "node_modules/@types"
27
- ]
28
- },
29
-
30
- "include": [
31
- "src",
32
- ],
33
- "exclude": [
34
- // "node_modules/data-structure-typed",
35
- "node_modules",
36
- "dist"
37
- ]
38
- }
39
-
40
-
41
- //{
42
- // "compilerOptions": {
43
- // "declaration": true,
44
- // "outDir": "./dist",
45
- // "module": "commonjs",
46
- // "target": "es5",
47
- // "lib": ["es6", "dom"],
48
- // "strict": true,
49
- // "esModuleInterop": true,
50
- // "moduleResolution": "node",
51
- // "declarationDir": "./dist/types",
52
- // "skipLibCheck": true
53
- // },
54
- // "include": ["src"],
55
- // "exclude": ["node_modules", "dist"]
56
- //}