Skip to content

Commit

Permalink
refactor: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyshlyaev177 committed Nov 10, 2024
1 parent f064586 commit 5768f61
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/urlstate/react-router/useUrlState/useUrlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export function useUrlState<T extends JSONCompatible>(
updateState(
assignValue(
_defaultState,
state as T,
filterUnknownParams(
_defaultState,
parseSPObj(
Expand Down
10 changes: 4 additions & 6 deletions packages/urlstate/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,25 @@ describe('assignValue', () => {
}

it('should assign a value', () => {
expect(clone(assignValue(shape, { a1: 2 }, { ...shape, a1: 3 }))).toStrictEqual(clone({
expect(clone(assignValue(shape, { ...shape, a1: 3 }))).toStrictEqual(clone({
...shape,
a1: 3,
}))
})

it('should return a new instance of object', () => {
const curr = structuredClone(shape)
const newVal = {}
const result = assignValue(shape, curr, newVal)
const result = assignValue(shape, newVal)
expect(result === shape).toBeFalsy()
expect(result === curr).toBeFalsy()
expect(result === newVal).toBeFalsy()
})

it('should override curr value with newValue', () => {
expect(clone(assignValue(shape, { a1: 2 }, { a1: 3 }))).toStrictEqual(clone({ ...shape, a1: 3 }))
expect(clone(assignValue(shape, { a1: 3 }))).toStrictEqual(clone({ ...shape, a1: 3 }))
})

it('should use value from shape as default', () => {
expect(clone(assignValue(shape, { a1: 2 }, {}))).toStrictEqual(clone({ ...shape, a1: shape.a1 }))
expect(clone(assignValue(shape, {}))).toStrictEqual(clone({ ...shape, a1: shape.a1 }))
})
})

Expand Down
8 changes: 2 additions & 6 deletions packages/urlstate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ function filterUnknown<T extends object>(
.map(([key, val]) => [key.replaceAll("+", " "), val]);
}

export function assignValue<T extends object>(
shape: T,
curr: Partial<T>,
newVal: Partial<T>,
) {
const result: T = Object.assign({}, shape, curr);
export function assignValue<T extends object>(shape: T, newVal: Partial<T>) {
const result: T = Object.assign({}, shape);

Object.entries(shape).forEach(([key]) => {
const _key = key as keyof T;
Expand Down

0 comments on commit 5768f61

Please sign in to comment.