Skip to content

Commit 159a5b2

Browse files
committed
✨ feat: add @algorithm.ts/types
1 parent 1863c63 commit 159a5b2

File tree

10 files changed

+235
-10
lines changed

10 files changed

+235
-10
lines changed

Diff for: packages/types/README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<header>
2+
<h1 align="center">
3+
<a href="https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/types#readme">@algorithm.ts/types</a>
4+
</h1>
5+
<div align="center">
6+
<a href="https://www.npmjs.com/package/@algorithm.ts/types">
7+
<img
8+
alt="Npm Version"
9+
src="https://img.shields.io/npm/v/@algorithm.ts/types.svg"
10+
/>
11+
</a>
12+
<a href="https://www.npmjs.com/package/@algorithm.ts/types">
13+
<img
14+
alt="Npm Download"
15+
src="https://img.shields.io/npm/dm/@algorithm.ts/types.svg"
16+
/>
17+
</a>
18+
<a href="https://www.npmjs.com/package/@algorithm.ts/types">
19+
<img
20+
alt="Npm License"
21+
src="https://img.shields.io/npm/l/@algorithm.ts/types.svg"
22+
/>
23+
</a>
24+
<a href="#install">
25+
<img
26+
alt="Module Formats: cjs, esm"
27+
src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
28+
/>
29+
</a>
30+
<a href="https://github.com/nodejs/node">
31+
<img
32+
alt="Node.js Version"
33+
src="https://img.shields.io/node/v/@algorithm.ts/types"
34+
/>
35+
</a>
36+
<a href="https://github.com/facebook/jest">
37+
<img
38+
alt="Tested with Jest"
39+
src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
40+
/>
41+
</a>
42+
<a href="https://github.com/prettier/prettier">
43+
<img
44+
alt="Code Style: prettier"
45+
src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
46+
/>
47+
</a>
48+
</div>
49+
</header>
50+
<br/>
51+
52+
53+
Common types.
54+
55+
## Install
56+
57+
* npm
58+
59+
```bash
60+
npm install --save @algorithm.ts/types
61+
```
62+
63+
* yarn
64+
65+
```bash
66+
yarn add @algorithm.ts/types
67+
```
68+
69+
70+
## Usage
71+
72+
### ICollection
73+
74+
Signature | Description
75+
:-----------------------|:------------------------------------------------------------------------
76+
`readonly size: number` | Count the element in the collection.
77+
`destroy(): void` | Release memory.
78+
`clear(): void` | Remove all elements. (Notice that this method does not release memory)
79+
80+
81+
## Related
82+
83+
84+
[homepage]: https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/types#readme

Diff for: packages/types/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@algorithm.ts/types",
3+
"version": "2.0.14",
4+
"description": "Common types for the algorithm.ts packages.",
5+
"author": {
6+
"name": "guanghechen",
7+
"url": "https://github.com/guanghechen/"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x",
12+
"directory": "packages/types"
13+
},
14+
"homepage": "https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/types#readme",
15+
"main": "lib/cjs/index.js",
16+
"module": "lib/esm/index.js",
17+
"types": "lib/types/index.d.ts",
18+
"source": "src/index.ts",
19+
"license": "MIT",
20+
"engines": {
21+
"node": ">= 14.15.0"
22+
},
23+
"files": [
24+
"lib/",
25+
"!lib/**/*.js.map",
26+
"!lib/**/*.d.ts.map",
27+
"package.json",
28+
"CHANGELOG.md",
29+
"LICENSE",
30+
"README.md"
31+
],
32+
"scripts": {
33+
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js",
34+
"prebuild": "rimraf lib/",
35+
"prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
36+
"test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
37+
}
38+
}

Diff for: packages/types/src/collection.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export interface ICollection<T> extends Iterable<T> {
2+
/**
3+
* Count the element in the collection.
4+
* @getter
5+
*/
6+
readonly size: number
7+
/**
8+
* Release memory.
9+
*/
10+
destroy(): void
11+
/**
12+
* Remove all elements.
13+
* Notice that this method does not release memory.
14+
*/
15+
clear(): void
16+
}

Diff for: packages/types/src/graph.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Adjacent list
3+
*/
4+
export type IAdjacentList = number[][]
5+
6+
export interface IDigraphEdge {
7+
/**
8+
* Target point of the edge.
9+
*/
10+
to: number
11+
}
12+
13+
export interface IDigraph<E extends IDigraphEdge = IDigraphEdge> {
14+
/**
15+
* The number of nodes in the graph. (0-index)
16+
*/
17+
N: number
18+
/**
19+
* Adjacency list. G[i] represent the index list of the edges start from node i.
20+
*/
21+
G: number[][]
22+
/**
23+
*
24+
*/
25+
edges: E[]
26+
}

Diff for: packages/types/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './collection'
2+
export * from './graph'
3+
export * from './misc'
4+
export * from './operand'

Diff for: packages/types/src/misc.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type ICompare<T> = (x: T, y: T) => -1 | 0 | 1 | number
2+
export type IEquals<T> = (x: T, y: T) => boolean
3+
export type IKeyOf<T, K = number> = (element: T) => K
4+
5+
/**
6+
* Make all properties in `T` mutable.
7+
* @see https://stackoverflow.com/a/46634877
8+
*/
9+
export type Mutable<T extends object> = { -readonly [P in keyof T]: T[P] }
10+
11+
/**
12+
* Make a set of properties by key `K` become optional from `T`.
13+
*/
14+
export type PickPartial<T extends object, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
15+
16+
/**
17+
* Make an object deep-readonly.
18+
*/
19+
export type DeepReadonly<T extends object> = {
20+
readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : Readonly<T[P]>
21+
}
22+
23+
/**
24+
* Get element type of an array
25+
*/
26+
export type ElementOfArray<T extends ReadonlyArray<unknown> | string> = T extends ReadonlyArray<
27+
infer ElementType
28+
>
29+
? ElementType
30+
: T extends string
31+
? string
32+
: never

Diff for: packages/types/src/operand.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface IOperator<T> {
2+
readonly ZERO: T
3+
4+
readonly ONE: T
5+
6+
// x + y
7+
add(x: T, y: T): T
8+
9+
// x - y
10+
subtract(x: T, y: T): T
11+
12+
// x / y
13+
divide(x: T, y: T): T
14+
15+
// x * y
16+
multiple(x: T, y: T): T
17+
}

Diff for: packages/types/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig",
3+
"include": ["src", "script", "__test__"]
4+
}

Diff for: packages/types/tsconfig.src.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.settings",
3+
"compilerOptions": {
4+
"rootDir": "src"
5+
},
6+
"include": ["src"]
7+
}

Diff for: tsconfig.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
"@algorithm.ts/base64": ["packages/base64/src"],
99
"@algorithm.ts/bellman-ford": ["packages/bellman-ford/src"],
1010
"@algorithm.ts/binary-index-tree": ["packages/binary-index-tree/src"],
11-
"@algorithm.ts/bipartite-graph-matching": ["packages/bipartite-graph-matching/src"],
12-
"@algorithm.ts/calculate": ["packages/calculate/src"],
13-
"@algorithm.ts/circular-queue": ["packages/circular-queue/src"],
11+
"@algorithm.ts/bipartite-matching": ["packages/bipartite-matching/src"],
12+
"@algorithm.ts/binary-search": ["packages/binary-search/src"],
13+
"@algorithm.ts/calculator": ["packages/calculator/src"],
1414
"@algorithm.ts/dijkstra": ["packages/dijkstra/src"],
15-
"@algorithm.ts/dijkstra-bigint": ["packages/dijkstra-bigint/src"],
1615
"@algorithm.ts/dinic": ["packages/dinic/src"],
1716
"@algorithm.ts/dlx": ["packages/dlx/src"],
1817
"@algorithm.ts/findset": ["packages/findset/src"],
@@ -21,19 +20,17 @@
2120
"@algorithm.ts/graph": ["packages/graph/src"],
2221
"@algorithm.ts/huffman": ["packages/huffman"],
2322
"@algorithm.ts/isap": ["packages/isap/src"],
24-
"@algorithm.ts/knuth-shuffle": ["packages/knuth-shuffle/src"],
2523
"@algorithm.ts/lcs": ["packages/lcs/src"],
26-
"@algorithm.ts/lower-bound": ["packages/lower-bound/src"],
2724
"@algorithm.ts/manacher": ["packages/manacher/src"],
2825
"@algorithm.ts/mcmf": ["packages/mcmf/src"],
29-
"@algorithm.ts/priority-queue": ["packages/priority-queue/src"],
26+
"@algorithm.ts/queue": ["packages/queue/src"],
3027
"@algorithm.ts/roman": ["packages/roman/src"],
31-
"@algorithm.ts/sieve-prime": ["packages/sieve-prime/src"],
32-
"@algorithm.ts/sieve-totient": ["packages/sieve-totient/src"],
28+
"@algorithm.ts/shuffle": ["packages/shuffle/src"],
29+
"@algorithm.ts/prime": ["packages/prime/src"],
3330
"@algorithm.ts/sliding-window": ["packages/sliding-window/src"],
3431
"@algorithm.ts/sudoku": ["packages/sudoku/src"],
3532
"@algorithm.ts/trie": ["packages/trie/src"],
36-
"@algorithm.ts/upper-bound": ["packages/upper-bound/src"],
33+
"@algorithm.ts/types": ["packages/types/src"],
3734
"jest.setup": ["jest.setup.ts"]
3835
}
3936
},

0 commit comments

Comments
 (0)