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/__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) {} 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/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 new file mode 100644 index 00000000..10bf5c65 --- /dev/null +++ b/src/class/index.ts @@ -0,0 +1,2 @@ +export * from './CalcChain'; +export * from './StateTool'; 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'; /** * 坐标系相关的工具函数 */