Skip to content

Commit

Permalink
release: v1.0.0-alpha.0
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoruikang committed Dec 13, 2024
0 parents commit 072f207
Show file tree
Hide file tree
Showing 44 changed files with 4,660 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .commitlintrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { UserConfig } from '@commitlint/types';
import { RuleConfigSeverity } from '@commitlint/types';
import { globSync } from 'glob';
import { basename } from 'node:path';
import { packageDir } from './scripts/utils';

export default <UserConfig>{
extends: ['@commitlint/config-conventional'],
rules: {
'scope-enum': [
RuleConfigSeverity.Error,
'always',
globSync(`${packageDir}/*/`).map(pkg => basename(pkg))
],
'type-enum': [
RuleConfigSeverity.Error,
'always',
['feat', 'chore', 'release', 'fix', 'test', 'refactor']
],
release: [RuleConfigSeverity.Error, 'always']
},
plugins: [
{
rules: {
release({ type, subject }) {
if (type !== 'release') return [true];
if (
!subject ||
!/^v?\d+\.\d+\.\d+(-(rc|beta|alpha)(\.\d+)?)?$/.test(subject)
)
return [
false,
`release type must follow format 'release: vX.Y.Z[-rc|beta|alpha][.W]'`
];
return [true];
}
}
}
]
};
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release

on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+*

jobs:
release:
runs-on: ubuntu-latest
environment: Release
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version-file: package.json
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'
- name: Install deps
run: pnpm install
- name: publish
run: pnpm release --provenance
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
dist/
.eslintcache
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm commitlint --edit $1
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm lint-staged
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 80,
"semi": true,
"singleQuote": true,
"arrowParens": "avoid",
"trailingComma": "none",
"tabWidth": 2
}
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"eslint.useFlatConfig": true,
"eslint.options": {
"flags": ["unstable_ts_config"]
},
"eslint.format.enable": false,
"editor.formatOnSave": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 lrk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
TARGET: string;
OUT: string;
DEV: 'true' | 'false';
}
interface Process {
env: ProcessEnv;
}
}

type BuildFormat = 'cjs' | 'esm' | 'iife';
}

declare module 'pkg-types' {
interface PackageBuildOptions {
name?: string;
internal?: string[];
}

interface PackageJson {
buildOptions?: PackageBuildOptions;
}
}

declare module 'rollop.config.ts' {
type a = number
}

export {};
22 changes: 22 additions & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Linter } from 'eslint';
import globals from 'globals';
import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintConfigPrettier from 'eslint-config-prettier';

export default <Linter.Config[]>[
js.configs.recommended,
...tseslint.configs.recommended,
{ ignores: ['**/dist/', '**/node_modules/'] },
{
languageOptions: {
globals: globals.es2016
},
rules: {
'@typescript-eslint/no-this-alias': 'off',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/no-explicit-any': 'off'
}
},
eslintConfigPrettier
];
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"private": true,
"version": "1.0.0-alpha.0",
"packageManager": "[email protected]",
"engines": {
"node": ">=20.15.1"
},
"type": "module",
"scripts": {
"build": "tsx ./scripts/build.ts",
"clean": "rimraf -g ./packages/*/dist",
"dev": "pnpm build --dev",
"lint": "eslint . --flag unstable_ts_config --cache",
"lint:fix": "pnpm lint --fix",
"format": "pnpm prettier packages/** -c .prettierrc --write",
"sync-version": "tsx ./scripts/sync-version.ts",
"release": "tsx ./scripts/release.ts",
"prepare": "husky"
},
"lint-staged": {
"**/*.ts": [
"pnpm lint:fix",
"pnpm format",
"pnpm sync-version"
]
},
"devDependencies": {
"@commitlint/cli": "^19.6.0",
"@commitlint/config-conventional": "^19.6.0",
"@commitlint/types": "^19.5.0",
"@eslint/js": "^9.16.0",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.1",
"@types/eslint-config-prettier": "^6.11.3",
"@types/node": "^20.17.10",
"@types/semver": "^7.5.8",
"eslint": "^9.16.0",
"eslint-config-prettier": "^9.1.0",
"execa": "^9.5.2",
"glob": "^11.0.0",
"globals": "^15.13.0",
"husky": "^9.1.7",
"jiti": "^2.4.1",
"lint-staged": "^15.2.11",
"picocolors": "^1.1.1",
"pkg-types": "^1.2.1",
"prettier": "^3.4.2",
"rimraf": "^6.0.1",
"rollup": "^4.28.1",
"rollup-plugin-dts": "^6.1.1",
"semver": "^7.6.3",
"tsc": "^2.0.4",
"tslib": "^2.8.1",
"tsx": "^4.19.2",
"typescript": "^5.7.2",
"typescript-eslint": "^8.18.0"
}
}
33 changes: 33 additions & 0 deletions packages/avl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@dstl-js/avl",
"version": "1.0.0-alpha.0",
"main": "src/index.ts",
"files": [
"dist"
],
"types": "dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.cjs"
}
}
},
"keywords": [
"datastruct",
"dstl-js",
"tree",
"avl"
],
"author": "Lrk",
"license": "MIT",
"dependencies": {
"@dstl-js/bst": "workspace:^",
"@dstl-js/shared": "workspace:^"
}
}
114 changes: 114 additions & 0 deletions packages/avl/src/AVLTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { BSNode, BSNodeType, BSTree } from '@dstl-js/bst';
import { isUndefined } from '@dstl-js/shared';

enum AVLRotateType {
LL,
RR,
LR,
RL
}

export interface AVLNode<Key, T> extends BSNode<Key, T> {
balanceFactor?: number;
left: AVLNode<Key, T> | null;
right: AVLNode<Key, T> | null;
parent: AVLNode<Key, T> | null;
}

export class AVLTree<T, Key = number> extends BSTree<T, Key> {
declare protected _root: AVLNode<Key, T> | null;

protected _insert(key: Key, value: T): AVLNode<Key, T> | null {
const current = super._insert(key, value);
let node: AVLNode<Key, T> | null = current?.parent ?? null;
let type = current?.type;
while (node && !isUndefined(type)) {
node.balanceFactor ??= 0;
node.balanceFactor += type === BSNodeType.LEFT ? 1 : -1;
if (node.balanceFactor === 0) return current;
if (Math.abs(node.balanceFactor) > 1) break;
type = node.type;
node = node.parent;
}
node && this._adjustment(node);
return current;
}
protected _remove(key: Key): AVLNode<Key, T> | null {
const target: AVLNode<Key, T> | null = super._remove(key);
if (!target) return null;
let node = target.parent ?? null;
let type = target.type;
while (node) {
node.balanceFactor ??= 0;
node.balanceFactor += type === BSNodeType.LEFT ? -1 : 1;
if (
(Math.abs(node.balanceFactor) > 1 &&
!(node = this._adjustment(node) ?? null)) ||
node.balanceFactor !== 0
)
break;
type = node.type;
node = node.parent;
}
return target;
}
private _adjustment(node: AVLNode<Key, T>): AVLNode<Key, T> | undefined {
const rotateType = this._getRotateType(node);
if (rotateType !== void 0) return this._rotate(node, rotateType), node;
else if (node.parent) return this._adjustment(node.parent);
}

private _getRotateType(node: AVLNode<Key, T>) {
if (node.balanceFactor === 2) {
if (node.left?.balanceFactor === 1) return AVLRotateType.LL;
if (node.left?.balanceFactor === -1) return AVLRotateType.LR;
}
if (node.balanceFactor === -2) {
if (node.right?.balanceFactor === -1) return AVLRotateType.RR;
if (node.right?.balanceFactor === 1) return AVLRotateType.RL;
}
}

private _rotate(node: AVLNode<Key, T>, type: AVLRotateType) {
const { left, right } = node;

let balanceFactor: number | undefined;
switch (type) {
case AVLRotateType.LL:
if (left && this._rotateRight(node))
node.balanceFactor = left.balanceFactor = 0;
break;
case AVLRotateType.RR:
if (right && this._rotateLeft(node))
node.balanceFactor = right.balanceFactor = 0;
break;
case AVLRotateType.LR:
if (!left) break;
balanceFactor = left.right?.balanceFactor;

this._rotate(left, AVLRotateType.RR);
this._rotate(node, AVLRotateType.LL);

if (balanceFactor === -1) {
left.balanceFactor = 1;
} else if (balanceFactor === 1) {
node.balanceFactor = -1;
}

break;
case AVLRotateType.RL:
if (!right) break;
balanceFactor = right.left?.balanceFactor;

this._rotate(right, AVLRotateType.LL);
this._rotate(node, AVLRotateType.RR);

if (balanceFactor === 1) {
right.balanceFactor = -1;
} else if (balanceFactor === -1) {
node.balanceFactor = 1;
}
break;
}
}
}
1 change: 1 addition & 0 deletions packages/avl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './AVLTree';
Loading

0 comments on commit 072f207

Please sign in to comment.