-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
struct.ts
66 lines (62 loc) · 1.76 KB
/
struct.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @since 2.10.0
*/
import * as _ from './internal'
import { Semigroup } from './Semigroup'
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------
/**
* Return a semigroup which works like `Object.assign`.
*
* @example
* import { getAssignSemigroup } from 'fp-ts/struct'
*
* interface Person {
* readonly name: string
* readonly age: number
* }
*
* const S = getAssignSemigroup<Person>()
* assert.deepStrictEqual(S.concat({ name: 'name', age: 23 }, { name: 'name', age: 24 }), { name: 'name', age: 24 })
*
* @category instances
* @since 2.10.0
*/
export const getAssignSemigroup = <A extends object = never>(): Semigroup<A> => ({
concat: (first, second) => Object.assign({}, first, second)
})
// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
/**
* Creates a new object by recursively evolving a shallow copy of `a`, according to the `transformation` functions.
*
* @example
* import { pipe } from 'fp-ts/function'
* import { evolve } from 'fp-ts/struct'
*
* assert.deepStrictEqual(
* pipe(
* { a: 'a', b: 1 },
* evolve({
* a: (a) => a.length,
* b: (b) => b * 2
* })
* ),
* { a: 1, b: 2 }
* )
*
* @since 2.11.0
*/
export const evolve =
<A, F extends { [K in keyof A]: (a: A[K]) => unknown }>(transformations: F) =>
(a: A): { [K in keyof F]: ReturnType<F[K]> } => {
const out: Record<string, unknown> = {}
for (const k in a) {
if (_.has.call(a, k)) {
out[k] = transformations[k](a[k])
}
}
return out as any
}