Skip to content

Commit 6239f88

Browse files
committed
struct: evolve to skip unspecified transformations
1 parent bb9c6dd commit 6239f88

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/struct.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ export const getAssignSemigroup = <A extends object = never>(): Semigroup<A> =>
5353
*
5454
* @since 2.11.0
5555
*/
56-
export const evolve = <A, F extends { [K in keyof A]: (a: A[K]) => unknown }>(transformations: F) => (
56+
export const evolve = <A, F extends { [K in keyof A]?: (a: A[K]) => unknown }>(transformations: F) => (
5757
a: A
58-
): { [K in keyof F]: ReturnType<F[K]> } => {
58+
): { [K in keyof A]: F[K] extends (a: A[K]) => unknown ? ReturnType<F[K]> : A[K] } => {
5959
const out: Record<string, unknown> = {}
6060
for (const k in a) {
6161
if (_.has.call(a, k)) {
62-
out[k] = transformations[k](a[k])
62+
const fn = transformations[k]
63+
out[k] = typeof fn === 'function' ? fn(a[k]) : a[k]
6364
}
6465
}
6566
return out as any

test/struct.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pipe } from '../src/function'
1+
import { pipe, increment } from '../src/function'
22
import * as _ from '../src/struct'
33
import * as U from './util'
44

@@ -35,5 +35,7 @@ describe('struct', () => {
3535
const x: Record<'b', number> = Object.create({ a: 1 })
3636
x.b = 1
3737
U.deepStrictEqual(pipe(x, _.evolve({ b: (b) => b > 0 })), { b: true })
38+
// does not invoke absent transformations
39+
U.deepStrictEqual(pipe({ a: 1, b: 's' }, _.evolve({ a: increment })), { a: 2, b: 's' })
3840
})
3941
})

0 commit comments

Comments
 (0)