Skip to content

Commit

Permalink
feat: box member function
Browse files Browse the repository at this point in the history
  • Loading branch information
acrazing committed Oct 24, 2024
1 parent 2795c71 commit dc474bd
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 161 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
38 changes: 18 additions & 20 deletions packages/amos-boxes/src/arrayBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ import { Box, Mutation, ShapeBox } from 'amos-core';
import { removeElement } from 'amos-utils';

export interface ArrayBox<E>
extends ShapeBox<
readonly E[],
'slice' | 'filter',
'at' | 'includes' | 'indexOf' | 'lastIndexOf' | 'find' | 'findIndex' | 'some' | 'every'
> {
push(...items: E[]): Mutation<[...items: E[]], readonly E[]>;
pop(): Mutation<[], readonly E[]>;
unshift(...items: E[]): Mutation<[...items: E[]], readonly E[]>;
shift(): Mutation<[], readonly E[]>;
splice(
start: number,
count: number,
...items: E[]
): Mutation<[start: number, count: number, ...items: E[]], readonly E[]>;
sort(
compare?: (a: E, b: E) => number,
): Mutation<[compare?: (a: E, b: E) => number], readonly E[]>;
delete(...items: E[]): Mutation<E[], readonly E[]>;
extends Box<E>,
ShapeBox<
readonly E[],
'slice' | 'filter',
'at' | 'includes' | 'indexOf' | 'lastIndexOf' | 'find' | 'findIndex' | 'some' | 'every'
> {
push(...items: E[]): Mutation<readonly E[]>;
pop(): Mutation<readonly E[]>;
unshift(...items: E[]): Mutation<readonly E[]>;
shift(): Mutation<readonly E[]>;
splice(start: number, count: number, ...items: E[]): Mutation<readonly E[]>;
sort(compare?: (a: E, b: E) => number): Mutation<readonly E[]>;
delete(...items: E[]): Mutation<readonly E[]>;
}

export const ArrayBox = Box.extends<ArrayBox<any>>({
Expand All @@ -35,8 +30,11 @@ export const ArrayBox = Box.extends<ArrayBox<any>>({
unshift: (state, ...items) => items.concat(state),
shift: (state) => state.slice(1),
slice: null,
splice: (state, start, deleteCount, ...items) =>
state.slice().splice(start, deleteCount, ...items),
splice: (state, start, deleteCount, ...items) => {
const value = state.slice();
value.splice(start, deleteCount, ...items);
return value;
},
sort: (state, compare) => state.slice().sort(compare),
filter: null,
delete: (state, ...items) => removeElement(state.slice(), ...items),
Expand Down
10 changes: 5 additions & 5 deletions packages/amos-boxes/src/boolBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* @author junbao <[email protected]>
*/

import { Box, Mutation, ShapeBox } from 'amos-core';
import { Box, Mutation } from 'amos-core';

export interface BoolBox extends ShapeBox<boolean, never, never> {
toggle(): Mutation<[], boolean>;
open(): Mutation<[], boolean>;
close(): Mutation<[], boolean>;
export interface BoolBox extends Box<boolean> {
toggle(): Mutation<boolean>;
open(): Mutation<boolean>;
close(): Mutation<boolean>;
}

export const BoolBox = Box.extends<BoolBox>({
Expand Down
72 changes: 36 additions & 36 deletions packages/amos-boxes/src/listBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ import { Box, ShapeBox } from 'amos-core';
import { List } from 'amos-shapes';

export interface ListBox<L extends List<any>>
extends ShapeBox<
L,
| 'concat'
| 'copyWithin'
| 'fill'
| 'filterThis'
| 'mapThis'
| 'pop'
| 'push'
| 'reverse'
| 'shift'
| 'unshift'
| 'slice'
| 'sort'
| 'splice'
| 'delete'
| 'set'
| 'reset',
| 'some'
| 'reduce'
| 'reduceRight'
| 'findIndex'
| 'flat'
| 'includes'
| 'indexOf'
| 'join'
| 'lastIndexOf'
| 'map'
| 'find'
| 'filter'
| 'get'
| 'length'
| 'every',
Box<L>,
List<any>
> {}
extends Box<L>,
ShapeBox<
L,
| 'concat'
| 'copyWithin'
| 'fill'
| 'filterThis'
| 'mapThis'
| 'pop'
| 'push'
| 'reverse'
| 'shift'
| 'unshift'
| 'slice'
| 'sort'
| 'splice'
| 'delete'
| 'set'
| 'reset',
| 'some'
| 'reduce'
| 'reduceRight'
| 'findIndex'
| 'flat'
| 'includes'
| 'indexOf'
| 'join'
| 'lastIndexOf'
| 'map'
| 'find'
| 'filter'
| 'get'
| 'length'
| 'every',
List<any>
> {}

export const ListBox = Box.extends<ListBox<any>>({
name: 'ListBox',
Expand Down
48 changes: 24 additions & 24 deletions packages/amos-boxes/src/listMapBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ import { IDOf } from 'amos-utils';
import { MapBox } from './mapBox';

export interface ListMapBox<LM extends ListMap<any, any>>
extends ShapeBox<
LM,
| 'setItem'
| 'setAll'
| 'concatIn'
| 'copyWithinIn'
| 'fillIn'
| 'filterThisIn'
| 'mapThisIn'
| 'popIn'
| 'pushIn'
| 'reverseIn'
| 'shiftIn'
| 'unshiftIn'
| 'sliceIn'
| 'sortIn'
| 'spliceIn'
| 'deleteIn'
| 'setIn'
| 'resetIn',
never,
MapBox<LM>,
ListMap<any, any>
> {}
extends MapBox<LM>,
ShapeBox<
LM,
| 'setItem'
| 'setAll'
| 'concatIn'
| 'copyWithinIn'
| 'fillIn'
| 'filterThisIn'
| 'mapThisIn'
| 'popIn'
| 'pushIn'
| 'reverseIn'
| 'shiftIn'
| 'unshiftIn'
| 'sliceIn'
| 'sortIn'
| 'spliceIn'
| 'deleteIn'
| 'setIn'
| 'resetIn',
never,
ListMap<any, any>
> {}

export const ListMapBox = MapBox.extends<ListMapBox<any>>({
name: 'ListMap',
Expand Down
32 changes: 16 additions & 16 deletions packages/amos-boxes/src/mapBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import { Map } from 'amos-shapes';
import { ID, IDOf } from 'amos-utils';

export interface MapBox<M extends Map<any, any> = Map<any, any>>
extends ShapeBox<
M,
| 'setItem'
| 'setAll'
| 'mergeItem'
| 'mergeAll'
| 'updateItem'
| 'updateAll'
| 'removeItem'
| 'removeAll'
| 'clear'
| 'reset',
'getItem' | 'hasItem' | 'size',
Box<M>,
Map<any, any>
> {}
extends Box<M>,
ShapeBox<
M,
| 'setItem'
| 'setAll'
| 'mergeItem'
| 'mergeAll'
| 'updateItem'
| 'updateAll'
| 'removeItem'
| 'removeAll'
| 'clear'
| 'reset',
'getItem' | 'hasItem' | 'size',
Map<any, any>
> {}

export const MapBox = Box.extends<MapBox>({
name: 'MapBox',
Expand Down
14 changes: 8 additions & 6 deletions packages/amos-boxes/src/numberBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { Box, Mutation, ShapeBox } from 'amos-core';

// should consider the method is selector and mutation at the same time.
// note: don't plan to deal with restricted types of scenarios, it should be the base box type.
export interface NumberBox extends ShapeBox<number, never, 'toExponential' | 'toFixed'> {
add(value: number): Mutation<[number], number>;
minus(value: number): Mutation<[number], number>;
multiply(value: number): Mutation<[number], number>;
divide(value: number): Mutation<[number], number>;
mod(value: number): Mutation<[number], number>;
export interface NumberBox
extends Box<number>,
ShapeBox<number, never, 'toExponential' | 'toFixed'> {
add(value: number): Mutation<number>;
minus(value: number): Mutation<number>;
multiply(value: number): Mutation<number>;
divide(value: number): Mutation<number>;
mod(value: number): Mutation<number>;
}

export const NumberBox = Box.extends<NumberBox>({
Expand Down
6 changes: 2 additions & 4 deletions packages/amos-boxes/src/objectBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import { Box, Mutation, Selector } from 'amos-core';
import { resolveFuncValue, shallowEqual } from 'amos-utils';

export interface ObjectBox<T extends object> extends Box<Readonly<T>> {
mergeState(state: Partial<T>): Mutation<[state: Partial<T>], Readonly<T>>;
mergeState(
next: (state: Readonly<T>) => Partial<T>,
): Mutation<[next: (state: Readonly<T>) => Partial<T>], Readonly<T>>;
mergeState(state: Partial<T>): Mutation<Readonly<T>>;
mergeState(next: (state: Readonly<T>) => Partial<T>): Mutation<Readonly<T>>;
get<K extends keyof T>(key: K): Selector<[K], T[K]>;
pick<Ks extends Array<keyof T>>(...keys: Ks): Selector<Ks, Pick<T, Ks[number]>>;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/amos-boxes/src/recordBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import { Record, RecordProps } from 'amos-shapes';
import { resolveCtorValue, ValueOrConstructor } from 'amos-utils';

export interface RecordBox<R extends Record<any>>
extends ShapeBox<R, 'merge', 'isValid', Box<R>, Record<{}>> {
extends Box<R>,
ShapeBox<R, 'merge', 'isValid', Record<{}>> {
/** @see {Record#get} */
get<K extends keyof RecordProps<R>>(key: K): Selector<[K], R[K]>;
get<K extends keyof RecordProps<R>>(key: K): Selector<R[K]>;
/** @see {Record#set} */
set<K extends keyof RecordProps<R>>(key: K, value: R[K]): Mutation<[K, R[K]], R>;
set<K extends keyof RecordProps<R>>(key: K, value: R[K]): Mutation<R>;
/** @see {Record#update} */
update<K extends keyof RecordProps<R>>(
key: K,
updater: (value: R[K], record: R) => R[K],
): Mutation<[K, (value: R[K], record: R) => R[K]], R>;
): Mutation<R>;
}

export const RecordBox = Box.extends<RecordBox<any>>({
Expand Down
30 changes: 9 additions & 21 deletions packages/amos-boxes/src/recordMapBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author junbao <[email protected]>
*/

import { Mutation, ShapeBox } from 'amos-core';
import { Box, Mutation, ShapeBox } from 'amos-core';
import {
MapKey,
MapValue,
Expand All @@ -14,29 +14,17 @@ import {
RecordMapKeyField,
RecordProps,
} from 'amos-shapes';
import { IDKeyof, OmitKeys, resolveCtorValue, ValueOrConstructor } from 'amos-utils';
import { IDKeyof, resolveCtorValue, ValueOrConstructor } from 'amos-utils';
import { MapBox } from './mapBox';

export interface RecordMapBox<RM extends RecordMap<any, any> = RecordMap<any, any>>
extends ShapeBox<
RM,
'setAll' | 'mergeAll',
never,
OmitKeys<MapBox<RM>, 'setItem' | 'setAll' | 'mergeItem'>,
RecordMap<Record<{}>, never>
> {
setItem(
key: MapKey<RM>,
value: MapValue<RM>,
): Mutation<[key: MapKey<RM>, value: MapValue<RM>], RM>;
setItem(value: MapValue<RM>): Mutation<[value: MapValue<RM>], RM>;
mergeItem(
props: PartialRequiredProps<MapValue<RM>, RecordMapKeyField<RM>>,
): Mutation<[props: PartialRequiredProps<MapValue<RM>, RecordMapKeyField<RM>>], RM>;
mergeItem(
key: MapKey<RM>,
props: PartialProps<MapValue<RM>>,
): Mutation<[key: MapKey<RM>, props: PartialProps<MapValue<RM>>], RM>;
extends Omit<MapBox<RM>, 'setItem' | 'mergeItem' | keyof Box>,
Box<RM>,
ShapeBox<RM, 'setAll' | 'mergeAll', never, RecordMap<Record<{}>, never>> {
setItem(key: MapKey<RM>, value: MapValue<RM>): Mutation<RM>;
setItem(value: MapValue<RM>): Mutation<RM>;
mergeItem(props: PartialRequiredProps<MapValue<RM>, RecordMapKeyField<RM>>): Mutation<RM>;
mergeItem(key: MapKey<RM>, props: PartialProps<MapValue<RM>>): Mutation<RM>;
}

export const RecordMapBox = MapBox.extends<RecordMapBox>({
Expand Down
Loading

0 comments on commit dc474bd

Please sign in to comment.