Skip to content

Commit

Permalink
fix(lens/set): program intended application order
Browse files Browse the repository at this point in the history
  • Loading branch information
drizzer14 committed Jul 5, 2024
1 parent 532046f commit 849ad52
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 123 deletions.
9 changes: 5 additions & 4 deletions src/lens/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,20 @@ export default function set (...args: [any, any?, any?]): any {
switch(args.length) {
case 1: {
// @ts-ignore
return <Path extends string, Value>(path: Path, value: Value) => _set(args[0], path, value)
return <Path extends string, Value>(path: Path, value: Value) => setter(args[0], path, value)
}
case 2: {
// @ts-ignore
return <Source extends Flattenable>(source: Source) => _set(source, args[1], args[2])
return <Source extends Flattenable>(source: Source) => setter(source, args[0], args[1])
}
case 3: {
return _set
// @ts-ignore
return setter(...args)
}
}
}

function _set<
function setter<
Source extends Flattenable,
Path extends Flatten<Source>,
// @ts-ignore
Expand Down
117 changes: 0 additions & 117 deletions src/set.ts

This file was deleted.

6 changes: 4 additions & 2 deletions tests/set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sut from '../src/lens/set'
import type { Flatten } from '../src/types'

describe('set', () => {
describe('when setting a property', () => {
it('should return source copy with modified value', () => {
const source = { a: { b: { c: 1 } } }
const result = sut(source, 'a.b.c', 2)
const result = sut(source)('a.b.c', 2)

expect(result).toEqual({ a: { b: { c: 2 } } })
expect(source === result).toBe(false)
Expand All @@ -14,7 +15,8 @@ describe('set', () => {
describe('when setting a nested array\'s element', () => {
it('should return source copy with modified value', () => {
const source = { a: { b: { c: [1] } } }
const result = sut(source, 'a.b.c.0', 2)
const path: Flatten<typeof source> = 'a.b.c.0'
const result = sut<typeof source, typeof path, 2>(path, 2)(source)

expect(result).toEqual({ a: { b: { c: [2] } } })
expect(source === result).toBe(false)
Expand Down

0 comments on commit 849ad52

Please sign in to comment.