@sparted/shared-library 0.0.16 → 0.0.17
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.
|
@@ -5,11 +5,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var runWhile = require('../fp/run-while.js');
|
|
6
6
|
|
|
7
7
|
const walkListItemRecord = (callback, config) => {
|
|
8
|
-
var _a;
|
|
9
8
|
const { items, startId, initValue } = config;
|
|
10
9
|
const visited = new Set();
|
|
11
10
|
const totalNodes = Object.keys(items).length;
|
|
12
|
-
const {
|
|
11
|
+
const { result } = runWhile.runWhile({
|
|
13
12
|
iterationLimit: totalNodes + 1,
|
|
14
13
|
initialValue: {
|
|
15
14
|
currentId: startId,
|
|
@@ -43,13 +42,6 @@ const walkListItemRecord = (callback, config) => {
|
|
|
43
42
|
};
|
|
44
43
|
},
|
|
45
44
|
});
|
|
46
|
-
/**
|
|
47
|
-
* When force stopping the walk, we will always have unvisited
|
|
48
|
-
* nodes, so we skip this check.
|
|
49
|
-
*/
|
|
50
|
-
if (iterationCount !== totalNodes && ((_a = result.control) === null || _a === void 0 ? void 0 : _a.stop) !== true) {
|
|
51
|
-
throw new Error('Isolated nodes found');
|
|
52
|
-
}
|
|
53
45
|
return result.accumulator;
|
|
54
46
|
};
|
|
55
47
|
const findPreviousId = (id, list) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"linked-list-utils.js","sources":["../../src/linked-list/linked-list-utils.ts"],"sourcesContent":["import { runWhile } from '../fp';\nimport { LinkedListFormat, LinkedListItem, SimpleLinkedList } 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 {
|
|
1
|
+
{"version":3,"file":"linked-list-utils.js","sources":["../../src/linked-list/linked-list-utils.ts"],"sourcesContent":["import { runWhile } from '../fp';\nimport { LinkedListFormat, LinkedListItem, SimpleLinkedList } 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 { 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 return result.accumulator;\n};\n\nexport const findPreviousId = <T extends LinkedListItem>(id: string | null, list: SimpleLinkedList<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, format: LinkedListFormat | null) => {\n /**\n * An empty linked list won't have any format yet, it will be\n * determined by the first item inserted in the list.\n */\n if (format === null) {\n return;\n }\n\n const incompatibleWithDoubleLinks = item.previousId === undefined && format === 'double';\n const incompatibleWithSingleLink = item.previousId !== undefined && format === 'single';\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: SimpleLinkedList<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":["runWhile"],"mappings":";;;;;;MAwBa,kBAAkB,GAAG,CAChC,QAAiC,EACjC,MAA6B,KAC3B;IACF,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;AAC7C,IAAA,MAAM,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC1B,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC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,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE;AACpB,QAAA,MAAM,IAAI,KAAK,CAAC,mCAAmC,EAAE,CAAA,CAAA,CAAG,CAAC,CAAC;AAC3D,KAAA;AACH,EAAC;MAOY,qBAAqB,GAAG,CAA2B,IAAO,EAAE,MAA+B,KAAI;AAC1G,IAAA,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;AAEpC,IAAA,MAAM,oBAAoB,GAAG,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,CAAC;IACzF,MAAM,kBAAkB,GAAG,UAAU,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC;AACxG,IAAA,MAAM,kBAAkB,GAAG,UAAU,KAAK,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,CAAC;AAE9E,IAAA,IAAI,oBAAoB,IAAI,kBAAkB,IAAI,kBAAkB,EAAE;QACpE,MAAM,IAAI,KAAK,CAAC,CAAA,qCAAA,EAAwC,IAAI,CAAC,EAAE,CAAG,CAAA,CAAA,CAAC,CAAC;AACrE,KAAA;AACH;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,15 +0,0 @@
|
|
|
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 {};
|