From c8ee1222b1393f21cf1ab0c841e8062055cd46e1 Mon Sep 17 00:00:00 2001 From: Cotton Hou Date: Wed, 8 Jun 2022 23:50:18 +0800 Subject: [PATCH] struct: evolve to skip unspecified transformations --- src/struct.ts | 7 ++++--- test/struct.ts | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/struct.ts b/src/struct.ts index 399c1e5ec0..465fff0913 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -54,12 +54,13 @@ export const getAssignSemigroup = (): Semigroup => * @since 2.11.0 */ export const evolve = - unknown }>(transformations: F) => - (a: A): { [K in keyof F]: ReturnType } => { + unknown }>(transformations: F) => + (a: A): { [K in keyof A]: F[K] extends (a: A[K]) => unknown ? ReturnType : A[K] } => { const out: Record = {} for (const k in a) { if (_.has.call(a, k)) { - out[k] = transformations[k](a[k]) + const fn = transformations[k] + out[k] = typeof fn === 'function' ? fn(a[k]) : a[k] } } return out as any diff --git a/test/struct.ts b/test/struct.ts index 73d1312aad..eeaba84465 100644 --- a/test/struct.ts +++ b/test/struct.ts @@ -1,4 +1,4 @@ -import { pipe } from '../src/function' +import { pipe, increment } from '../src/function' import * as _ from '../src/struct' import * as U from './util' @@ -35,5 +35,7 @@ describe('struct', () => { const x: Record<'b', number> = Object.create({ a: 1 }) x.b = 1 U.deepStrictEqual(pipe(x, _.evolve({ b: (b) => b > 0 })), { b: true }) + // does not invoke absent transformations + U.deepStrictEqual(pipe({ a: 1, b: 's' }, _.evolve({ a: increment })), { a: 2, b: 's' }) }) })