Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/CalcChain.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CalcChain } from '../src/CalcChain';
import { CalcChain } from '../src';

describe('Calc', function () {
test('Calc', () => {
Expand Down
52 changes: 52 additions & 0 deletions __tests__/StateTool.test.ts
Original file line number Diff line number Diff line change
@@ -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<Color | null>(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<Color | void>(void 0);
state.value = Color.RED;
state.value = undefined;
expectType<Color | void>(state.value);

const state2 = new StateTool(Color.RED);
state2.value = Color.GREEN;
// @ts-expect-error
state2.value = undefined;
expectType<Color>(state2.value);
});
});
2 changes: 1 addition & 1 deletion __tests__/object/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cm from '../../src/object/common';
import * as arr from '../../src/array';
import { forEachNum } from '../../src';

function expectType<T>(_value: T) {}
export function expectType<T>(_value: T) {}

export function expectError<T>(_value: T) {}

Expand Down
2 changes: 1 addition & 1 deletion src/CalcChain.ts → src/class/CalcChain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { divide, minus, times, strip, plus } from './number';
import { divide, minus, times, strip, plus } from '../number';

/**
* 链式计算
Expand Down
46 changes: 46 additions & 0 deletions src/class/StateTool.ts
Original file line number Diff line number Diff line change
@@ -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<Color | null>(null);
* state2.value // null
* state2.value = Color.RED;
*/
export class StateTool<T> {
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;
}
}
2 changes: 2 additions & 0 deletions src/class/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './CalcChain';
export * from './StateTool';
2 changes: 1 addition & 1 deletion src/coordinate/getRotatePoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Point } from '@tool-pack/types';
import { CalcChain } from '../CalcChain';
import { CalcChain } from '../class';

/**
* 根据半径与角度获取对应坐标点
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,3 +15,4 @@ export * from './bezier';
export * from './generator';
export * from './array-buffer';
export * from './base64';
export * from './class';
4 changes: 2 additions & 2 deletions src/index.typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export * as Time from './time';
*/
export * as Clone from './clone';
/**
* 计算链工具函数
* 各种工具类
*/
export * as CalcChain from './CalcChain';
export * as Classes from './class';
/**
* 坐标系相关的工具函数
*/
Expand Down
Loading