@sparted/shared-library 0.0.12 → 0.0.13

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 (108) hide show
  1. package/_virtual/_tslib.js +23 -1
  2. package/_virtual/_tslib.js.map +1 -1
  3. package/date/constants.d.ts +4 -0
  4. package/date/constants.js +7 -0
  5. package/date/constants.js.map +1 -0
  6. package/date/convert.d.ts +2 -0
  7. package/date/convert.js +7 -0
  8. package/date/convert.js.map +1 -0
  9. package/date/format-minutes.d.ts +1 -0
  10. package/date/format-minutes.js +12 -0
  11. package/date/format-minutes.js.map +1 -0
  12. package/date/get-time-diff-as-string.d.ts +47 -0
  13. package/date/get-time-diff-as-string.js +126 -0
  14. package/date/get-time-diff-as-string.js.map +1 -0
  15. package/date/index.d.ts +4 -0
  16. package/date/index.js +4 -0
  17. package/date/index.js.map +1 -0
  18. package/formatting/format-number.d.ts +1 -0
  19. package/formatting/format-number.js +14 -0
  20. package/formatting/format-number.js.map +1 -0
  21. package/formatting/index.d.ts +1 -0
  22. package/formatting/index.js +2 -0
  23. package/formatting/index.js.map +1 -0
  24. package/fp/run-while.js +1 -1
  25. package/fp/run-while.js.map +1 -1
  26. package/http/encode-search-object.d.ts +7 -0
  27. package/http/encode-search-object.js +31 -0
  28. package/http/encode-search-object.js.map +1 -0
  29. package/http/fetch-with-timeout.d.ts +8 -0
  30. package/http/fetch-with-timeout.js +18 -0
  31. package/http/fetch-with-timeout.js.map +1 -0
  32. package/http/index.d.ts +2 -0
  33. package/http/index.js +3 -0
  34. package/http/index.js.map +1 -0
  35. package/index.d.ts +6 -0
  36. package/index.js +13 -0
  37. package/index.js.map +1 -1
  38. package/linked-list/apply-diff-linked-list.d.ts +2 -0
  39. package/linked-list/apply-diff-linked-list.js +27 -0
  40. package/linked-list/apply-diff-linked-list.js.map +1 -0
  41. package/linked-list/create-linked-list.d.ts +2 -0
  42. package/linked-list/create-linked-list.js +66 -0
  43. package/linked-list/create-linked-list.js.map +1 -0
  44. package/linked-list/find-linked-list.d.ts +2 -0
  45. package/linked-list/find-linked-list.js +25 -0
  46. package/linked-list/find-linked-list.js.map +1 -0
  47. package/linked-list/get-linked-list.d.ts +2 -0
  48. package/linked-list/get-linked-list.js +9 -0
  49. package/linked-list/get-linked-list.js.map +1 -0
  50. package/linked-list/index.d.ts +2 -0
  51. package/linked-list/index.js +2 -0
  52. package/linked-list/index.js.map +1 -0
  53. package/linked-list/insert-linked-list.d.ts +2 -0
  54. package/linked-list/insert-linked-list.js +30 -0
  55. package/linked-list/insert-linked-list.js.map +1 -0
  56. package/linked-list/linked-list-utils.d.ts +23 -0
  57. package/linked-list/linked-list-utils.js +97 -0
  58. package/linked-list/linked-list-utils.js.map +1 -0
  59. package/linked-list/reduce-linked-list.d.ts +2 -0
  60. package/linked-list/reduce-linked-list.js +17 -0
  61. package/linked-list/reduce-linked-list.js.map +1 -0
  62. package/linked-list/remove-linked-list.d.ts +2 -0
  63. package/linked-list/remove-linked-list.js +25 -0
  64. package/linked-list/remove-linked-list.js.map +1 -0
  65. package/linked-list/type.d.ts +34 -0
  66. package/linked-list/update-linked-list.d.ts +2 -0
  67. package/linked-list/update-linked-list.js +21 -0
  68. package/linked-list/update-linked-list.js.map +1 -0
  69. package/linked-list/walk-list-item-record.d.ts +15 -0
  70. package/linked-list/while-linked-list.d.ts +2 -0
  71. package/linked-list/while-linked-list.js +29 -0
  72. package/linked-list/while-linked-list.js.map +1 -0
  73. package/math/clamp-number.d.ts +2 -0
  74. package/math/clamp-number.js +10 -0
  75. package/math/clamp-number.js.map +1 -0
  76. package/math/index.d.ts +4 -0
  77. package/math/index.js +5 -0
  78. package/math/index.js.map +1 -0
  79. package/math/interpolate.d.ts +2 -0
  80. package/math/interpolate.js +16 -0
  81. package/math/interpolate.js.map +1 -0
  82. package/math/number-range.d.ts +6 -0
  83. package/math/number-range.js +13 -0
  84. package/math/number-range.js.map +1 -0
  85. package/math/random-number.d.ts +2 -0
  86. package/math/random-number.js +10 -0
  87. package/math/random-number.js.map +1 -0
  88. package/object/index.d.ts +1 -0
  89. package/object/index.js +2 -0
  90. package/object/index.js.map +1 -0
  91. package/object/merge-deep.d.ts +2 -0
  92. package/object/merge-deep.js +24 -0
  93. package/object/merge-deep.js.map +1 -0
  94. package/package.json +2 -1
  95. package/predicates/is-uuid.d.ts +1 -0
  96. package/promise/delay.d.ts +1 -0
  97. package/promise/delay.js +6 -0
  98. package/promise/delay.js.map +1 -0
  99. package/promise/execute-with-delay.d.ts +1 -1
  100. package/promise/execute-with-delay.js +3 -4
  101. package/promise/execute-with-delay.js.map +1 -1
  102. package/promise/index.d.ts +1 -0
  103. package/promise/index.js +1 -0
  104. package/promise/index.js.map +1 -1
  105. package/types/general.d.ts +1 -0
  106. package/types/index.d.ts +1 -0
  107. package/types/object.d.ts +13 -0
  108. package/types/search.d.ts +24 -0
@@ -0,0 +1,2 @@
1
+ import { LinkedListItem, ItemPredicate, HeadAndItems } from './type';
2
+ export declare const findLinkedList: <Item extends LinkedListItem>(predicate: ItemPredicate<Item>, list: HeadAndItems<Item>) => Item | null;
@@ -0,0 +1,25 @@
1
+ import { walkListItemRecord } from './linked-list-utils.js';
2
+
3
+ var findLinkedList = function (predicate, list) {
4
+ list.headId;
5
+ return walkListItemRecord(function (acc, item) {
6
+ if (predicate(item)) {
7
+ return {
8
+ result: item,
9
+ control: {
10
+ stop: true,
11
+ },
12
+ };
13
+ }
14
+ return {
15
+ result: acc,
16
+ };
17
+ }, {
18
+ initValue: null,
19
+ items: list.items,
20
+ startId: list.headId,
21
+ });
22
+ };
23
+
24
+ export { findLinkedList };
25
+ //# sourceMappingURL=find-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-linked-list.js","sources":["../../src/linked-list/find-linked-list.ts"],"sourcesContent":["import { LinkedListItem, ItemPredicate, HeadAndItems } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const findLinkedList = <Item extends LinkedListItem>(\n predicate: ItemPredicate<Item>,\n list: HeadAndItems<Item>,\n): Item | null => {\n const { headId } = list;\n\n return walkListItemRecord(\n (acc, item) => {\n if (predicate(item)) {\n return {\n result: item,\n control: {\n stop: true,\n },\n };\n };\n\n return {\n result: acc,\n };\n }, {\n initValue: null as Item | null,\n items: list.items,\n startId: list.headId,\n },\n );\n};\n"],"names":[],"mappings":";;AAGa,IAAA,cAAc,GAAG,UAC5B,SAA8B,EAC9B,IAAwB,EAAA;AAEhB,IAAW,IAAI,CAAA,OAAC;AAExB,IAAA,OAAO,kBAAkB,CACvB,UAAC,GAAG,EAAE,IAAI,EAAA;AACR,QAAA,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE;YACnB,OAAO;AACL,gBAAA,MAAM,EAAE,IAAI;AACZ,gBAAA,OAAO,EAAE;AACP,oBAAA,IAAI,EAAE,IAAI;AACX,iBAAA;aACF,CAAC;AACH,SAAA;QAED,OAAO;AACL,YAAA,MAAM,EAAE,GAAG;SACZ,CAAC;AACJ,KAAC,EAAE;AACD,QAAA,SAAS,EAAE,IAAmB;QAC9B,KAAK,EAAE,IAAI,CAAC,KAAK;QACjB,OAAO,EAAE,IAAI,CAAC,MAAM;AACrB,KAAA,CACF,CAAC;AACJ;;;;"}
@@ -0,0 +1,2 @@
1
+ import { HeadAndItems, LinkedListItem } from './type';
2
+ export declare const getLinkedList: <T extends LinkedListItem>(id: string, list: HeadAndItems<T>) => T | null;
@@ -0,0 +1,9 @@
1
+ var getLinkedList = function (id, list) {
2
+ if (list.items[id]) {
3
+ return list.items[id];
4
+ }
5
+ return null;
6
+ };
7
+
8
+ export { getLinkedList };
9
+ //# sourceMappingURL=get-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-linked-list.js","sources":["../../src/linked-list/get-linked-list.ts"],"sourcesContent":["import { HeadAndItems, LinkedListItem } from './type';\n\nexport const getLinkedList = <T extends LinkedListItem>(id: string, list: HeadAndItems<T>) => {\n if (list.items[id]) {\n return list.items[id];\n }\n\n return null;\n}"],"names":[],"mappings":"AAEa,IAAA,aAAa,GAAG,UAA2B,EAAU,EAAE,IAAqB,EAAA;AACvF,IAAA,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE;AAClB,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AACvB,KAAA;AAED,IAAA,OAAO,IAAI,CAAC;AACd;;;;"}
@@ -0,0 +1,2 @@
1
+ export * from './create-linked-list';
2
+ export * from './type';
@@ -0,0 +1,2 @@
1
+ export { createLinkedList } from './create-linked-list.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import { ManipulationConfig, LinkedListItem } from './type';
2
+ export declare const insertLinkedList: <T extends LinkedListItem>(item: T, config: ManipulationConfig<T>) => import("./type").LinkedList<T>;
@@ -0,0 +1,30 @@
1
+ import { __assign } from '../_virtual/_tslib.js';
2
+ import { assertItemCompatibility, assertPresence, findPreviousId, assertNewNodePosition } from './linked-list-utils.js';
3
+
4
+ var insertLinkedList = function (item, config) {
5
+ var _a, _b, _c;
6
+ var isDoubleLinkList = config.isDoubleLinkList, list = config.list, factory = config.factory;
7
+ assertItemCompatibility(item, isDoubleLinkList);
8
+ assertPresence(item.nextId, list.items);
9
+ assertPresence(item.previousId, list.items);
10
+ var previousId = isDoubleLinkList
11
+ ? item.previousId
12
+ : findPreviousId(item.nextId, list);
13
+ assertNewNodePosition(item, {
14
+ previousId: previousId,
15
+ list: list,
16
+ });
17
+ var newHeadId = item.nextId === list.headId ? item.id : list.headId;
18
+ var newItems = __assign(__assign(__assign(__assign({}, list.items), (previousId ? (_a = {},
19
+ _a[previousId] = __assign(__assign({}, list.items[previousId]), { nextId: item.id }),
20
+ _a) : {})), (isDoubleLinkList && item.nextId ? (_b = {},
21
+ _b[item.nextId] = __assign(__assign({}, list.items[item.nextId]), (isDoubleLinkList ? { previousId: item.id } : {})),
22
+ _b) : {})), (_c = {}, _c[item.id] = item, _c));
23
+ return factory({
24
+ headId: newHeadId,
25
+ items: newItems,
26
+ });
27
+ };
28
+
29
+ export { insertLinkedList };
30
+ //# sourceMappingURL=insert-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"insert-linked-list.js","sources":["../../src/linked-list/insert-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\n\nimport {\n findPreviousId,\n assertItemCompatibility,\n assertPresence,\n assertNewNodePosition,\n} from './linked-list-utils';\n\nexport const insertLinkedList = <T extends LinkedListItem>(item: T, config: ManipulationConfig<T>) => {\n const { isDoubleLinkList, list, factory } = config;\n\n assertItemCompatibility(item, isDoubleLinkList);\n assertPresence(item.nextId, list.items);\n assertPresence(item.previousId, list.items);\n\n const previousId = isDoubleLinkList\n ? item.previousId!\n : findPreviousId(item.nextId, list);\n\n assertNewNodePosition(item, {\n previousId,\n list,\n });\n\n const newHeadId = item.nextId === list.headId ? item.id : list.headId;\n const newItems = {\n ...list.items,\n ...(previousId ? {\n [previousId]: {\n ...list.items[previousId],\n nextId: item.id,\n },\n } : {}),\n ...(isDoubleLinkList && item.nextId ? {\n [item.nextId]: {\n ...list.items[item.nextId],\n ...(isDoubleLinkList ? { previousId: item.id } : {}),\n },\n } : {}),\n [item.id]: item,\n };\n\n return factory({\n headId: newHeadId,\n items: newItems,\n });\n};\n"],"names":[],"mappings":";;;AASa,IAAA,gBAAgB,GAAG,UAA2B,IAAO,EAAE,MAA6B,EAAA;;AACvF,IAAA,IAAA,gBAAgB,GAAoB,MAAM,CAAA,gBAA1B,EAAE,IAAI,GAAc,MAAM,CAAA,IAApB,EAAE,OAAO,GAAK,MAAM,QAAX,CAAY;AAEnD,IAAA,uBAAuB,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;IAChD,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IACxC,cAA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@
1
+ import { LinkedListItem, HeadAndItems } from './type';
2
+ export declare type WalkConfig<T extends LinkedListItem, Result> = {
3
+ startId: string | null;
4
+ items: Record<string, T>;
5
+ initValue: Result;
6
+ };
7
+ declare type WalkCallbackResult<R> = {
8
+ result: R;
9
+ control?: {
10
+ stop?: boolean;
11
+ };
12
+ };
13
+ export declare type WalkCallback<Item, Result> = (accumulator: Result, current: Item) => WalkCallbackResult<Result>;
14
+ export declare const walkListItemRecord: <T extends LinkedListItem, Result>(callback: WalkCallback<T, Result>, config: WalkConfig<T, Result>) => Result;
15
+ export declare const findPreviousId: <T extends LinkedListItem>(id: string | null, list: HeadAndItems<T>) => string | null;
16
+ export declare const assertItemCompatibility: <T extends LinkedListItem>(item: T, isDoubleLinkList: boolean) => void;
17
+ export declare const assertPresence: (id: string | null | undefined, items: Record<string, unknown>) => void;
18
+ declare type PositionAssertionConfig = {
19
+ previousId: string | null;
20
+ list: HeadAndItems<LinkedListItem>;
21
+ };
22
+ export declare const assertNewNodePosition: <T extends LinkedListItem>(item: T, config: PositionAssertionConfig) => void;
23
+ export {};
@@ -0,0 +1,97 @@
1
+ import { runWhile } from '../fp/run-while.js';
2
+
3
+ var walkListItemRecord = function (callback, config) {
4
+ var _a;
5
+ var items = config.items, startId = config.startId, initValue = config.initValue;
6
+ var visited = new Set();
7
+ var totalNodes = Object.keys(items).length;
8
+ var _b = runWhile({
9
+ iterationLimit: totalNodes + 1,
10
+ initialValue: {
11
+ currentId: startId,
12
+ accumulator: initValue,
13
+ control: {
14
+ stop: false,
15
+ },
16
+ },
17
+ predicate: function (_a) {
18
+ var control = _a.control, currentId = _a.currentId;
19
+ var stop = control === null || control === void 0 ? void 0 : control.stop;
20
+ var forcedStop = stop === undefined ? false : stop;
21
+ /**
22
+ * Stop early or when the end is reached
23
+ */
24
+ return !forcedStop && currentId !== null;
25
+ },
26
+ callback: function (current) {
27
+ if (visited.has(current.currentId)) {
28
+ throw new Error('Infinite loop found');
29
+ }
30
+ visited.add(current.currentId);
31
+ var currentItem = items[current.currentId];
32
+ if (!currentItem) {
33
+ throw new Error('Cul de sac detected');
34
+ }
35
+ var _a = callback(current.accumulator, currentItem), result = _a.result, control = _a.control;
36
+ return {
37
+ accumulator: result,
38
+ currentId: currentItem.nextId,
39
+ control: control,
40
+ };
41
+ },
42
+ }), iterationCount = _b.iterationCount, result = _b.result;
43
+ /**
44
+ * When force stopping the walk, we will always have unvisited
45
+ * nodes, so we skip this check.
46
+ */
47
+ if (iterationCount !== totalNodes && ((_a = result.control) === null || _a === void 0 ? void 0 : _a.stop) !== true) {
48
+ throw new Error('Isolated nodes found');
49
+ }
50
+ return result.accumulator;
51
+ };
52
+ var findPreviousId = function (id, list) {
53
+ if (id === list.headId) {
54
+ return null;
55
+ }
56
+ return walkListItemRecord(function (acc, current) {
57
+ if (current.nextId === id) {
58
+ return {
59
+ result: current.id,
60
+ control: {
61
+ stop: true,
62
+ },
63
+ };
64
+ }
65
+ return {
66
+ result: acc,
67
+ };
68
+ }, {
69
+ initValue: null,
70
+ startId: list.headId,
71
+ items: list.items,
72
+ });
73
+ };
74
+ var assertItemCompatibility = function (item, isDoubleLinkList) {
75
+ var incompatibleWithDoubleLinks = item.previousId === undefined && isDoubleLinkList;
76
+ var incompatibleWithSingleLink = item.previousId !== undefined && !isDoubleLinkList;
77
+ if (incompatibleWithDoubleLinks || incompatibleWithSingleLink) {
78
+ throw new Error("Node is incompatible with the list (id: ".concat(item.id, ")"));
79
+ }
80
+ };
81
+ var assertPresence = function (id, items) {
82
+ if (id && !items[id]) {
83
+ throw new Error("Node not found in the list (id: ".concat(id, ")"));
84
+ }
85
+ };
86
+ var assertNewNodePosition = function (item, config) {
87
+ var previousId = config.previousId, list = config.list;
88
+ var insertionMiddleError = previousId && list.items[previousId].nextId !== item.nextId;
89
+ var insertionTailError = previousId && item.nextId === null && list.items[previousId].nextId !== null;
90
+ var insertionHeadError = previousId === null && item.nextId !== list.headId;
91
+ if (insertionMiddleError || insertionTailError || insertionHeadError) {
92
+ throw new Error("Invalid position for given node (id: ".concat(item.id, ")"));
93
+ }
94
+ };
95
+
96
+ export { assertItemCompatibility, assertNewNodePosition, assertPresence, findPreviousId, walkListItemRecord };
97
+ //# sourceMappingURL=linked-list-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linked-list-utils.js","sources":["../../src/linked-list/linked-list-utils.ts"],"sourcesContent":["import { runWhile } from '../fp';\nimport { LinkedListItem, HeadAndItems} from './type';\n\nexport type WalkConfig<T extends LinkedListItem, Result> = {\n startId: string | null,\n items: Record<string, T>,\n initValue: Result,\n}\n\ntype WalkCallbackResult<R> = {\n result: R,\n control?: {\n stop?: boolean,\n },\n};\n\nexport type WalkCallback<Item, Result> = (accumulator: Result, current: Item) => WalkCallbackResult<Result>;\n\ntype RunWhileValue<T> = {\n currentId: string | null,\n accumulator: T,\n control: WalkCallbackResult<any>['control'],\n}\n\nexport const walkListItemRecord = <T extends LinkedListItem, Result>(\n callback: WalkCallback<T, Result>,\n config: WalkConfig<T, Result>,\n) => {\n const { items, startId, initValue } = config;\n const visited = new Set();\n const totalNodes = Object.keys(items).length;\n\n const { iterationCount, result } = runWhile({\n iterationLimit: totalNodes + 1,\n initialValue: {\n currentId: startId, \n accumulator: initValue,\n control: {\n stop: false,\n },\n } as RunWhileValue<Result>,\n predicate: ({ control, currentId }) => {\n const stop = control?.stop;\n const forcedStop = stop === undefined ? false : stop;\n\n /**\n * Stop early or when the end is reached\n */\n return !forcedStop && currentId !== null;\n },\n callback: (current) => {\n if (visited.has(current.currentId)) {\n throw new Error('Infinite loop found');\n }\n\n visited.add(current.currentId);\n\n const currentItem = items[current.currentId!];\n\n if (!currentItem) {\n throw new Error('Cul de sac detected');\n }\n\n const { result, control } = callback(current.accumulator, currentItem);\n\n return {\n accumulator: result,\n currentId: currentItem.nextId,\n control: control,\n };\n },\n });\n\n /**\n * When force stopping the walk, we will always have unvisited\n * nodes, so we skip this check.\n */\n if (iterationCount !== totalNodes && result.control?.stop !== true) {\n throw new Error('Isolated nodes found');\n }\n\n return result.accumulator;\n};\n\nexport const findPreviousId = <T extends LinkedListItem>(id: string | null, list: HeadAndItems<T>) => {\n if (id === list.headId) {\n return null;\n }\n\n return walkListItemRecord(\n (acc, current) => {\n if (current.nextId === id) {\n return {\n result: current.id,\n control: {\n stop: true,\n },\n };\n }\n\n return {\n result: acc,\n }\n },\n {\n initValue: null as null | string,\n startId: list.headId,\n items: list.items,\n }\n )\n};\n\nexport const assertItemCompatibility = <T extends LinkedListItem>(item: T, isDoubleLinkList: boolean) => {\n const incompatibleWithDoubleLinks = item.previousId === undefined && isDoubleLinkList;\n const incompatibleWithSingleLink = item.previousId !== undefined && !isDoubleLinkList;\n\n if (incompatibleWithDoubleLinks || incompatibleWithSingleLink) {\n throw new Error(`Node is incompatible with the list (id: ${item.id})`);\n }\n}\n\nexport const assertPresence = (id: string | null | undefined, items: Record<string, unknown>) => {\n if (id && !items[id]) {\n throw new Error(`Node not found in the list (id: ${id})`);\n }\n}\n\ntype PositionAssertionConfig = {\n previousId: string | null,\n list: HeadAndItems<LinkedListItem>,\n};\n\nexport const assertNewNodePosition = <T extends LinkedListItem>(item: T, config: PositionAssertionConfig) => {\n const { previousId, list } = config;\n\n const insertionMiddleError = previousId && list.items[previousId].nextId !== item.nextId;\n const insertionTailError = previousId && item.nextId === null && list.items[previousId].nextId !== null;\n const insertionHeadError = previousId === null && item.nextId !== list.headId;\n\n if (insertionMiddleError || insertionTailError || insertionHeadError) {\n throw new Error(`Invalid position for given node (id: ${item.id})`);\n }\n}"],"names":[],"mappings":";;AAwBa,IAAA,kBAAkB,GAAG,UAChC,QAAiC,EACjC,MAA6B,EAAA;;AAErB,IAAA,IAAA,KAAK,GAAyB,MAAM,CAAA,KAA/B,EAAE,OAAO,GAAgB,MAAM,CAAA,OAAtB,EAAE,SAAS,GAAK,MAAM,UAAX,CAAY;AAC7C,IAAA,IAAM,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC1B,IAAM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E,KAAA;AACH;;;;"}
@@ -0,0 +1,2 @@
1
+ import { ReduceCallback, LinkedListItem, HeadAndItems } from './type';
2
+ export declare const reduceLinkedList: <Item extends LinkedListItem, Result>(cb: ReduceCallback<Item, Result>, initValue: Result, list: HeadAndItems<Item>) => Result;
@@ -0,0 +1,17 @@
1
+ import { walkListItemRecord } from './linked-list-utils.js';
2
+
3
+ var reduceLinkedList = function (cb, initValue, list) {
4
+ var items = list.items, headId = list.headId;
5
+ return walkListItemRecord(function (acc, current) {
6
+ return {
7
+ result: cb(acc, current),
8
+ };
9
+ }, {
10
+ startId: headId,
11
+ items: items,
12
+ initValue: initValue,
13
+ });
14
+ };
15
+
16
+ export { reduceLinkedList };
17
+ //# sourceMappingURL=reduce-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reduce-linked-list.js","sources":["../../src/linked-list/reduce-linked-list.ts"],"sourcesContent":["import { ReduceCallback, LinkedListItem, HeadAndItems } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const reduceLinkedList = <Item extends LinkedListItem, Result>(\n cb: ReduceCallback<Item, Result>,\n initValue: Result,\n list: HeadAndItems<Item>,\n): Result => {\n const { items, headId } = list;\n\n return walkListItemRecord((acc, current) => {\n return {\n result: cb(acc, current),\n };\n }, {\n startId: headId,\n items,\n initValue,\n });\n};\n"],"names":[],"mappings":";;IAGa,gBAAgB,GAAG,UAC9B,EAAgC,EAChC,SAAiB,EACjB,IAAwB,EAAA;IAEhB,IAAA,KAAK,GAAa,IAAI,CAAA,KAAjB,EAAE,MAAM,GAAK,IAAI,CAAA,MAAT,CAAU;AAE/B,IAAA,OAAO,kBAAkB,CAAC,UAAC,GAAG,EAAE,OAAO,EAAA;QACrC,OAAO;AACL,YAAA,MAAM,EAAE,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC;SACzB,CAAC;AACJ,KAAC,EAAE;AACD,QAAA,OAAO,EAAE,MAAM;AACf,QAAA,KAAK,EAAA,KAAA;AACL,QAAA,SAAS,EAAA,SAAA;AACV,KAAA,CAAC,CAAC;AACL;;;;"}
@@ -0,0 +1,2 @@
1
+ import { ManipulationConfig, LinkedListItem } from './type';
2
+ export declare const removeLinkedList: <T extends LinkedListItem>(id: string, config: ManipulationConfig<T>) => import("./type").LinkedList<T>;
@@ -0,0 +1,25 @@
1
+ import { __rest, __assign } from '../_virtual/_tslib.js';
2
+ import { assertPresence, findPreviousId } from './linked-list-utils.js';
3
+
4
+ var removeLinkedList = function (id, config) {
5
+ var _a, _b;
6
+ var isDoubleLinkList = config.isDoubleLinkList, list = config.list, factory = config.factory;
7
+ var _c = list.items, _d = id, itemToDelete = _c[_d], otherItems = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
8
+ assertPresence(id, list.items);
9
+ var previousId = isDoubleLinkList
10
+ ? itemToDelete.previousId
11
+ : findPreviousId(id, list);
12
+ var newHeadId = previousId === null ? itemToDelete.nextId : list.headId;
13
+ var newItems = __assign(__assign(__assign({}, otherItems), (previousId ? (_a = {},
14
+ _a[previousId] = __assign(__assign({}, otherItems[previousId]), { nextId: itemToDelete.nextId }),
15
+ _a) : {})), (isDoubleLinkList && itemToDelete.nextId ? (_b = {},
16
+ _b[itemToDelete.nextId] = __assign(__assign({}, otherItems[itemToDelete.nextId]), (isDoubleLinkList ? { previousId: previousId } : {})),
17
+ _b) : {}));
18
+ return factory({
19
+ headId: newHeadId,
20
+ items: newItems,
21
+ });
22
+ };
23
+
24
+ export { removeLinkedList };
25
+ //# sourceMappingURL=remove-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-linked-list.js","sources":["../../src/linked-list/remove-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\nimport { assertPresence, findPreviousId } from './linked-list-utils';\n\nexport const removeLinkedList = <T extends LinkedListItem>(id: string, config: ManipulationConfig<T>) => {\n const { isDoubleLinkList, list, factory } = config;\n const { [id]: itemToDelete, ...otherItems } = list.items;\n\n assertPresence(id, list.items);\n\n const previousId = isDoubleLinkList\n ? itemToDelete.previousId!\n : findPreviousId(id, list);\n\n const newHeadId = previousId === null ? itemToDelete.nextId : list.headId;\n const newItems = {\n ...otherItems,\n ...(previousId ? {\n [previousId]: {\n ...otherItems[previousId],\n nextId: itemToDelete.nextId,\n },\n } : {}),\n ...(isDoubleLinkList && itemToDelete.nextId ? {\n [itemToDelete.nextId]: {\n ...otherItems[itemToDelete.nextId],\n ...(isDoubleLinkList ? { previousId } : {}),\n }\n } : {}),\n };\n\n return factory({\n headId: newHeadId,\n items: newItems,\n });\n};"],"names":[],"mappings":";;;AAGa,IAAA,gBAAgB,GAAG,UAA2B,EAAU,EAAE,MAA6B,EAAA;;AAC1F,IAAA,IAAA,gBAAgB,GAAoB,MAAM,CAAA,gBAA1B,EAAE,IAAI,GAAc,MAAM,CAAA,IAApB,EAAE,OAAO,GAAK,MAAM,QAAX,CAAY;AACnD,IAAA,IAA8C,EAAA,GAAA,IAAI,CAAC,KAAK,EAAhD,EAAC,GAAA,EAAG,EAAE,YAAY,SAAA,EAAK,UAAU,GAAnC,MAAA,CAAA,EAAA,EAAA,CAAA,OAAA,EAAA,KAAA,QAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EAAA,CAAqC,CAAa,CAAC;AAEzD,IAAA,cAAc,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IAE/B,IAAM,UAAU,GAAG,gBAAgB;UAC/B,YAAY,CAAC,UAAW;AAC1B,UAAE,cAAc,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;AAE7B,IAAA,IAAM,SAAS,GAAG,UAAU,KAAK,IAAI,GAAG,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;AAC1E,IAAA,IAAM,QAAQ,GACT,QAAA,CAAA,QAAA,CAAA,QAAA,CAAA,EAAA,EAAA,UAAU,IACT,UAAU,IAAE,EAAA,GAAA,EAAA;AACd,QAAA,EAAA,CAAC,UAAU,CAAA,GAAA,QAAA,CAAA,QAAA,CAAA,EAAA,EACN,UAAU,CAAC,UAAU,CAAC,CACzB,EAAA,EAAA,MAAM,EAAE,YAAY,CAAC,MAAM,EAC5B,CAAA;cACC,EAAE,EACH,GAAC,gBAAgB,IAAI,YAAY,CAAC,MAAM,IAAE,EAAA,GAAA,EAAA;QAC3C,EAAC,CAAA,YAAY,CAAC,MAAM,CACf,GAAA,QAAA,CAAA,QAAA,CAAA,EAAA,EAAA,UAAU,CAAC,YAAY,CAAC,MAAM,CAAC,CAC/B,GAAC,gBAAgB,GAAG,EAAE,UAAU,EAAA,UAAA,EAAE,GAAG,EAAE,EAC3C;AACD,QAAA,EAAA,IAAE,EAAE,EACP,CAAC;AAEF,IAAA,OAAO,OAAO,CAAC;AACb,QAAA,MAAM,EAAE,SAAS;AACjB,QAAA,KAAK,EAAE,QAAQ;AAChB,KAAA,CAAC,CAAC;AACL;;;;"}
@@ -0,0 +1,34 @@
1
+ export declare type LinkedListItem = {
2
+ id: string;
3
+ nextId: string | null;
4
+ previousId?: string | null;
5
+ };
6
+ export declare type HeadAndItems<T extends LinkedListItem> = {
7
+ headId: string | null;
8
+ items: {
9
+ [id: string]: T;
10
+ };
11
+ };
12
+ export declare type ReduceCallback<Item, Result> = (accumulator: Result, current: Item) => Result;
13
+ export declare type ItemPredicate<Item> = (item: Item) => boolean;
14
+ export declare type ManipulationConfig<T extends LinkedListItem> = {
15
+ isDoubleLinkList: boolean;
16
+ list: HeadAndItems<T>;
17
+ factory: (list: HeadAndItems<T>) => LinkedList<T>;
18
+ };
19
+ export declare type LinkedList<T extends LinkedListItem> = {
20
+ readonly head: T | null;
21
+ readonly items: {
22
+ [id: string]: T;
23
+ };
24
+ readonly size: number;
25
+ readonly isDoubleLinked: boolean;
26
+ readonly reduce: <Result>(cb: ReduceCallback<T, Result>, init: Result) => Result;
27
+ readonly find: (predicate: ItemPredicate<T>) => T | null;
28
+ readonly while: (predicate: ItemPredicate<T>, startId?: string) => string[];
29
+ readonly remove: (id: string) => LinkedList<T>;
30
+ readonly insert: (item: T) => LinkedList<T>;
31
+ readonly get: (id: string) => T | null;
32
+ readonly update: (item: T) => LinkedList<T>;
33
+ readonly applyDiff: (before: T | null, after: T | null) => LinkedList<T>;
34
+ };
@@ -0,0 +1,2 @@
1
+ import { ManipulationConfig, LinkedListItem } from './type';
2
+ export declare const updateLinkedList: <T extends LinkedListItem>(item: T, config: ManipulationConfig<T>) => import("./type").LinkedList<T>;
@@ -0,0 +1,21 @@
1
+ import { __rest, __assign } from '../_virtual/_tslib.js';
2
+ import { assertPresence } from './linked-list-utils.js';
3
+
4
+ var updateLinkedList = function (item, config) {
5
+ var _a;
6
+ var list = config.list, factory = config.factory;
7
+ assertPresence(item.id, list.items);
8
+ /**
9
+ * This method should not update any ids.
10
+ */
11
+ item.nextId; item.previousId; item.id; var updatableProperties = __rest(item, ["nextId", "previousId", "id"]);
12
+ var currentItem = list.items[item.id];
13
+ var newItems = __assign(__assign({}, list.items), (_a = {}, _a[item.id] = __assign(__assign({}, currentItem), updatableProperties), _a));
14
+ return factory({
15
+ headId: list.headId,
16
+ items: newItems,
17
+ });
18
+ };
19
+
20
+ export { updateLinkedList };
21
+ //# sourceMappingURL=update-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-linked-list.js","sources":["../../src/linked-list/update-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\nimport { assertPresence } from './linked-list-utils';\n\nexport const updateLinkedList = <T extends LinkedListItem>(item: T, config: ManipulationConfig<T>) => {\n const { list, factory } = config;\n\n assertPresence(item.id, list.items);\n\n /**\n * This method should not update any ids.\n */\n const { nextId, previousId, id, ...updatableProperties } = item;\n const currentItem = list.items[item.id];\n\n const newItems = {\n ...list.items,\n [item.id]: {\n ...currentItem,\n ...updatableProperties,\n },\n };\n\n return factory({\n headId: list.headId,\n items: newItems,\n });\n};\n"],"names":[],"mappings":";;;AAGa,IAAA,gBAAgB,GAAG,UAA2B,IAAO,EAAG,MAA6B,EAAA;;IACxF,IAAA,IAAI,GAAc,MAAM,CAAA,IAApB,EAAE,OAAO,GAAK,MAAM,CAAA,OAAX,CAAY;IAEjC,cAAc,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;AAEpC;;AAEG;IACwD,IAAI,CAAA,MAAjD,EAA6C,IAAI,CAArC,UAAA,CAAA,CAAiC,IAAI,GAAjC,CAAK,KAAA,mBAAmB,UAAK,IAAI,EAAzD,CAAkD,QAAA,EAAA,YAAA,EAAA,IAAA,CAAA,EAAQ;IAChE,IAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAExC,IAAA,IAAM,QAAQ,GAAA,QAAA,CAAA,QAAA,CAAA,EAAA,EACT,IAAI,CAAC,KAAK,CACZ,GAAA,EAAA,GAAA,EAAA,EAAA,EAAA,CAAA,IAAI,CAAC,EAAE,CACH,GAAA,QAAA,CAAA,QAAA,CAAA,EAAA,EAAA,WAAW,CACX,EAAA,mBAAmB,OAEzB,CAAC;AAEF,IAAA,OAAO,OAAO,CAAC;QACb,MAAM,EAAE,IAAI,CAAC,MAAM;AACnB,QAAA,KAAK,EAAE,QAAQ;AAChB,KAAA,CAAC,CAAC;AACL;;;;"}
@@ -0,0 +1,15 @@
1
+ import { LinkedListItem } from './type';
2
+ export declare type WalkConfig<T extends LinkedListItem, Result> = {
3
+ startId: string | null;
4
+ items: Record<string, T>;
5
+ initValue: Result;
6
+ };
7
+ declare type WalkCallbackResult<R> = {
8
+ result: R;
9
+ control?: {
10
+ stop?: boolean;
11
+ };
12
+ };
13
+ export declare type WalkCallback<Item, Result> = (accumulator: Result, current: Item) => WalkCallbackResult<Result>;
14
+ export declare const walkListItemRecord: <T extends LinkedListItem, Result>(callback: WalkCallback<T, Result>, config: WalkConfig<T, Result>) => Result;
15
+ export {};
@@ -0,0 +1,2 @@
1
+ import { LinkedListItem, ItemPredicate, HeadAndItems } from './type';
2
+ export declare const whileLinkedList: <Item extends LinkedListItem>(predicate: ItemPredicate<Item>, list: HeadAndItems<Item>, startId?: string) => string[];
@@ -0,0 +1,29 @@
1
+ import { __spreadArray } from '../_virtual/_tslib.js';
2
+ import { walkListItemRecord } from './linked-list-utils.js';
3
+
4
+ var whileLinkedList = function (predicate, list, startId) {
5
+ if (startId && !list.items[startId]) {
6
+ throw new Error('Cannot start from a node that does not exist');
7
+ }
8
+ return walkListItemRecord(function (acc, item) {
9
+ var itemMatched = predicate(item);
10
+ if (itemMatched) {
11
+ return {
12
+ result: __spreadArray(__spreadArray([], acc, true), [item.id], false),
13
+ };
14
+ }
15
+ return {
16
+ result: acc,
17
+ control: {
18
+ stop: true,
19
+ },
20
+ };
21
+ }, {
22
+ initValue: [],
23
+ startId: startId || list.headId,
24
+ items: list.items,
25
+ });
26
+ };
27
+
28
+ export { whileLinkedList };
29
+ //# sourceMappingURL=while-linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"while-linked-list.js","sources":["../../src/linked-list/while-linked-list.ts"],"sourcesContent":["\n\nimport { LinkedListItem, ItemPredicate, HeadAndItems } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const whileLinkedList = <Item extends LinkedListItem>(\n predicate: ItemPredicate<Item>,\n list: HeadAndItems<Item>,\n startId?: string,\n): string[] => {\n if (startId && !list.items[startId]) {\n throw new Error('Cannot start from a node that does not exist');\n }\n\n return walkListItemRecord(\n (acc, item) => {\n const itemMatched = predicate(item);\n\n if (itemMatched) {\n return {\n result: [...acc, item.id],\n };\n }\n\n return {\n result: acc,\n control: {\n stop: true,\n },\n };\n }, {\n initValue: [] as string[],\n startId: startId || list.headId,\n items: list.items,\n },\n );\n};\n"],"names":[],"mappings":";;;IAKa,eAAe,GAAG,UAC7B,SAA8B,EAC9B,IAAwB,EACxB,OAAgB,EAAA;IAEhB,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;AACnC,QAAA,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;AACjE,KAAA;AAED,IAAA,OAAO,kBAAkB,CACvB,UAAC,GAAG,EAAE,IAAI,EAAA;AACR,QAAA,IAAM,WAAW,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;AAEpC,QAAA,IAAI,WAAW,EAAE;YACf,OAAO;AACL,gBAAA,MAAM,kCAAM,GAAG,EAAA,IAAA,CAAA,EAAA,CAAE,IAAI,CAAC,EAAE,CAAC,EAAA,KAAA,CAAA;aAC1B,CAAC;AACH,SAAA;QAED,OAAO;AACL,YAAA,MAAM,EAAE,GAAG;AACX,YAAA,OAAO,EAAE;AACP,gBAAA,IAAI,EAAE,IAAI;AACX,aAAA;SACF,CAAC;AACJ,KAAC,EAAE;AACD,QAAA,SAAS,EAAE,EAAc;AACzB,QAAA,OAAO,EAAE,OAAO,IAAI,IAAI,CAAC,MAAM;QAC/B,KAAK,EAAE,IAAI,CAAC,KAAK;AAClB,KAAA,CACF,CAAC;AACJ;;;;"}
@@ -0,0 +1,2 @@
1
+ import type { NumberRange } from './number-range';
2
+ export declare const clampNumber: (num: number, { min, max }: NumberRange) => number;
@@ -0,0 +1,10 @@
1
+ import { assertNumberRange } from './number-range.js';
2
+
3
+ var clampNumber = function (num, _a) {
4
+ var min = _a.min, max = _a.max;
5
+ assertNumberRange({ min: min, max: max });
6
+ return Math.min(Math.max(num, min), max);
7
+ };
8
+
9
+ export { clampNumber };
10
+ //# sourceMappingURL=clamp-number.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clamp-number.js","sources":["../../src/math/clamp-number.ts"],"sourcesContent":["import { assertNumberRange } from './number-range';\nimport type { NumberRange } from './number-range';\n\nexport const clampNumber = (num: number, { min, max }: NumberRange) => {\n assertNumberRange({ min, max });\n\n return Math.min(Math.max(num, min), max);\n};"],"names":[],"mappings":";;AAGa,IAAA,WAAW,GAAG,UAAC,GAAW,EAAE,EAAyB,EAAA;QAAvB,GAAG,GAAA,EAAA,CAAA,GAAA,EAAE,GAAG,GAAA,EAAA,CAAA,GAAA,CAAA;IACjD,iBAAiB,CAAC,EAAE,GAAG,EAAA,GAAA,EAAE,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;AAEhC,IAAA,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAC3C;;;;"}
@@ -0,0 +1,4 @@
1
+ export * from './clamp-number';
2
+ export * from './interpolate';
3
+ export * from './number-range';
4
+ export * from './random-number';
package/math/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { clampNumber } from './clamp-number.js';
2
+ export { interpolate } from './interpolate.js';
3
+ export { assertNumberRange, isNumberInRange } from './number-range.js';
4
+ export { randomNumber } from './random-number.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
@@ -0,0 +1,2 @@
1
+ export declare type RangeArray = [number, number];
2
+ export declare const interpolate: (num: number, inputRange: RangeArray, outputRange: RangeArray) => number;
@@ -0,0 +1,16 @@
1
+ import { isNumberInRange } from './number-range.js';
2
+
3
+ var interpolate = function (num, inputRange, outputRange) {
4
+ var x1 = inputRange[0], x2 = inputRange[1];
5
+ var y1 = outputRange[0], y2 = outputRange[1];
6
+ if (x1 > x2) {
7
+ throw new Error('interpolate: inputRange[0] should not be superior to inputRange[1]');
8
+ }
9
+ if (!isNumberInRange(num, { min: x1, max: x2 })) {
10
+ throw new Error("interpolate: input number {".concat(num, "} is not in range of {").concat(x1, "}-{").concat(x2, "}"));
11
+ }
12
+ return y1 + ((num - x1) / (x2 - x1)) * (y2 - y1);
13
+ };
14
+
15
+ export { interpolate };
16
+ //# sourceMappingURL=interpolate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolate.js","sources":["../../src/math/interpolate.ts"],"sourcesContent":["import { isNumberInRange } from './number-range';\n\nexport type RangeArray = [number, number];\n\nexport const interpolate = (num: number, inputRange: RangeArray, outputRange: RangeArray) => {\n const [x1, x2] = inputRange;\n const [y1, y2] = outputRange;\n\n if (x1 > x2) {\n throw new Error('interpolate: inputRange[0] should not be superior to inputRange[1]');\n }\n\n if (!isNumberInRange(num, { min: x1, max: x2 })) {\n throw new Error(`interpolate: input number {${num}} is not in range of {${x1}}-{${x2}}`);\n }\n\n return y1 + ((num - x1) / (x2 - x1)) * (y2 - y1);\n};"],"names":[],"mappings":";;IAIa,WAAW,GAAG,UAAC,GAAW,EAAE,UAAsB,EAAE,WAAuB,EAAA;IAC/E,IAAA,EAAE,GAAQ,UAAU,CAAA,CAAA,CAAlB,EAAE,EAAE,GAAI,UAAU,CAAA,CAAA,CAAd,CAAe;IACrB,IAAA,EAAE,GAAQ,WAAW,CAAA,CAAA,CAAnB,EAAE,EAAE,GAAI,WAAW,CAAA,CAAA,CAAf,CAAgB;IAE7B,IAAI,EAAE,GAAG,EAAE,EAAE;AACX,QAAA,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;AACvF,KAAA;AAED,IAAA,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE;QAC/C,MAAM,IAAI,KAAK,CAAC,6BAA8B,CAAA,MAAA,CAAA,GAAG,EAAyB,wBAAA,CAAA,CAAA,MAAA,CAAA,EAAE,EAAM,KAAA,CAAA,CAAA,MAAA,CAAA,EAAE,EAAG,GAAA,CAAA,CAAC,CAAC;AAC1F,KAAA;IAED,OAAO,EAAE,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;AACnD;;;;"}
@@ -0,0 +1,6 @@
1
+ export declare type NumberRange = {
2
+ min: number;
3
+ max: number;
4
+ };
5
+ export declare const assertNumberRange: (range: NumberRange) => void;
6
+ export declare const isNumberInRange: (num: number, { min, max }: NumberRange) => boolean;
@@ -0,0 +1,13 @@
1
+ var assertNumberRange = function (range) {
2
+ if (range.min > range.max) {
3
+ throw new Error('Invalid number range: min must be lower than or equal to max');
4
+ }
5
+ };
6
+ var isNumberInRange = function (num, _a) {
7
+ var min = _a.min, max = _a.max;
8
+ assertNumberRange({ min: min, max: max });
9
+ return num >= min && num <= max;
10
+ };
11
+
12
+ export { assertNumberRange, isNumberInRange };
13
+ //# sourceMappingURL=number-range.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number-range.js","sources":["../../src/math/number-range.ts"],"sourcesContent":["export type NumberRange = {\n min: number,\n max: number,\n};\n\nexport const assertNumberRange = (range: NumberRange) => {\n if (range.min > range.max) {\n throw new Error('Invalid number range: min must be lower than or equal to max');\n }\n};\n\nexport const isNumberInRange = (num: number, { min, max }: NumberRange) => {\n assertNumberRange({ min, max });\n\n return num >= min && num <= max;\n};\n"],"names":[],"mappings":"AAKO,IAAM,iBAAiB,GAAG,UAAC,KAAkB,EAAA;AAClD,IAAA,IAAI,KAAK,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,EAAE;AACzB,QAAA,MAAM,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAC;AACjF,KAAA;AACH,EAAE;AAEW,IAAA,eAAe,GAAG,UAAC,GAAW,EAAE,EAAyB,EAAA;QAAvB,GAAG,GAAA,EAAA,CAAA,GAAA,EAAE,GAAG,GAAA,EAAA,CAAA,GAAA,CAAA;IACrD,iBAAiB,CAAC,EAAE,GAAG,EAAA,GAAA,EAAE,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;AAEhC,IAAA,OAAO,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,CAAC;AAClC;;;;"}
@@ -0,0 +1,2 @@
1
+ import type { NumberRange } from './number-range';
2
+ export declare const randomNumber: ({ min, max }: NumberRange) => number;
@@ -0,0 +1,10 @@
1
+ import { assertNumberRange } from './number-range.js';
2
+
3
+ var randomNumber = function (_a) {
4
+ var min = _a.min, max = _a.max;
5
+ assertNumberRange({ min: min, max: max });
6
+ return Math.floor(Math.random() * (max - min) + min);
7
+ };
8
+
9
+ export { randomNumber };
10
+ //# sourceMappingURL=random-number.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-number.js","sources":["../../src/math/random-number.ts"],"sourcesContent":["import { assertNumberRange } from './number-range';\nimport type { NumberRange } from './number-range';\n\nexport const randomNumber = ({ min, max }: NumberRange) => {\n assertNumberRange({ min, max });\n\n return Math.floor(\n Math.random() * (max - min) + min,\n );\n};\n"],"names":[],"mappings":";;AAGO,IAAM,YAAY,GAAG,UAAC,EAAyB,EAAA;QAAvB,GAAG,GAAA,EAAA,CAAA,GAAA,EAAE,GAAG,GAAA,EAAA,CAAA,GAAA,CAAA;IACrC,iBAAiB,CAAC,EAAE,GAAG,EAAA,GAAA,EAAE,GAAG,EAAA,GAAA,EAAE,CAAC,CAAC;AAEhC,IAAA,OAAO,IAAI,CAAC,KAAK,CACf,IAAI,CAAC,MAAM,EAAE,IAAI,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,CAClC,CAAC;AACJ;;;;"}
@@ -0,0 +1 @@
1
+ export * from './merge-deep';
@@ -0,0 +1,2 @@
1
+ export { mergeDeep } from './merge-deep.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import type { DeepPartial } from '../types';
2
+ export declare const mergeDeep: <T>(a: T, b: DeepPartial<T>) => T;
@@ -0,0 +1,24 @@
1
+ import { __assign } from '../_virtual/_tslib.js';
2
+
3
+ // https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript/8511350#8511350
4
+ var isObject = function (a) { return typeof a === 'object' && a !== null; };
5
+ var isValueType = function (a) { return (!isObject(a) || Array.isArray(a)); };
6
+ var mergeDeep = function (a, b) {
7
+ if (isValueType(a) || isValueType(b)) {
8
+ return b;
9
+ }
10
+ return Object.keys(b).reduce(function (acc, key) {
11
+ var _a;
12
+ if (!(key in a)) {
13
+ return acc;
14
+ }
15
+ if (b[key] === a[key]) {
16
+ return acc;
17
+ }
18
+ var merged = mergeDeep(a[key], b[key]);
19
+ return __assign(__assign({}, acc), (_a = {}, _a[key] = merged, _a));
20
+ }, a);
21
+ };
22
+
23
+ export { mergeDeep };
24
+ //# sourceMappingURL=merge-deep.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merge-deep.js","sources":["../../src/object/merge-deep.ts"],"sourcesContent":["import type { DeepPartial, ValueType } from '../types';\n\n// https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript/8511350#8511350\nconst isObject = (a: any) => typeof a === 'object' && a !== null;\nconst isValueType = (a: any): a is ValueType => (\n !isObject(a) || Array.isArray(a)\n);\n\nexport const mergeDeep = <T>(a: T, b: DeepPartial<T>): T => {\n if (isValueType(a) || isValueType(b)) {\n return b as T;\n }\n\n return (Object.keys(b) as (keyof T)[]).reduce((acc, key) => {\n if (!(key in a)) {\n return acc;\n }\n\n if (b[key] === a[key]) {\n return acc;\n }\n\n const merged = mergeDeep(a[key], (b[key] as DeepPartial<T[keyof T]>));\n\n return { ...acc, [key]: merged };\n }, a);\n};\n"],"names":[],"mappings":";;AAEA;AACA,IAAM,QAAQ,GAAG,UAAC,CAAM,EAAA,EAAK,OAAA,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,CAAA,EAAA,CAAC;AACjE,IAAM,WAAW,GAAG,UAAC,CAAM,IAAqB,QAC9C,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EADc,EAE/C,CAAC;AAEW,IAAA,SAAS,GAAG,UAAI,CAAI,EAAE,CAAiB,EAAA;IAClD,IAAI,WAAW,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE;AACpC,QAAA,OAAO,CAAM,CAAC;AACf,KAAA;AAED,IAAA,OAAQ,MAAM,CAAC,IAAI,CAAC,CAAC,CAAiB,CAAC,MAAM,CAAC,UAAC,GAAG,EAAE,GAAG,EAAA;;AACrD,QAAA,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE;AACf,YAAA,OAAO,GAAG,CAAC;AACZ,SAAA;QAED,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE;AACrB,YAAA,OAAO,GAAG,CAAC;AACZ,SAAA;AAED,QAAA,IAAM,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,EAAG,CAAC,CAAC,GAAG,CAA6B,CAAC,CAAC;AAEtE,QAAA,OAAA,QAAA,CAAA,QAAA,CAAA,EAAA,EAAY,GAAG,CAAA,GAAA,EAAA,GAAA,EAAA,EAAA,EAAA,CAAG,GAAG,CAAA,GAAG,MAAM,EAAG,EAAA,EAAA,CAAA;KAClC,EAAE,CAAC,CAAC,CAAC;AACR;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sparted/shared-library",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "repository": "git@github.com:Sparted/shared-library.git",
5
5
  "author": "Thomas <duprethomasfr@gmail.com>",
6
6
  "license": "MIT",
@@ -24,6 +24,7 @@
24
24
  "jest": "^29.0.3",
25
25
  "jest-environment-jsdom": "^29.0.3",
26
26
  "jest-extended": "^3.1.0",
27
+ "jest-fetch-mock": "^3.0.3",
27
28
  "rollup": "^2.79.1",
28
29
  "rollup-plugin-typescript2": "^0.34.0",
29
30
  "ts-jest": "^29.0.2",
@@ -0,0 +1 @@
1
+ export declare const isUuidV4: (arg: any) => arg is string;