Skip to content

Commit

Permalink
refactor: 使用重载的方式声明类型
Browse files Browse the repository at this point in the history
  • Loading branch information
MinJieLiu committed May 15, 2022
1 parent fa40d2b commit 5a06344
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "immot",
"version": "0.3.0",
"version": "0.3.1",
"type": "module",
"description": "Create a new copy without changing the original data.",
"exports": "./dist/immot.modern.js",
Expand Down
16 changes: 8 additions & 8 deletions src/immot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,30 @@ export const $updateIn: UpdateInOperator = <S, KS>(state: S, keyPath: KS, update
return baseUpdateIn(state, keyPath, updater);
};

export const $delete = <S>(state: S, key: DeleteFnPath<S>): S => {
export const $delete = <S>(state: S, keyList: DeleteFnPath<S>): S => {
if (isArray(state)) {
if (isArray(key)) {
return state.filter((n, i) => !key.includes(i)) as unknown as S;
if (isArray(keyList)) {
return state.filter((n, i) => !keyList.includes(i)) as unknown as S;
}
return $splice(state, key as number, 1) as unknown as S;
return $splice(state, keyList as number, 1) as unknown as S;
}

if (isMap(state)) {
const result = new Map(state as unknown as Map<unknown, unknown>);
if (isArray(key)) {
key.forEach((key) => {
if (isArray(keyList)) {
keyList.forEach((key) => {
result.delete(key);
});
} else {
result.delete(key);
result.delete(keyList);
}
return result as unknown as S;
}

const result = {} as S;
const keys = Object.keys(state);
keys.forEach((key) => {
if (isArray(key) ? !key.includes(key) : key !== key) {
if (isArray(keyList) ? !keyList.includes(key) : keyList !== key) {
result[key] = state[key];
}
});
Expand Down

0 comments on commit 5a06344

Please sign in to comment.