diff --git a/README.md b/README.md index c68b5e7..996d62b 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,13 @@ yarn add data-structure-typed ```bash npm install data-structure-typed ``` +## Online examples + +[Online Examples](https://data-structure-typed-examples.vercel.app) + +## Examples Repository + +[Example Repository](https://github.com/zrwusa/data-structure-typed-examples) ## api docs diff --git a/package.json b/package.json index 977561b..d7841a6 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,8 @@ "Min Priority Queue", "Trie" ], - "author": "Tyler Zeng", - "license": "ISC", + "author": "Tyler Zeng zrwusa@gmail.com", + "license": "MIT", "bugs": { "url": "https://github.com/zrwusa/data-structure-typed/issues" }, diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 2bb0789..44a5adb 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {BST, BSTNode} from './bst'; import type {AVLTreeDeleted, BinaryTreeNodeId} from '../types'; diff --git a/src/data-structures/binary-tree/binary-indexed-tree.ts b/src/data-structures/binary-tree/binary-indexed-tree.ts index e00a651..15e7e20 100644 --- a/src/data-structures/binary-tree/binary-indexed-tree.ts +++ b/src/data-structures/binary-tree/binary-indexed-tree.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ export class BinaryIndexedTree { private readonly _sumTree: number[]; diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 5ed6018..29e47bd 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -1,9 +1,12 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ -import {trampoline} from '../../utils/trampoline'; +import {trampoline} from '../../utils'; import type { BinaryTreeDeleted, BinaryTreeNodeId, diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index a7e73bd..4ef2107 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types'; import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree'; diff --git a/src/data-structures/binary-tree/segment-tree.ts b/src/data-structures/binary-tree/segment-tree.ts index 7604f9f..e028c3a 100644 --- a/src/data-structures/binary-tree/segment-tree.ts +++ b/src/data-structures/binary-tree/segment-tree.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import type {SegmentTreeNodeVal} from '../types'; diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 6b348ba..8dc7926 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {BST, BSTNode} from './bst'; import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types'; diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 5572d1d..92a4f41 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {arrayRemove, uuidV4} from '../../utils'; import {PriorityQueue} from '../priority-queue'; diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index bfbb185..98b96a0 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {arrayRemove} from '../../utils'; import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph'; @@ -184,7 +187,7 @@ export class DirectedGraph ext const destInEdges = this._inEdgeMap.get(dest); if (destInEdges && destInEdges.length > 0) { - removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.dest === dest.id)[0]; + removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.id)[0]; } } diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 6cc47c6..6fd0e82 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {arrayRemove} from '../../utils'; import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph'; diff --git a/src/data-structures/hash/coordinate-map.ts b/src/data-structures/hash/coordinate-map.ts index 1347aa1..b99f9cd 100644 --- a/src/data-structures/hash/coordinate-map.ts +++ b/src/data-structures/hash/coordinate-map.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ export class CoordinateMap extends Map { private readonly _joint: string = '_'; diff --git a/src/data-structures/hash/coordinate-set.ts b/src/data-structures/hash/coordinate-set.ts index 17a5bde..8c1e3e6 100644 --- a/src/data-structures/hash/coordinate-set.ts +++ b/src/data-structures/hash/coordinate-set.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ export class CoordinateSet extends Set { private readonly _joint: string = '_'; diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 2ccfc45..4ba2af9 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {PriorityQueue} from '../priority-queue'; import type {HeapItem, HeapOptions} from '../types'; diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index c270fda..ee969ad 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {Heap} from './heap'; diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index 6edf9c9..63d5145 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {Heap} from './heap'; diff --git a/src/data-structures/index.ts b/src/data-structures/index.ts index 5a154bd..6957d45 100644 --- a/src/data-structures/index.ts +++ b/src/data-structures/index.ts @@ -8,4 +8,6 @@ export * from './heap'; export * from './priority-queue'; export * from './matrix'; export * from './trie'; +export * from './types'; + diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 3dc6ced..6ba4d47 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import type {DoublyLinkedListGetBy} from '../types'; diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index 8971d59..37bcd3f 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ /** diff --git a/src/data-structures/matrix/matrix.ts b/src/data-structures/matrix/matrix.ts index 5f65e1e..e61959e 100644 --- a/src/data-structures/matrix/matrix.ts +++ b/src/data-structures/matrix/matrix.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ // todo need to be improved export class MatrixNTI2D { diff --git a/src/data-structures/matrix/matrix2d.ts b/src/data-structures/matrix/matrix2d.ts index 9204110..1a5a7a4 100644 --- a/src/data-structures/matrix/matrix2d.ts +++ b/src/data-structures/matrix/matrix2d.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import Vector2D from './vector2d' diff --git a/src/data-structures/matrix/navigator.ts b/src/data-structures/matrix/navigator.ts index fb00720..fd6a42e 100644 --- a/src/data-structures/matrix/navigator.ts +++ b/src/data-structures/matrix/navigator.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import type {Direction, NavigatorParams, Turning} from '../types'; diff --git a/src/data-structures/matrix/vector2d.ts b/src/data-structures/matrix/vector2d.ts index b23213b..6163194 100644 --- a/src/data-structures/matrix/vector2d.ts +++ b/src/data-structures/matrix/vector2d.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ export class Vector2D { constructor( diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index 4ff881c..fb764d1 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {PriorityQueue} from './priority-queue'; import type {PriorityQueueOptions} from '../types'; diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index 9e3c9d7..6e84308 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {PriorityQueue} from './priority-queue'; import type {PriorityQueueOptions} from '../types'; diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index c25da51..172fa41 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types'; diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 02b3a1d..ce8d508 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ import {DoublyLinkedList} from '../linked-list'; diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index 638ec91..d476ead 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -1,6 +1,9 @@ /** - * @copyright Tyler Zeng - * @license MIT + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License */ export class TrieNode { protected _value; diff --git a/src/utils/index.ts b/src/utils/index.ts index 038522d..be7aaf8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1,2 @@ -export * from './utils'; \ No newline at end of file +export * from './utils'; +export * from './types'; \ No newline at end of file diff --git a/src/utils/trampoline.ts b/src/utils/trampoline.ts deleted file mode 100644 index c3649c4..0000000 --- a/src/utils/trampoline.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @copyright Tyler Zeng - * @license MIT - */ -import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types'; - -export const THUNK_SYMBOL = Symbol('thunk') - -export const isThunk = (fnOrValue: any) => { - return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL -} - -export const toThunk = (fn: ToThunkFn): Thunk => { - const thunk = () => fn() - thunk.__THUNK__ = THUNK_SYMBOL - return thunk -} - -export const trampoline = (fn: TrlFn) => { - const cont = (...args: [...Parameters]) => toThunk(() => fn(...args)) - - return Object.assign( - (...args: [...Parameters]) => { - let result = fn(...args) - - while (isThunk(result) && typeof result === 'function') { - result = result() - } - - return result - }, - {cont} - ) -} - -export const trampolineAsync = (fn: TrlAsyncFn) => { - const cont = (...args: [...Parameters]) => toThunk(() => fn(...args)) - - return Object.assign( - async (...args: [...Parameters]) => { - let result = await fn(...args) - - while (isThunk(result) && typeof result === 'function') { - result = await result() - } - - return result - }, - {cont} - ) -} \ No newline at end of file diff --git a/src/utils/types/utils.ts b/src/utils/types/utils.ts index 30568e5..a683bf1 100644 --- a/src/utils/types/utils.ts +++ b/src/utils/types/utils.ts @@ -1,3 +1,12 @@ +export type JSONSerializable = { + [key: string]: any +} +export type JSONValue = string | number | boolean | undefined | JSONObject; + +export interface JSONObject { + [key: string]: JSONValue; +} + export type AnyFunction = (...args: A) => R; export type Primitive = | number @@ -32,16 +41,6 @@ export type DeepLeavesWrap = type Json = null | string | number | boolean | Json [] | { [name: string]: Json } -export type JSONSerializable = { - [key: string]: any -} - -export type JSONValue = string | number | boolean | undefined | JSONObject; - -export interface JSONObject { - [key: string]: JSONValue; -} - export type TypeName = T extends string ? 'string' : T extends number @@ -173,8 +172,7 @@ export type CurryFunc = T extends (...args: infer Args) => infer R export type ToThunkFn = () => ReturnType; -const THUNK_SYMBOL = Symbol('thunk') -export type Thunk = () => ReturnType & { __THUNK__: typeof THUNK_SYMBOL }; +export type Thunk = () => ReturnType & { __THUNK__: Symbol }; export type TrlFn = (...args: any[]) => any; export type TrlAsyncFn = (...args: any[]) => any; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 46bd598..a153a8a 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,14 +1,5 @@ -import * as _ from 'lodash'; -import {AnyFunction} from './types'; - -export type JSONSerializable = { - [key: string]: any -} -export type JSONValue = string | number | boolean | undefined | JSONObject; - -export interface JSONObject { - [key: string]: JSONValue; -} +import _ from 'lodash'; +import type {AnyFunction, JSONObject, JSONSerializable} from './types'; export function randomText(length: number) { let result = ''; @@ -512,4 +503,59 @@ export function zip(array1: T[], array2: T1[], options? } } return isToObj ? zippedObjCoords : zipped; +} + +/** + * data-structure-typed + * + * @author Tyler Zeng + * @copyright Copyright (c) 2022 Tyler Zeng + * @license MIT License + */ +import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types'; + +export const THUNK_SYMBOL = Symbol('thunk') + +export const isThunk = (fnOrValue: any) => { + return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL +} + +export const toThunk = (fn: ToThunkFn): Thunk => { + const thunk = () => fn() + thunk.__THUNK__ = THUNK_SYMBOL + return thunk +} + +export const trampoline = (fn: TrlFn) => { + const cont = (...args: [...Parameters]) => toThunk(() => fn(...args)) + + return Object.assign( + (...args: [...Parameters]) => { + let result = fn(...args) + + while (isThunk(result) && typeof result === 'function') { + result = result() + } + + return result + }, + {cont} + ) +} + +export const trampolineAsync = (fn: TrlAsyncFn) => { + const cont = (...args: [...Parameters]) => toThunk(() => fn(...args)) + + return Object.assign( + async (...args: [...Parameters]) => { + let result = await fn(...args) + + while (isThunk(result) && typeof result === 'function') { + result = await result() + } + + return result + }, + {cont} + ) } \ No newline at end of file