diff --git a/src/ReadonlyRecord.ts b/src/ReadonlyRecord.ts index 5e971c2a8..f7a029640 100644 --- a/src/ReadonlyRecord.ts +++ b/src/ReadonlyRecord.ts @@ -1430,10 +1430,7 @@ const _traverseWithIndex = (O: Ord) => ( let fr: HKT> = 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]) ) } diff --git a/test/ReadonlyRecord.ts b/test/ReadonlyRecord.ts index 437894625..9d5cebc43 100644 --- a/test/ReadonlyRecord.ts +++ b/test/ReadonlyRecord.ts @@ -289,27 +289,30 @@ describe('ReadonlyRecord', () => { ) }) - it('traverseWithIndex', () => { - const f = (k: string, n: number): O.Option => (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 => (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', () => {