@sparted/shared-library 1.0.6 → 1.0.8-alpha.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.
- package/.circleci/config.yml +40 -0
- package/.eslintignore +1 -0
- package/.eslintrc.js +17 -0
- package/.gitignore +37 -0
- package/CHANGELOG.md +34 -0
- package/README.md +26 -0
- package/{cdn → dist/cdn}/cdn.factory.js +1 -1
- package/dist/cdn/cdn.factory.js.map +1 -0
- package/dist/github/commits/create-git-blob.d.ts +26 -0
- package/dist/github/commits/create-git-blob.js +42 -0
- package/dist/github/commits/create-git-blob.js.map +1 -0
- package/dist/github/commits/create-git-commit.d.ts +23 -0
- package/dist/github/commits/create-git-commit.js +39 -0
- package/dist/github/commits/create-git-commit.js.map +1 -0
- package/dist/github/commits/create-git-tree.d.ts +29 -0
- package/dist/github/commits/create-git-tree.js +47 -0
- package/dist/github/commits/create-git-tree.js.map +1 -0
- package/dist/github/commits/get-sha-last-commit-of-branch.d.ts +21 -0
- package/dist/github/commits/get-sha-last-commit-of-branch.js +35 -0
- package/dist/github/commits/get-sha-last-commit-of-branch.js.map +1 -0
- package/dist/github/commits/get-tree-by-commit-sha.d.ts +23 -0
- package/dist/github/commits/get-tree-by-commit-sha.js +37 -0
- package/dist/github/commits/get-tree-by-commit-sha.js.map +1 -0
- package/dist/github/commits/index.d.ts +6 -0
- package/dist/github/commits/index.js +20 -0
- package/dist/github/commits/run-new-commit-process.d.ts +27 -0
- package/dist/github/commits/run-new-commit-process.js +49 -0
- package/dist/github/commits/run-new-commit-process.js.map +1 -0
- package/dist/github/commits/utils.d.ts +1 -0
- package/dist/github/commits/utils.js +29 -0
- package/dist/github/commits/utils.js.map +1 -0
- package/dist/github/github.d.ts +22 -0
- package/dist/github/github.js +50 -0
- package/dist/github/github.js.map +1 -0
- package/dist/github/index.d.ts +1 -0
- package/dist/github/index.js +10 -0
- package/dist/index.d.ts +15 -0
- package/{index.js → dist/index.js} +2 -0
- package/{index.js.map → dist/index.js.map} +1 -1
- package/dist/package.json +65 -0
- package/dist/string/index.js.map +1 -0
- package/dist/time/index.js.map +1 -0
- package/global.d.ts +2 -0
- package/package.json +5 -1
- package/rollup.config.ts +15 -0
- package/src/array/__tests__/array-to-map.test.ts +50 -0
- package/src/array/__tests__/get-values-from-keys.test.ts +14 -0
- package/src/array/array-to-map.ts +32 -0
- package/src/array/get-values-from-keys.ts +3 -0
- package/src/array/index.ts +2 -0
- package/src/cdn/__tests__/cdn.factory.test.ts +120 -0
- package/src/cdn/__tests__/utils.test.ts +456 -0
- package/src/cdn/cdn.factory.ts +63 -0
- package/src/cdn/index.ts +2 -0
- package/src/cdn/types/cdn-factory.type.ts +35 -0
- package/src/cdn/types/index.ts +1 -0
- package/src/cdn/utils/extract-info-from-url.util.ts +21 -0
- package/src/cdn/utils/get-cdn-from-s3-url.util.ts +20 -0
- package/src/cdn/utils/get-cdn-url.util.ts +26 -0
- package/src/formatting/__tests__/format-number.test.ts +11 -0
- package/src/formatting/format-number.ts +14 -0
- package/src/formatting/index.ts +1 -0
- package/src/fp/__tests__/run-while.test.ts +29 -0
- package/src/fp/index.ts +1 -0
- package/src/fp/run-while.ts +54 -0
- package/src/github/commits/create-git-blob.ts +57 -0
- package/src/github/commits/create-git-commit.ts +57 -0
- package/src/github/commits/create-git-tree.ts +68 -0
- package/src/github/commits/get-sha-last-commit-of-branch.ts +50 -0
- package/src/github/commits/get-tree-by-commit-sha.ts +52 -0
- package/src/github/commits/index.ts +6 -0
- package/src/github/commits/run-new-commit-process.ts +88 -0
- package/src/github/commits/utils.ts +31 -0
- package/src/github/github.ts +69 -0
- package/src/github/index.ts +1 -0
- package/src/http/__tests__/encode-search-object.test.ts +34 -0
- package/src/http/__tests__/fetch-with-timeout.test.ts +50 -0
- package/src/http/encode-search-object.ts +39 -0
- package/src/http/fetch-with-timeout.ts +24 -0
- package/src/http/index.ts +2 -0
- package/{index.d.ts → src/index.ts} +15 -14
- package/src/linked-list/__tests__/apply-diff-linked-list.test.ts +103 -0
- package/src/linked-list/__tests__/create-linked-list.test.ts +204 -0
- package/src/linked-list/__tests__/find-linked-list.test.ts +40 -0
- package/src/linked-list/__tests__/get-linked-list.test.ts +28 -0
- package/src/linked-list/__tests__/insert-linked-list.test.ts +186 -0
- package/src/linked-list/__tests__/linked-list-utils.test.ts +252 -0
- package/src/linked-list/__tests__/reduce-linked-list.test.ts +31 -0
- package/src/linked-list/__tests__/remove-linked-list.test.ts +129 -0
- package/src/linked-list/__tests__/update-linked-list.test.ts +53 -0
- package/src/linked-list/__tests__/while-linked-list.test.ts +58 -0
- package/src/linked-list/apply-diff-linked-list.ts +35 -0
- package/src/linked-list/create-linked-list.ts +79 -0
- package/src/linked-list/find-linked-list.ts +26 -0
- package/src/linked-list/get-linked-list.ts +9 -0
- package/src/linked-list/index.ts +2 -0
- package/src/linked-list/insert-linked-list.ts +50 -0
- package/src/linked-list/linked-list-utils.ts +143 -0
- package/src/linked-list/reduce-linked-list.ts +18 -0
- package/src/linked-list/remove-linked-list.ts +36 -0
- package/src/linked-list/type.ts +38 -0
- package/src/linked-list/update-linked-list.ts +29 -0
- package/src/linked-list/while-linked-list.ts +33 -0
- package/src/math/__tests__/clamp-number.test.ts +30 -0
- package/src/math/__tests__/interpolate.test.ts +28 -0
- package/src/math/__tests__/number-range.test.ts +59 -0
- package/src/math/__tests__/random-number.test.ts +11 -0
- package/src/math/clamp-number.ts +8 -0
- package/src/math/index.ts +4 -0
- package/src/math/interpolate.ts +18 -0
- package/src/math/number-range.ts +16 -0
- package/src/math/random-number.ts +10 -0
- package/src/object/__tests__/merge-deep.test.ts +56 -0
- package/src/object/index.ts +1 -0
- package/src/object/merge-deep.ts +27 -0
- package/src/parsing/__tests__/parse-boolean.test.ts +15 -0
- package/src/parsing/__tests__/parse-enum.test.ts +19 -0
- package/src/parsing/__tests__/parse-number.test.ts +20 -0
- package/src/parsing/index.ts +3 -0
- package/src/parsing/parse-boolean.ts +10 -0
- package/src/parsing/parse-enum.ts +16 -0
- package/src/parsing/parse-number.ts +12 -0
- package/src/predicates/__tests__/is-function.test.ts +11 -0
- package/src/predicates/__tests__/is-null.test.ts +11 -0
- package/src/predicates/__tests__/is-object.test.ts +11 -0
- package/src/predicates/__tests__/is-primitive.test.ts +42 -0
- package/src/predicates/__tests__/is-undefined.test.ts +11 -0
- package/src/predicates/__tests__/is-uuid.test.ts +13 -0
- package/src/predicates/index.ts +5 -0
- package/src/predicates/is-function.ts +3 -0
- package/src/predicates/is-null.ts +3 -0
- package/src/predicates/is-object.ts +3 -0
- package/src/predicates/is-primitive.ts +19 -0
- package/src/predicates/is-undefined.ts +3 -0
- package/src/predicates/is-uuid.ts +3 -0
- package/src/promise/__tests__/delay.test.ts +13 -0
- package/src/promise/__tests__/execute-or-resolve-default.test.ts +25 -0
- package/src/promise/__tests__/execute-with-delay.test.ts +24 -0
- package/src/promise/delay.ts +3 -0
- package/src/promise/execute-or-resolve-default.ts +14 -0
- package/src/promise/execute-with-delay.ts +6 -0
- package/src/promise/index.ts +3 -0
- package/src/string/__tests__/replace-in-template.test.ts +52 -0
- package/src/string/index.ts +1 -0
- package/src/string/replace-in-template.ts +45 -0
- package/src/time/__tests__/convert.test.ts +14 -0
- package/src/time/__tests__/format-minutes.test.ts +10 -0
- package/src/time/__tests__/get-time-diff-as-string.test.ts +128 -0
- package/src/time/constants.ts +4 -0
- package/src/time/convert.ts +5 -0
- package/src/time/format-minutes.ts +9 -0
- package/src/time/get-time-diff-as-string.ts +196 -0
- package/src/time/index.ts +4 -0
- package/src/types/function.ts +1 -0
- package/src/types/general.ts +8 -0
- package/src/types/index.ts +4 -0
- package/src/types/object.ts +33 -0
- package/src/types/search.ts +29 -0
- package/tsconfig.eslint.json +8 -0
- package/tsconfig.json +25 -0
- package/cdn/cdn.factory.js.map +0 -1
- /package/{_virtual → dist/_virtual}/_tslib.js +0 -0
- /package/{_virtual → dist/_virtual}/_tslib.js.map +0 -0
- /package/{array → dist/array}/array-to-map.d.ts +0 -0
- /package/{array → dist/array}/array-to-map.js +0 -0
- /package/{array → dist/array}/array-to-map.js.map +0 -0
- /package/{array → dist/array}/get-values-from-keys.d.ts +0 -0
- /package/{array → dist/array}/get-values-from-keys.js +0 -0
- /package/{array → dist/array}/get-values-from-keys.js.map +0 -0
- /package/{array → dist/array}/index.d.ts +0 -0
- /package/{array → dist/array}/index.js +0 -0
- /package/{array → dist/array}/index.js.map +0 -0
- /package/{cdn → dist/cdn}/cdn.factory.d.ts +0 -0
- /package/{cdn → dist/cdn}/index.d.ts +0 -0
- /package/{cdn → dist/cdn}/index.js +0 -0
- /package/{cdn → dist/cdn}/index.js.map +0 -0
- /package/{cdn → dist/cdn}/types/cdn-factory.type.d.ts +0 -0
- /package/{cdn → dist/cdn}/types/index.d.ts +0 -0
- /package/{cdn → dist/cdn}/types/index.js +0 -0
- /package/{cdn → dist/cdn}/types/index.js.map +0 -0
- /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.d.ts +0 -0
- /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.js +0 -0
- /package/{cdn → dist/cdn}/utils/extract-info-from-url.util.js.map +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.d.ts +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.js +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-from-s3-url.util.js.map +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-url.util.d.ts +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-url.util.js +0 -0
- /package/{cdn → dist/cdn}/utils/get-cdn-url.util.js.map +0 -0
- /package/{formatting → dist/formatting}/format-number.d.ts +0 -0
- /package/{formatting → dist/formatting}/format-number.js +0 -0
- /package/{formatting → dist/formatting}/format-number.js.map +0 -0
- /package/{formatting → dist/formatting}/index.d.ts +0 -0
- /package/{formatting → dist/formatting}/index.js +0 -0
- /package/{formatting → dist/formatting}/index.js.map +0 -0
- /package/{fp → dist/fp}/index.d.ts +0 -0
- /package/{fp → dist/fp}/index.js +0 -0
- /package/{fp → dist/fp}/index.js.map +0 -0
- /package/{fp → dist/fp}/run-while.d.ts +0 -0
- /package/{fp → dist/fp}/run-while.js +0 -0
- /package/{fp → dist/fp}/run-while.js.map +0 -0
- /package/{time → dist/github/commits}/index.js.map +0 -0
- /package/{linked-list → dist/github}/index.js.map +0 -0
- /package/{http → dist/http}/encode-search-object.d.ts +0 -0
- /package/{http → dist/http}/encode-search-object.js +0 -0
- /package/{http → dist/http}/encode-search-object.js.map +0 -0
- /package/{http → dist/http}/fetch-with-timeout.d.ts +0 -0
- /package/{http → dist/http}/fetch-with-timeout.js +0 -0
- /package/{http → dist/http}/fetch-with-timeout.js.map +0 -0
- /package/{http → dist/http}/index.d.ts +0 -0
- /package/{http → dist/http}/index.js +0 -0
- /package/{http → dist/http}/index.js.map +0 -0
- /package/{linked-list → dist/linked-list}/apply-diff-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/apply-diff-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/apply-diff-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/create-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/create-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/create-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/find-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/find-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/find-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/get-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/get-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/get-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/index.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/index.js +0 -0
- /package/{object → dist/linked-list}/index.js.map +0 -0
- /package/{linked-list → dist/linked-list}/insert-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/insert-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/insert-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/linked-list-utils.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/linked-list-utils.js +0 -0
- /package/{linked-list → dist/linked-list}/linked-list-utils.js.map +0 -0
- /package/{linked-list → dist/linked-list}/reduce-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/reduce-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/reduce-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/remove-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/remove-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/remove-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/type.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/update-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/update-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/update-linked-list.js.map +0 -0
- /package/{linked-list → dist/linked-list}/while-linked-list.d.ts +0 -0
- /package/{linked-list → dist/linked-list}/while-linked-list.js +0 -0
- /package/{linked-list → dist/linked-list}/while-linked-list.js.map +0 -0
- /package/{math → dist/math}/clamp-number.d.ts +0 -0
- /package/{math → dist/math}/clamp-number.js +0 -0
- /package/{math → dist/math}/clamp-number.js.map +0 -0
- /package/{math → dist/math}/index.d.ts +0 -0
- /package/{math → dist/math}/index.js +0 -0
- /package/{math → dist/math}/index.js.map +0 -0
- /package/{math → dist/math}/interpolate.d.ts +0 -0
- /package/{math → dist/math}/interpolate.js +0 -0
- /package/{math → dist/math}/interpolate.js.map +0 -0
- /package/{math → dist/math}/number-range.d.ts +0 -0
- /package/{math → dist/math}/number-range.js +0 -0
- /package/{math → dist/math}/number-range.js.map +0 -0
- /package/{math → dist/math}/random-number.d.ts +0 -0
- /package/{math → dist/math}/random-number.js +0 -0
- /package/{math → dist/math}/random-number.js.map +0 -0
- /package/{object → dist/object}/index.d.ts +0 -0
- /package/{object → dist/object}/index.js +0 -0
- /package/{string → dist/object}/index.js.map +0 -0
- /package/{object → dist/object}/merge-deep.d.ts +0 -0
- /package/{object → dist/object}/merge-deep.js +0 -0
- /package/{object → dist/object}/merge-deep.js.map +0 -0
- /package/{parsing → dist/parsing}/index.d.ts +0 -0
- /package/{parsing → dist/parsing}/index.js +0 -0
- /package/{parsing → dist/parsing}/index.js.map +0 -0
- /package/{parsing → dist/parsing}/parse-boolean.d.ts +0 -0
- /package/{parsing → dist/parsing}/parse-boolean.js +0 -0
- /package/{parsing → dist/parsing}/parse-boolean.js.map +0 -0
- /package/{parsing → dist/parsing}/parse-enum.d.ts +0 -0
- /package/{parsing → dist/parsing}/parse-enum.js +0 -0
- /package/{parsing → dist/parsing}/parse-enum.js.map +0 -0
- /package/{parsing → dist/parsing}/parse-number.d.ts +0 -0
- /package/{parsing → dist/parsing}/parse-number.js +0 -0
- /package/{parsing → dist/parsing}/parse-number.js.map +0 -0
- /package/{predicates → dist/predicates}/index.d.ts +0 -0
- /package/{predicates → dist/predicates}/index.js +0 -0
- /package/{predicates → dist/predicates}/index.js.map +0 -0
- /package/{predicates → dist/predicates}/is-function.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-function.js +0 -0
- /package/{predicates → dist/predicates}/is-function.js.map +0 -0
- /package/{predicates → dist/predicates}/is-null.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-null.js +0 -0
- /package/{predicates → dist/predicates}/is-null.js.map +0 -0
- /package/{predicates → dist/predicates}/is-object.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-object.js +0 -0
- /package/{predicates → dist/predicates}/is-object.js.map +0 -0
- /package/{predicates → dist/predicates}/is-primitive.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-primitive.js +0 -0
- /package/{predicates → dist/predicates}/is-primitive.js.map +0 -0
- /package/{predicates → dist/predicates}/is-undefined.d.ts +0 -0
- /package/{predicates → dist/predicates}/is-undefined.js +0 -0
- /package/{predicates → dist/predicates}/is-undefined.js.map +0 -0
- /package/{predicates → dist/predicates}/is-uuid.d.ts +0 -0
- /package/{promise → dist/promise}/delay.d.ts +0 -0
- /package/{promise → dist/promise}/delay.js +0 -0
- /package/{promise → dist/promise}/delay.js.map +0 -0
- /package/{promise → dist/promise}/execute-or-resolve-default.d.ts +0 -0
- /package/{promise → dist/promise}/execute-or-resolve-default.js +0 -0
- /package/{promise → dist/promise}/execute-or-resolve-default.js.map +0 -0
- /package/{promise → dist/promise}/execute-with-delay.d.ts +0 -0
- /package/{promise → dist/promise}/execute-with-delay.js +0 -0
- /package/{promise → dist/promise}/execute-with-delay.js.map +0 -0
- /package/{promise → dist/promise}/index.d.ts +0 -0
- /package/{promise → dist/promise}/index.js +0 -0
- /package/{promise → dist/promise}/index.js.map +0 -0
- /package/{string → dist/string}/index.d.ts +0 -0
- /package/{string → dist/string}/index.js +0 -0
- /package/{string → dist/string}/replace-in-template.d.ts +0 -0
- /package/{string → dist/string}/replace-in-template.js +0 -0
- /package/{string → dist/string}/replace-in-template.js.map +0 -0
- /package/{time → dist/time}/constants.d.ts +0 -0
- /package/{time → dist/time}/constants.js +0 -0
- /package/{time → dist/time}/constants.js.map +0 -0
- /package/{time → dist/time}/convert.d.ts +0 -0
- /package/{time → dist/time}/convert.js +0 -0
- /package/{time → dist/time}/convert.js.map +0 -0
- /package/{time → dist/time}/format-minutes.d.ts +0 -0
- /package/{time → dist/time}/format-minutes.js +0 -0
- /package/{time → dist/time}/format-minutes.js.map +0 -0
- /package/{time → dist/time}/get-time-diff-as-string.d.ts +0 -0
- /package/{time → dist/time}/get-time-diff-as-string.js +0 -0
- /package/{time → dist/time}/get-time-diff-as-string.js.map +0 -0
- /package/{time → dist/time}/index.d.ts +0 -0
- /package/{time → dist/time}/index.js +0 -0
- /package/{types → dist/types}/function.d.ts +0 -0
- /package/{types → dist/types}/general.d.ts +0 -0
- /package/{types → dist/types}/index.d.ts +0 -0
- /package/{types → dist/types}/index.js +0 -0
- /package/{types → dist/types}/index.js.map +0 -0
- /package/{types → dist/types}/object.d.ts +0 -0
- /package/{types → dist/types}/search.d.ts +0 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {
|
|
2
|
+
walkListItemRecord,
|
|
3
|
+
findPreviousId,
|
|
4
|
+
assertItemCompatibility,
|
|
5
|
+
assertPresence,
|
|
6
|
+
assertNewNodePosition,
|
|
7
|
+
} from '../linked-list-utils';
|
|
8
|
+
|
|
9
|
+
describe('walkListItemRecord', () => {
|
|
10
|
+
it('should throw an error if it detected an infinite loop', () => {
|
|
11
|
+
const items = {
|
|
12
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
13
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
14
|
+
3: { id: '3', nextId: '1', previousId: '2' },
|
|
15
|
+
4: { id: '4', nextId: null, previousId: '3' },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
expect(
|
|
19
|
+
() => {
|
|
20
|
+
walkListItemRecord(
|
|
21
|
+
() => ({
|
|
22
|
+
result: null,
|
|
23
|
+
}),
|
|
24
|
+
{
|
|
25
|
+
initValue: null,
|
|
26
|
+
startId: '1',
|
|
27
|
+
items,
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
},
|
|
31
|
+
).toThrowError('Infinite loop found');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should throw an error if a node points to another node that does not exist', () => {
|
|
35
|
+
const items = {
|
|
36
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
37
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
38
|
+
3: { id: '3', nextId: '4', previousId: '2' },
|
|
39
|
+
5: { id: '5', nextId: null, previousId: '3' },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
expect(
|
|
43
|
+
() => {
|
|
44
|
+
walkListItemRecord(
|
|
45
|
+
() => ({
|
|
46
|
+
result: null,
|
|
47
|
+
}),
|
|
48
|
+
{
|
|
49
|
+
initValue: null,
|
|
50
|
+
startId: '1',
|
|
51
|
+
items,
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
},
|
|
55
|
+
).toThrowError('Cul de sac detected');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should not throw an error if it did not walk through every nodes and it was force stopped', () => {
|
|
59
|
+
const items = {
|
|
60
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
61
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
62
|
+
3: { id: '3', nextId: null, previousId: '2' },
|
|
63
|
+
4: { id: '4', nextId: null, previousId: '3' },
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
expect(
|
|
67
|
+
() => {
|
|
68
|
+
walkListItemRecord(
|
|
69
|
+
(_, current) => {
|
|
70
|
+
if (current.id === '2') {
|
|
71
|
+
return {
|
|
72
|
+
result: null,
|
|
73
|
+
control: {
|
|
74
|
+
stop: true,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
result: null,
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
initValue: null,
|
|
85
|
+
startId: '1',
|
|
86
|
+
items,
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
},
|
|
90
|
+
).not.toThrowError();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should accumulate the result and return it', () => {
|
|
94
|
+
const items = {
|
|
95
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
96
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
97
|
+
3: { id: '3', nextId: '4', previousId: '2' },
|
|
98
|
+
4: { id: '4', nextId: null, previousId: '3' },
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const result = walkListItemRecord(
|
|
102
|
+
(acc, current) => ({
|
|
103
|
+
result: acc + current.id.toString(),
|
|
104
|
+
}),
|
|
105
|
+
{
|
|
106
|
+
initValue: '',
|
|
107
|
+
startId: '1',
|
|
108
|
+
items,
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(result).toStrictEqual('1234');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('findPreviousId', () => {
|
|
117
|
+
const list = {
|
|
118
|
+
headId: '1',
|
|
119
|
+
items: {
|
|
120
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
121
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
122
|
+
3: { id: '3', nextId: '4', previousId: '2' },
|
|
123
|
+
4: { id: '4', nextId: null, previousId: '3' },
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
it('should return the headId if the id is the head', () => {
|
|
128
|
+
expect(findPreviousId('1', list)).toStrictEqual(null);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should correctly return the previous id', () => {
|
|
132
|
+
expect(findPreviousId('4', list)).toStrictEqual('3');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should correctly find the previous id if it is the tail', () => {
|
|
136
|
+
expect(findPreviousId(null, list)).toStrictEqual('4');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should return null if the list is empty', () => {
|
|
140
|
+
expect(findPreviousId('4', { headId: null, items: {} })).toStrictEqual(null);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should return null if it can\'t find the previous', () => {
|
|
144
|
+
expect(findPreviousId('6', list)).toStrictEqual(null);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('assertItemCompatibility', () => {
|
|
149
|
+
const itemWithPrevious = { id: '2', nextId: '3', previousId: '1' };
|
|
150
|
+
const itemWithoutPrevious = { id: '2', nextId: '3' };
|
|
151
|
+
|
|
152
|
+
it('should throw an error if the node has previous and the linked list is not double linked', () => {
|
|
153
|
+
expect(() => assertItemCompatibility(itemWithPrevious, 'single')).toThrowError(
|
|
154
|
+
'Node is incompatible with the list (id: 2)',
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should throw an error if the node does not have previous and the linked list is double linked', () => {
|
|
159
|
+
expect(() => assertItemCompatibility(itemWithoutPrevious, 'double')).toThrowError(
|
|
160
|
+
'Node is incompatible with the list (id: 2)',
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should not throw error if the node is correct', () => {
|
|
165
|
+
expect(() => assertItemCompatibility(itemWithoutPrevious, 'single')).not.toThrowError();
|
|
166
|
+
expect(() => assertItemCompatibility(itemWithPrevious, 'double')).not.toThrowError();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should not throw error if format is null', () => {
|
|
170
|
+
expect(() => assertItemCompatibility(itemWithoutPrevious, null)).not.toThrowError();
|
|
171
|
+
expect(() => assertItemCompatibility(itemWithPrevious, null)).not.toThrowError();
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('assertPresence', () => {
|
|
176
|
+
const items = {
|
|
177
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
178
|
+
2: { id: '2', nextId: null, previousId: '1' },
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
it('should throw an error if the item is not in the list', () => {
|
|
182
|
+
expect(() => assertPresence('3', items)).toThrowError(
|
|
183
|
+
'Node not found in the list (id: 3)',
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should not throw an error if the item is in the list', () => {
|
|
188
|
+
expect(() => assertPresence('2', items)).not.toThrowError();
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('assertNewNodePosition', () => {
|
|
193
|
+
const list = {
|
|
194
|
+
headId: '1',
|
|
195
|
+
items: {
|
|
196
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
197
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
198
|
+
3: { id: '3', nextId: null, previousId: '2' },
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
it('should throw an error if the head position is incorrect', () => {
|
|
203
|
+
const item = { id: '4', nextId: '2' };
|
|
204
|
+
|
|
205
|
+
expect(
|
|
206
|
+
() => {
|
|
207
|
+
assertNewNodePosition(item, {
|
|
208
|
+
previousId: null,
|
|
209
|
+
list,
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
).toThrowError('Invalid position for given node (id: 4)');
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('should throw an error if the tail position is incorrect', () => {
|
|
216
|
+
const item = { id: '4', nextId: null };
|
|
217
|
+
|
|
218
|
+
expect(
|
|
219
|
+
() => {
|
|
220
|
+
assertNewNodePosition(item, {
|
|
221
|
+
previousId: '2',
|
|
222
|
+
list,
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
).toThrowError('Invalid position for given node (id: 4)');
|
|
226
|
+
});
|
|
227
|
+
it('should throw an error if the middle position is incorrect', () => {
|
|
228
|
+
const item = { id: '4', nextId: '3' };
|
|
229
|
+
|
|
230
|
+
expect(
|
|
231
|
+
() => {
|
|
232
|
+
assertNewNodePosition(item, {
|
|
233
|
+
previousId: '1',
|
|
234
|
+
list,
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
).toThrowError('Invalid position for given node (id: 4)');
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should not throw an error if the position is correct', () => {
|
|
241
|
+
const item = { id: '4', nextId: '3' };
|
|
242
|
+
|
|
243
|
+
expect(
|
|
244
|
+
() => {
|
|
245
|
+
assertNewNodePosition(item, {
|
|
246
|
+
previousId: '2',
|
|
247
|
+
list,
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
).not.toThrowError();
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { reduceLinkedList } from '../reduce-linked-list';
|
|
2
|
+
|
|
3
|
+
describe('reduceLinkedList', () => {
|
|
4
|
+
it('should execute the reduce function on every node', () => {
|
|
5
|
+
const nodes = {
|
|
6
|
+
3: {
|
|
7
|
+
id: '3',
|
|
8
|
+
nextId: '2',
|
|
9
|
+
},
|
|
10
|
+
1: {
|
|
11
|
+
id: '1',
|
|
12
|
+
nextId: null,
|
|
13
|
+
},
|
|
14
|
+
2: {
|
|
15
|
+
id: '2',
|
|
16
|
+
nextId: '1',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const reduceResult = reduceLinkedList(
|
|
21
|
+
(acc: string, current) => acc.concat(current.id),
|
|
22
|
+
'',
|
|
23
|
+
{
|
|
24
|
+
headId: '3',
|
|
25
|
+
items: nodes,
|
|
26
|
+
},
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
expect(reduceResult).toStrictEqual('321');
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { removeLinkedList } from '../remove-linked-list';
|
|
2
|
+
import { assertPresence, findPreviousId } from '../linked-list-utils';
|
|
3
|
+
|
|
4
|
+
jest.mock('../linked-list-utils');
|
|
5
|
+
|
|
6
|
+
describe('removeLinkedList', () => {
|
|
7
|
+
const assertPresenceMocked = jest.mocked(assertPresence);
|
|
8
|
+
const findPreviousIdMocked = jest.mocked(findPreviousId);
|
|
9
|
+
|
|
10
|
+
const list = {
|
|
11
|
+
headId: '1',
|
|
12
|
+
items: {
|
|
13
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
14
|
+
2: { id: '2', nextId: '3', previousId: '1' },
|
|
15
|
+
3: { id: '3', nextId: null, previousId: '2' },
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
jest.clearAllMocks();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should make the right assertions', () => {
|
|
24
|
+
removeLinkedList('1', {
|
|
25
|
+
list,
|
|
26
|
+
factory: jest.fn(),
|
|
27
|
+
format: 'double',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
expect(assertPresenceMocked).toHaveBeenCalledTimes(1);
|
|
31
|
+
expect(assertPresenceMocked).toHaveBeenCalledWith('1', list.items);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should find the previous id if the list is not double linked', () => {
|
|
35
|
+
removeLinkedList('1', {
|
|
36
|
+
list,
|
|
37
|
+
factory: jest.fn(),
|
|
38
|
+
format: 'single',
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(findPreviousIdMocked).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(findPreviousIdMocked).toHaveBeenCalledWith('1', list);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should directly take the previous id from the deleted item if the list is double linked', () => {
|
|
46
|
+
removeLinkedList('1', {
|
|
47
|
+
list,
|
|
48
|
+
factory: jest.fn(),
|
|
49
|
+
format: 'double',
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(findPreviousIdMocked).not.toHaveBeenCalled();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should handle a head deletion', () => {
|
|
56
|
+
const newList = removeLinkedList('1', {
|
|
57
|
+
list,
|
|
58
|
+
factory: jest.fn().mockImplementation((theList) => theList),
|
|
59
|
+
format: 'double',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(newList).toStrictEqual({
|
|
63
|
+
headId: '2',
|
|
64
|
+
items: {
|
|
65
|
+
2: { id: '2', nextId: '3', previousId: null },
|
|
66
|
+
3: { id: '3', nextId: null, previousId: '2' },
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should handle a tail deletion', () => {
|
|
72
|
+
const newList = removeLinkedList('3', {
|
|
73
|
+
list,
|
|
74
|
+
factory: jest.fn().mockImplementation((theList) => theList),
|
|
75
|
+
format: 'double',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(newList).toStrictEqual({
|
|
79
|
+
headId: '1',
|
|
80
|
+
items: {
|
|
81
|
+
1: { id: '1', nextId: '2', previousId: null },
|
|
82
|
+
2: { id: '2', nextId: null, previousId: '1' },
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should handle a middle deletion', () => {
|
|
88
|
+
const newList = removeLinkedList('2', {
|
|
89
|
+
list,
|
|
90
|
+
factory: jest.fn().mockImplementation((theList) => theList),
|
|
91
|
+
format: 'double',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(newList).toStrictEqual({
|
|
95
|
+
headId: '1',
|
|
96
|
+
items: {
|
|
97
|
+
1: { id: '1', nextId: '3', previousId: null },
|
|
98
|
+
3: { id: '3', nextId: null, previousId: '1' },
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should a deletion in a single linked list', () => {
|
|
104
|
+
findPreviousIdMocked.mockReturnValue('1');
|
|
105
|
+
|
|
106
|
+
const singleList = {
|
|
107
|
+
headId: '1',
|
|
108
|
+
items: {
|
|
109
|
+
1: { id: '1', nextId: '2' },
|
|
110
|
+
2: { id: '2', nextId: '3' },
|
|
111
|
+
3: { id: '3', nextId: null },
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const newList = removeLinkedList('2', {
|
|
116
|
+
list: singleList,
|
|
117
|
+
factory: jest.fn().mockImplementation((theList) => theList),
|
|
118
|
+
format: 'single',
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
expect(newList).toStrictEqual({
|
|
122
|
+
headId: '1',
|
|
123
|
+
items: {
|
|
124
|
+
1: { id: '1', nextId: '3' },
|
|
125
|
+
3: { id: '3', nextId: null },
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { updateLinkedList } from '../update-linked-list';
|
|
2
|
+
import { assertPresence } from '../linked-list-utils';
|
|
3
|
+
|
|
4
|
+
jest.mock('../linked-list-utils');
|
|
5
|
+
|
|
6
|
+
describe('updateLinkedList', () => {
|
|
7
|
+
const assertPresenceMocked = jest.mocked(assertPresence);
|
|
8
|
+
|
|
9
|
+
const list = {
|
|
10
|
+
headId: '1',
|
|
11
|
+
items: {
|
|
12
|
+
1: { id: '1', nextId: '2', previousId: null, name: 'Barack Afritt' },
|
|
13
|
+
2: { id: '2', nextId: '3', previousId: '1', name: 'Djamal Dormi' },
|
|
14
|
+
3: { id: '3', nextId: null, previousId: '2', name: 'Harry Cover' },
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
jest.clearAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should make the right assertions', () => {
|
|
23
|
+
const itemToUpdate = { id: '1', nextId: '2', previousId: null };
|
|
24
|
+
|
|
25
|
+
updateLinkedList(itemToUpdate, {
|
|
26
|
+
list: list as any,
|
|
27
|
+
format: 'double',
|
|
28
|
+
factory: jest.fn(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(assertPresenceMocked).toHaveBeenCalledTimes(1);
|
|
32
|
+
expect(assertPresenceMocked).toHaveBeenCalledWith('1', list.items);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should update the item without reordering it', () => {
|
|
36
|
+
const itemToUpdate = { id: '1', nextId: '3', previousId: '2', surname: 'Ototo' };
|
|
37
|
+
|
|
38
|
+
const newList = updateLinkedList(itemToUpdate, {
|
|
39
|
+
list: list as any,
|
|
40
|
+
format: 'double',
|
|
41
|
+
factory: jest.fn().mockImplementation((theList) => theList),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(newList).toStrictEqual({
|
|
45
|
+
headId: '1',
|
|
46
|
+
items: {
|
|
47
|
+
1: { id: '1', nextId: '2', previousId: null, surname: 'Ototo' },
|
|
48
|
+
2: { id: '2', nextId: '3', previousId: '1', name: 'Djamal Dormi' },
|
|
49
|
+
3: { id: '3', nextId: null, previousId: '2', name: 'Harry Cover' },
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { whileLinkedList } from '../while-linked-list';
|
|
2
|
+
|
|
3
|
+
describe('whileLinkedList', () => {
|
|
4
|
+
it('should return every ids while the predicate is true', () => {
|
|
5
|
+
const list = {
|
|
6
|
+
headId: '3',
|
|
7
|
+
items: {
|
|
8
|
+
3: {
|
|
9
|
+
id: '3',
|
|
10
|
+
nextId: '2',
|
|
11
|
+
},
|
|
12
|
+
1: {
|
|
13
|
+
id: '1',
|
|
14
|
+
nextId: null,
|
|
15
|
+
},
|
|
16
|
+
2: {
|
|
17
|
+
id: '2',
|
|
18
|
+
nextId: '1',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const ids = whileLinkedList(
|
|
24
|
+
(item) => item.id !== '1',
|
|
25
|
+
list,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(ids).toStrictEqual(['3', '2']);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should return every ids while the predicate is true with a start id', () => {
|
|
32
|
+
const list = {
|
|
33
|
+
headId: '3',
|
|
34
|
+
items: {
|
|
35
|
+
3: {
|
|
36
|
+
id: '3',
|
|
37
|
+
nextId: '2',
|
|
38
|
+
},
|
|
39
|
+
1: {
|
|
40
|
+
id: '1',
|
|
41
|
+
nextId: null,
|
|
42
|
+
},
|
|
43
|
+
2: {
|
|
44
|
+
id: '2',
|
|
45
|
+
nextId: '1',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const ids = whileLinkedList(
|
|
51
|
+
(item) => item.id !== '1',
|
|
52
|
+
list,
|
|
53
|
+
'2',
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
expect(ids).toStrictEqual(['2']);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { LinkedList, LinkedListItem } from './type';
|
|
2
|
+
|
|
3
|
+
export const applyDiffLinkedList = <T extends LinkedListItem>(
|
|
4
|
+
before: T | null,
|
|
5
|
+
after: T | null,
|
|
6
|
+
list: LinkedList<T>,
|
|
7
|
+
) => {
|
|
8
|
+
// Delete
|
|
9
|
+
if (!after && before) {
|
|
10
|
+
return list.remove(before.id);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Insert
|
|
14
|
+
if (!before && after) {
|
|
15
|
+
return list.insert(after);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// For the following commands, none of the 2 items can be null
|
|
19
|
+
// We dont use && because checks are already done above, but ts cannot infer that they cannot be null afterwards
|
|
20
|
+
if (!before || !after) {
|
|
21
|
+
throw new Error('Cannot have both before & after as null');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (before.id !== after.id) {
|
|
25
|
+
throw new Error('Cannot change id of a ListNode');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const listWithPotentialReorder = before.nextId === after.nextId
|
|
29
|
+
? list
|
|
30
|
+
: list
|
|
31
|
+
.remove(before.id)
|
|
32
|
+
.insert(after);
|
|
33
|
+
|
|
34
|
+
return listWithPotentialReorder.update(after);
|
|
35
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { LinkedListItem, LinkedList, ManipulationConfig, LinkedListFormat } from './type';
|
|
2
|
+
|
|
3
|
+
import { reduceLinkedList } from './reduce-linked-list';
|
|
4
|
+
import { findLinkedList } from './find-linked-list';
|
|
5
|
+
import { whileLinkedList } from './while-linked-list';
|
|
6
|
+
import { walkListItemRecord } from './linked-list-utils';
|
|
7
|
+
import { removeLinkedList } from './remove-linked-list';
|
|
8
|
+
import { insertLinkedList } from './insert-linked-list';
|
|
9
|
+
import { getLinkedList } from './get-linked-list';
|
|
10
|
+
import { updateLinkedList } from './update-linked-list';
|
|
11
|
+
import { applyDiffLinkedList } from './apply-diff-linked-list';
|
|
12
|
+
|
|
13
|
+
type LinkedListValidation = {
|
|
14
|
+
format: LinkedListFormat | null,
|
|
15
|
+
previousId: null | string,
|
|
16
|
+
count: number,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const createLinkedList = <T extends LinkedListItem>(headId: string | null, items: Record<string, T>): LinkedList<T> => {
|
|
20
|
+
const head = headId ? items[headId] : null;
|
|
21
|
+
|
|
22
|
+
if (head === undefined) {
|
|
23
|
+
throw new Error('Cannot find the head.');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const list = { headId, items };
|
|
27
|
+
|
|
28
|
+
const { format, count } = walkListItemRecord((acc, current) => {
|
|
29
|
+
const newCount = acc.count + 1;
|
|
30
|
+
const currentNodeFormat: LinkedListFormat = current.previousId !== undefined ? 'double' : 'single';
|
|
31
|
+
const incompatibleFormats = acc.format !== null && acc.format !== currentNodeFormat;
|
|
32
|
+
|
|
33
|
+
if (incompatibleFormats || (currentNodeFormat === 'double' && current.previousId !== acc.previousId)) {
|
|
34
|
+
throw new Error('Invalid previous node in the linked list');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
result: {
|
|
39
|
+
previousId: current.id,
|
|
40
|
+
format: currentNodeFormat,
|
|
41
|
+
count: newCount,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}, {
|
|
45
|
+
initValue: {
|
|
46
|
+
format: null,
|
|
47
|
+
count: 0,
|
|
48
|
+
previousId: null,
|
|
49
|
+
} as LinkedListValidation,
|
|
50
|
+
startId: headId,
|
|
51
|
+
items,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const manipulationConfig: ManipulationConfig<T> = {
|
|
55
|
+
factory: (newList) => createLinkedList(newList.headId, newList.items),
|
|
56
|
+
/**
|
|
57
|
+
* At this point isDoubleLinked cannot be undefined
|
|
58
|
+
*/
|
|
59
|
+
format,
|
|
60
|
+
list,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const linkedList: LinkedList<T> = {
|
|
64
|
+
head,
|
|
65
|
+
items,
|
|
66
|
+
format,
|
|
67
|
+
size: count,
|
|
68
|
+
reduce: (cb, initValue) => reduceLinkedList(cb, initValue, list),
|
|
69
|
+
find: (predicate) => findLinkedList(predicate, list),
|
|
70
|
+
while: (predicate, startId) => whileLinkedList(predicate, list, startId),
|
|
71
|
+
remove: (id) => removeLinkedList(id, manipulationConfig),
|
|
72
|
+
insert: (item) => insertLinkedList(item, manipulationConfig),
|
|
73
|
+
get: (id) => getLinkedList(id, list),
|
|
74
|
+
update: (item) => updateLinkedList(item, manipulationConfig),
|
|
75
|
+
applyDiff: (before, after) => applyDiffLinkedList(before, after, linkedList),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return linkedList;
|
|
79
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { LinkedListItem, ItemPredicate, SimpleLinkedList } from './type';
|
|
2
|
+
import { walkListItemRecord } from './linked-list-utils';
|
|
3
|
+
|
|
4
|
+
export const findLinkedList = <Item extends LinkedListItem>(
|
|
5
|
+
predicate: ItemPredicate<Item>,
|
|
6
|
+
list: SimpleLinkedList<Item>,
|
|
7
|
+
): Item | null => (
|
|
8
|
+
walkListItemRecord((acc, item) => {
|
|
9
|
+
if (predicate(item)) {
|
|
10
|
+
return {
|
|
11
|
+
result: item,
|
|
12
|
+
control: {
|
|
13
|
+
stop: true,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
result: acc,
|
|
20
|
+
};
|
|
21
|
+
}, {
|
|
22
|
+
initValue: null as Item | null,
|
|
23
|
+
items: list.items,
|
|
24
|
+
startId: list.headId,
|
|
25
|
+
})
|
|
26
|
+
);
|