Skip to content

Commit

Permalink
fix(ReadonlyRecord): do not modify records in place in Traverse lik…
Browse files Browse the repository at this point in the history
…e functions
  • Loading branch information
waynevanson authored and gcanti committed Apr 28, 2022
1 parent 17e1a81 commit 802af2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/ReadonlyRecord.ts
Expand Up @@ -1430,10 +1430,7 @@ const _traverseWithIndex = (O: Ord<string>) => <F>(
let fr: HKT<F, Record<string, B>> = F.of({})
for (const key of ks) {
fr = F.ap(
F.map(fr, (r) => (b: B) => {
r[key] = b
return r
}),
F.map(fr, (r) => (b: B) => Object.assign({}, r, { [key]: b })),
f(key, ta[key])
)
}
Expand Down
27 changes: 15 additions & 12 deletions test/ReadonlyRecord.ts
Expand Up @@ -289,27 +289,30 @@ describe('ReadonlyRecord', () => {
)
})

it('traverseWithIndex', () => {
const f = (k: string, n: number): O.Option<number> => (k !== 'a' ? O.some(n) : O.none)
const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f)
U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none)
U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 }))

U.deepStrictEqual(
pipe(
describe('traverseWithIndex', () => {
it('simple Traversal', () => {
const f = (k: string, n: number): O.Option<number> => (k !== 'a' ? O.some(n) : O.none)
const traverseWithIndex = _.traverseWithIndex(O.Applicative)(f)
U.deepStrictEqual(pipe({ a: 1, b: 2 }, traverseWithIndex), O.none)
U.deepStrictEqual(pipe({ b: 2 }, traverseWithIndex), O.some({ b: 2 }))
})

it('should not modify arrays in place', () => {
const result = pipe(
{ a: 2, b: 3 },
_.fromRecord,
_.traverseWithIndex(RA.Applicative)((_, n) => RA.makeBy(n, (i) => i * 4))
),
[
)

U.deepStrictEqual(result, [
{ a: 0, b: 0 },
{ a: 0, b: 4 },
{ a: 0, b: 8 },
{ a: 4, b: 0 },
{ a: 4, b: 4 },
{ a: 4, b: 8 }
]
)
])
})
})

it('getTraversableWithIndex', () => {
Expand Down

0 comments on commit 802af2f

Please sign in to comment.