deno.land / x / rambda@v9.1.1 / source / into.js

نووسراو ببینە
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { isArray } from './_internals/isArray';import { bind } from './bind.js';import { curry } from './curry';import { identity } from './identity';
// move inside fn using closure, i.e. call itconst symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
function _xArrayReduce(xf, acc, list) { let idx = 0; const len = list.length; while (idx < len) { // todo: @@transducer/step' must be constant acc = xf['@@transducer/step'](acc, list[idx]); if (acc?.['@@transducer/reduced']) { acc = acc['@@transducer/value']; break; } idx += 1; } return xf['@@transducer/result'](acc);}
function _createReduce(arrayReduce, methodReduce, iterableReduce) { return function _reduce(xf, acc, list) { if (_isArrayLike(list)) { return arrayReduce(xf, acc, list); } if (list == null) { return acc; } if (list[symIterator] != null) { return iterableReduce(xf, acc, list[symIterator]()); } if (typeof list.next === 'function') { return iterableReduce(xf, acc, list); } if (typeof list.reduce === 'function') { return methodReduce(xf, acc, list, 'reduce'); }
throw new TypeError('reduce: list must be array or iterable'); };}
function _xIterableReduce(xf, acc, iter) { let step = iter.next(); while (!step.done) { acc = xf['@@transducer/step'](acc, step.value); if (acc?.['@@transducer/reduced']) { acc = acc['@@transducer/value']; break; } step = iter.next(); } return xf['@@transducer/result'](acc);}
function _xMethodReduce(xf, acc, obj, methodName) { return xf['@@transducer/result']( obj[methodName](bind(xf['@@transducer/step'], xf), acc), );}
const _xReduce = _createReduce(_xArrayReduce, _xMethodReduce, _xIterableReduce);
function _isTransformer(obj) { return obj != null && typeof obj['@@transducer/step'] === 'function';}
function _isArrayLike(x) { if (isArray(x)) { return true; } if (!x) { return false; } if (typeof x !== 'object') { return false; } if (typeof x === 'string') { return false; } if (x.length === 0) { return true; } if (x.length > 0) { return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1); } return false;}
const _stepCatArray = { '@@transducer/init': Array, '@@transducer/step': (xs, x) => { xs.push(x); return xs; }, '@@transducer/result': identity,};const _stepCatString = { '@@transducer/init': String, '@@transducer/step': (a, b) => a + b, '@@transducer/result': identity,};const _stepCatObject = { '@@transducer/init': Object, '@@transducer/step': (result, input) => Object.assign( result, _isArrayLike(input) ? { [input[0]]: input[1] } : input, ), '@@transducer/result': identity,};
function _stepCat(obj) { if (_isTransformer(obj)) { return obj; } if (_isArrayLike(obj)) { return _stepCatArray; } if (typeof obj === 'string') { return _stepCatString; } if (typeof obj === 'object') { return _stepCatObject; } throw new Error(`Cannot create transformer for ${obj}`);}
export function intoFn(acc, transducer, list) { const xf = transducer(_isTransformer(acc) ? acc : _stepCat(acc)); return _xReduce(xf, xf['@@transducer/init'](), list);}
export const into = curry(intoFn);
rambda

Version Info

Tagged at
2 months ago