-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpmap.js
279 lines (247 loc) · 7.44 KB
/
fpmap.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* =========================================
Copyright 2017 Panu Viljamaa
SPDX-License-Identifier: Apache-2.0
------------------------------------
FILE: fpmap/fpmap.js
USAGE:
a) require ("fpmap") ()
- Installs the funciton funkProtoMap()
defined below as the method "map" of
Function.prototype.
b) require ("fpmap") ("map9")
- Installs the same thing but as method "map9"
See the associated tests-file fpmap_test.js
for use-examples.
========================================= */
"use strict"
module.exports = fpm_installer
function fpm_installer (methodName)
{ methodName = methodName ? methodName : 'map';
var fp = Function.prototype;
if (fp[methodName] === funkProtoMap)
{ return;
}
if (fp[methodName])
{ var em = "Can not install the method '"
+ methodName
+ "' to Function.prototype "
+ "because that already exists "
+ "and has the value: "
+ fp[methodName];
throw new Error(em);
}
Object.defineProperty
(fp, methodName,
{ value : funkProtoMap,
writable : false,
enumerable : false,
configurable: false
});
ok (fp [methodName] === funkProtoMap);
ok (fp [methodName] === funkProtoMap);
return;
}
function funkProtoMap (objectOrArrayOrFunctionOrString, thisArg)
{ var resultA, resultB;
var arg = objectOrArrayOrFunctionOrString;
if ( (arg === undefined)
|| (arg === null)
)
{ throw "funkProtoMap() called with no argument or null";
}
var Ctor = arg.constructor;
var type = typeof(arg);
if (Ctor === String)
{ return funkProtoMap_string.call (this, arg, thisArg);
}
if (Ctor === Array)
{ return funkProtoMap_array.call (this, arg, thisArg);
}
if (Ctor === Function)
{ return funkProtoMap_function.call (this, arg, thisArg);
}
if (Ctor === Number)
{ return funkProtoMap_number.call (this, arg, thisArg);
}
if (Ctor === RegExp)
{ return funkProtoMap_reg.call (this, arg, thisArg);
}
if (type === "object")
{ return funkProtoMap_object.call (this, arg, thisArg);
}
throw "funkProtoMap() called with invalid argument";
function funkProtoMap_reg ($regExp, $thisArg)
{ var $transformer = this;
var $flags = $regExp.flags;
if (! $flags.match(/g/))
{ $flags += 'g';
}
if ($thisArg)
{ return ALL_MATCHES.bind($thisArg);
}
return ALL_MATCHES;
function ALL_MATCHES (inString)
{ var results = [];
var $resultString = "";
var transformer = $transformer;
var reg = new RegExp ($regExp, $flags);
var lastProcessedIndex = -1;
var ix = 0;
while (true)
{ ix++;
var m = reg.exec(inString);
if (! m)
{ break;
}
var startOfMatch = m.index;
var startOfCopy = lastProcessedIndex + 1;
var skipped = inString.slice (startOfCopy, startOfMatch);
$resultString += skipped;
lastProcessedIndex = startOfMatch + m[0].length - 1;
var aResult
= transformer.call (this, m, results, inString);
if (aResult === undefined)
{ $resultString += (m[0] + "");
continue;
}
$resultString += aResult + "";
m._replacement = aResult;
m.toString = replacementStringF;
results.push(m);
}
var rest = inString.slice
( lastProcessedIndex + 1
, inString.length
);
$resultString += rest;
results.toString = stringResultFunk;
return results;
function stringResultFunk ()
{ return $resultString;
}
function replacementStringF ()
{ return this._replacement + "";
}
}
}
function funkProtoMap_string (aString, thisArg)
{ aString = (aString + "").trim();
var produced = "";
var consumable = aString;
var nextIndex = 0;
while (true)
{ var stringy
= this.call
(thisArg, consumable, produced, aString);
if (stringy === undefined)
{ return undefined;
}
produced += stringy + "" ;
nextIndex += stringy.length;
if (nextIndex >= aString.length)
{ consumable = "";
var lastResult = this.call
(thisArg, consumable, produced, aString);
if (lastResult === undefined)
{ return undefined;
}
return produced + lastResult;
}
consumable = aString.slice (nextIndex);
}
}
function funkProtoMap_object (anObject, thisArg)
{ var result = new anObject.constructor();
for (var p in anObject)
{ var v = this.call (thisArg, anObject[p], p, anObject) ;
if (v !== undefined)
{ result [p] = v;
}
}
return result;
}
function funkProtoMap_array (anArray, thisArg)
{ resultA = [].map.call (anArray, this, thisArg);
resultB = [];
for (var j=0; j < resultA.length; j++)
{ var e = resultA[j];
if (e !== undefined)
{ resultB . push(e);
}
}
return resultB;
}
function funkProtoMap_function ($rightFunk, $arg2)
{ var $leftFunk = this;
if ( $rightFunk === Date)
{ return TIMER;
}
return FUNK_MAPPED;
function FUNK_MAPPED (vargs)
{ var args = [].slice.call(arguments);
var firstResult = $leftFunk.apply(this, args);
if (firstResult === undefined)
{ return undefined;
}
var isArray
= firstResult && (firstResult.constructor === Array);
if (! isArray)
{ firstResult = [firstResult];
}
var result
= $rightFunk.apply (this, firstResult);
return result;
}
function TIMER ( argsToApply )
{ if (argsToApply === undefined)
{ argsToApply = [];
}
if (argsToApply === null)
{ argsToApply = [];
}
if (argsToApply.constructor !== Array)
{ argsToApply = [argsToApply];
}
var times = $arg2;
times = times ? times : 1;
var start = (new Date()).getTime();
for (var j=0; j < times; j++)
{ $leftFunk.apply(this, argsToApply);
}
var end = (new Date()).getTime();
return end - start;
}
}
}
function funkProtoMap_number ($noOfLoops)
{ var $leftFunk = this;
return FUNK ;
function FUNK (firstArg, memory)
{ memory = memory ? memory : [];
if (firstArg === undefined)
{ firstArg = [];
}
var args = [].slice.call(arguments);
var leftFunk = $leftFunk;
var noOfLoops = $noOfLoops;
var nextArg = firstArg;
var mostRecentResult = undefined;
for (var j = noOfLoops; j > 0; j--)
{ var v = leftFunk.call
(this, nextArg, memory, firstArg);
if (v === undefined)
{ return mostRecentResult;
}
mostRecentResult = v;
nextArg = v;
}
return mostRecentResult;
}
}
function ok (bool, msg)
{ msg = msg ? msg : "ok() failed";
if(! bool)
{ throw msg;
}
return true;
}