-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopied.js
109 lines (95 loc) · 2.78 KB
/
copied.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
/* eslint strict: 0 */
'use strict';
module.exports = copied;
/**
* List of `stripped` versions for different lengths of arguments.
*
* @private
* @type {Function[]}
*/
var _stripped = [
require('./stripped.js') // eslint-disable-line global-require
];
/**
* List of `observable` versions for different lengths of arguments.
*
* @private
* @type {Function[]}
*/
var _observable = [
require('./observable.js') // eslint-disable-line global-require
];
/**
* Source code cache of the `stripped` function for faster creation of different lengths variations.
*
* @private
*/
_stripped.code = _stripped[0].toString()
// Clear function definition, we need only "content" of the function
.replace(/^function[^{]*{|}$/g, '')
// Prepare it for injecting arguments
.replace(/_f\s*\(\)/, '_f (...args)');
/**
* Source code cache of the `observable` function for faster creation of different lengths variations.
*
* @private
*/
_observable.code = _observable[0].toString()
// Clear function definition, we need only "content" of the function
.replace(/^function[^{]*{|}$/g, '')
// Prepare it for injecting arguments
.replace(/_f\s*\(\)/, '_f (...args)');
/**
* Creates a new variation of `nuonce` for specified length of arguments and stores it in cache.
*
* @private
* @param {Array} cache
* @param {number} length
* @return {Function}
*/
function _nuoncesPrepare (cache, length) {
var src = cache.code;
var args = new Array(length);
for (var i = length - 1; i > -1; i--) {
args[i] = 'a' + i;
}
var argv = args.join(', ');
/* eslint-disable no-new-func */
cache[length] = new Function('fn', 'cb', src.replace('...args', argv));
/* eslint-enable no-new-func */
return cache[length];
}
/**
* Returns a function that will call the `fn` function just once.
* Every next time the returned function is called, it will return value from the first call.
*
* The returned function has a copy of all enumerable properties and keeps `length`
* (number of declared arguments) of target function.
*
* @example
* const nuonce = require('nuonce/copied');
* let i = 0;
* const f = () => ++i;
* f.myProp = 'original';
* const once = nuonce(f);
* once() === once() || console.error('values differ');
* f.myProp = 'changed';
* once.myProp === 'original' || console.error('copied property differs from original');
*
* @function
* @memberof module:nuonce
* @param {Function} fn
* @param {Function} [cb] call back once with result, right after first call
* @return {Function}
*/
function copied (fn, cb) {
var l = fn.length;
var f = cb ? (_observable[l] || _nuoncesPrepare(_observable, l))(fn, cb) : (_stripped[l] || _nuoncesPrepare(_stripped, l))(fn);
var keys = Object.keys(fn);
var k;
for (var i = 0, imax = keys.length; i < imax; i++) {
k = keys[i];
f[k] = fn[k];
}
return f;
}