-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathassign.js
More file actions
97 lines (94 loc) · 2.79 KB
/
assign.js
File metadata and controls
97 lines (94 loc) · 2.79 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const isPromise = require('./_internal/isPromise')
const objectAssign = require('./_internal/objectAssign')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const curry3 = require('./_internal/curry3')
const functionObjectAll = require('./_internal/functionObjectAll')
// _assign(object Object, funcs Object<function>) -> Promise|Object
const _assign = function (object, funcs) {
const result = functionObjectAll(funcs, [object])
return isPromise(result)
? result.then(curry3(objectAssign, {}, object, __))
: ({ ...object, ...result })
}
/**
* @name assign
*
* @synopsis
* ```coffeescript [specscript]
* type UnarySyncOrAsyncResolver = any=>Promise|any
*
* assign(Promise|Object, Object<UnarySyncOrAsyncResolver|Promise|any>) -> Promise|Object
* assign(Object<UnarySyncOrAsyncResolver|Promise|any>)(Object) -> Promise|Object
* ```
*
* @description
* Function composer and data constructor. Constructs a new object from an argument object and an object of resolvers, promises, or values.
*
* If provided resolver functions, `assign` resolves the values to be assigned at the keys of the resolver functions in the new object by calling those resolvers with the argument object.
*
* ```javascript [playground]
* const assignSquaredAndCubed = assign({
* squared: ({ number }) => number ** 2,
* cubed: ({ number }) => number ** 3,
* })
*
* console.log(assignSquaredAndCubed({ number: 2 }))
* console.log(assignSquaredAndCubed({ number: 3 }))
*
* const n = 1
* const assignN = assign({ n })
*
* console.log(assignN({}))
* ```
*
* If any of the resolvers provided to `assign` are asynchronous, the execution of `assign` with the argument object returns a promise.
*
* ```javascript [playground]
* const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
*
* const asyncAssignTotal = assign({
* async total({ numbers }) {
* await sleep(500)
* return numbers.reduce((a, b) => a + b)
* },
* })
*
* const result = await asyncAssignTotal({ numbers: [1, 2, 3, 4, 5] })
*
* console.log(result)
* ```
*
* If the argument object is a promise, it is resolved for its value before further execution for the eager interface only.
*
* ```javascript [playground]
* assign(Promise.resolve({}), {
* a() {
* return 1
* },
* b() {
* return 2
* },
* }).then(console.log)
* ```
*
* See also:
* * [pipe](/docs/pipe)
* * [all](/docs/all)
* * [get](/docs/get)
* * [set](/docs/set)
* * [pick](/docs/pick)
* * [omit](/docs/omit)
* * [forEach](/docs/forEach)
*
* @execution concurrent
*/
const assign = function (arg0, arg1) {
if (arg1 == null) {
return curry2(_assign, __, arg0)
}
return isPromise(arg0)
? arg0.then(curry2(_assign, __, arg1))
: _assign(arg0, arg1)
}
module.exports = assign