From 3fd11cbf093724d0b7f477c5879319ead68dff1d Mon Sep 17 00:00:00 2001 From: dyh_a Date: Fri, 24 Apr 2026 06:58:01 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E6=8A=8A=20CalcChain=20?= =?UTF-8?q?=E4=BB=8E=E6=A0=B9=E7=9B=AE=E5=BD=95=E7=A7=BB=E5=8A=A8=E5=88=B0?= =?UTF-8?q?=20class=20=E7=9B=AE=E5=BD=95=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/CalcChain.test.ts | 2 +- src/{ => class}/CalcChain.ts | 2 +- src/class/index.ts | 1 + src/coordinate/getRotatePoint.ts | 2 +- src/index.ts | 2 +- src/index.typedoc.ts | 4 ++-- 6 files changed, 7 insertions(+), 6 deletions(-) rename src/{ => class}/CalcChain.ts (97%) create mode 100644 src/class/index.ts diff --git a/__tests__/CalcChain.test.ts b/__tests__/CalcChain.test.ts index acd52887..4b69ec55 100644 --- a/__tests__/CalcChain.test.ts +++ b/__tests__/CalcChain.test.ts @@ -1,4 +1,4 @@ -import { CalcChain } from '../src/CalcChain'; +import { CalcChain } from '../src'; describe('Calc', function () { test('Calc', () => { diff --git a/src/CalcChain.ts b/src/class/CalcChain.ts similarity index 97% rename from src/CalcChain.ts rename to src/class/CalcChain.ts index c16134ba..cf48c92a 100644 --- a/src/CalcChain.ts +++ b/src/class/CalcChain.ts @@ -1,4 +1,4 @@ -import { divide, minus, times, strip, plus } from './number'; +import { divide, minus, times, strip, plus } from '../number'; /** * 链式计算 diff --git a/src/class/index.ts b/src/class/index.ts new file mode 100644 index 00000000..ab6a5210 --- /dev/null +++ b/src/class/index.ts @@ -0,0 +1 @@ +export * from './CalcChain'; diff --git a/src/coordinate/getRotatePoint.ts b/src/coordinate/getRotatePoint.ts index 2d8d151e..34427178 100644 --- a/src/coordinate/getRotatePoint.ts +++ b/src/coordinate/getRotatePoint.ts @@ -1,5 +1,5 @@ import type { Point } from '@tool-pack/types'; -import { CalcChain } from '../CalcChain'; +import { CalcChain } from '../class'; /** * 根据半径与角度获取对应坐标点 diff --git a/src/index.ts b/src/index.ts index 4335d94a..08359baa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ export * from './object'; export * from './promise'; export * from './time'; export * from './common'; -export * from './CalcChain'; export * from './coordinate'; export * from './random'; export * from './color'; @@ -16,3 +15,4 @@ export * from './bezier'; export * from './generator'; export * from './array-buffer'; export * from './base64'; +export * from './class'; diff --git a/src/index.typedoc.ts b/src/index.typedoc.ts index f4a98e46..2d100c9f 100644 --- a/src/index.typedoc.ts +++ b/src/index.typedoc.ts @@ -36,9 +36,9 @@ export * as Time from './time'; */ export * as Clone from './clone'; /** - * 计算链工具函数 + * 各种工具类 */ -export * as CalcChain from './CalcChain'; +export * as Classes from './class'; /** * 坐标系相关的工具函数 */ From 1ab4c827a85a4abce2b15c4f732a6d089116baf0 Mon Sep 17 00:00:00 2001 From: dyh_a Date: Fri, 24 Apr 2026 08:31:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat(class):=20=E6=96=B0=E5=A2=9E=20StateTo?= =?UTF-8?q?ol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/class/StateTool.ts | 46 ++++++++++++++++++++++++++++++++++++++++++ src/class/index.ts | 1 + 2 files changed, 47 insertions(+) create mode 100644 src/class/StateTool.ts diff --git a/src/class/StateTool.ts b/src/class/StateTool.ts new file mode 100644 index 00000000..71fcd71a --- /dev/null +++ b/src/class/StateTool.ts @@ -0,0 +1,46 @@ +/** + * 状态工具 + * + * 一个简单的状态判断封装, + * 主要是写代码时总要把状态从===判断改成[state1,state2,...].includes(state), + * 而封装后,我只要把 is 方法改为 in 然后添加参数就行, + * 而且 in 方法预设的类型约束比现写数组的要好(可能要再写一遍约束) + * + * @example + * enum Color { + * RED, + * BLUE, + * GREEN, + * } + * + * const state = new StateTool(Color.RED); + * state.is(Color.RED); // return true + * state.in(Color.BLUE, Color.RED, Color.GREEN) // return true + * + * state.value = Color.GREEN; + * state.is(Color.GREEN); // return true + * state.value // Color.GREEN + * + * // 必须指定类型 + * const state2 = new StateTool(null); + * state2.value // null + * state2.value = Color.RED; + */ +export class StateTool { + constructor(private state: T) {} + + in(...status: T[]): boolean { + return status.includes(this.state); + } + is(state: T): boolean { + return this.state === state; + } + // 为什么不直接在构造函数内把参数设置为 public value: T + // 是因为后续如果要继承这个类给 set 或者 get 加个过滤的话,就要另外起一个名字,不够灵活 + set value(state: T) { + this.state = state; + } + get value(): T { + return this.state; + } +} diff --git a/src/class/index.ts b/src/class/index.ts index ab6a5210..10bf5c65 100644 --- a/src/class/index.ts +++ b/src/class/index.ts @@ -1 +1,2 @@ export * from './CalcChain'; +export * from './StateTool'; From 1aa063df8e9c8ce32c2f45a4b63b3880c5b42545 Mon Sep 17 00:00:00 2001 From: dyh_a Date: Fri, 24 Apr 2026 08:31:53 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test(class):=20=E6=96=B0=E5=A2=9E=20StateTo?= =?UTF-8?q?ol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/StateTool.test.ts | 52 +++++++++++++++++++++++++++++++++ __tests__/object/common.test.ts | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 __tests__/StateTool.test.ts diff --git a/__tests__/StateTool.test.ts b/__tests__/StateTool.test.ts new file mode 100644 index 00000000..397f6b41 --- /dev/null +++ b/__tests__/StateTool.test.ts @@ -0,0 +1,52 @@ +import { expectError, expectType } from './object/common.test'; +import { StateTool } from '../src'; + +describe('StateTool', () => { + enum Color { + RED, + BLUE, + GREEN, + } + it('is', () => { + const state = new StateTool(Color.RED); + expect(state.is(Color.RED)).toBe(true); + expect(state.is(Color.GREEN)).toBe(false); + // @ts-expect-error + expectError(state.is('green')); + }); + it('in', () => { + const state = new StateTool(Color.BLUE); + expect(state.in(Color.BLUE, Color.RED, Color.GREEN)).toBe(true); + expect(state.in(Color.RED, Color.GREEN)).toBe(false); + // @ts-expect-error + expectError(state.in('green')); + }); + it('set', () => { + const state = new StateTool(Color.RED); + expect(state.is(Color.RED)).toBe(true); + expect(state.is(Color.GREEN)).toBe(false); + state.value = Color.GREEN; + expect(state.is(Color.GREEN)).toBe(true); + // @ts-expect-error + expectError((state.value = 5)); + }); + it('is, no default', () => { + const state = new StateTool(null); + expect(state.in(Color.BLUE, Color.RED, Color.GREEN)).toBe(false); + expect(state.value === null).toBe(true); + // @ts-expect-error + expectError(state.is('green')); + }); + it('value type', () => { + const state = new StateTool(void 0); + state.value = Color.RED; + state.value = undefined; + expectType(state.value); + + const state2 = new StateTool(Color.RED); + state2.value = Color.GREEN; + // @ts-expect-error + state2.value = undefined; + expectType(state2.value); + }); +}); diff --git a/__tests__/object/common.test.ts b/__tests__/object/common.test.ts index 30141fd7..1446d261 100644 --- a/__tests__/object/common.test.ts +++ b/__tests__/object/common.test.ts @@ -2,7 +2,7 @@ import * as cm from '../../src/object/common'; import * as arr from '../../src/array'; import { forEachNum } from '../../src'; -function expectType(_value: T) {} +export function expectType(_value: T) {} export function expectError(_value: T) {}