forked from GrosSacASac/utilsac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
232 lines (209 loc) · 7.06 KB
/
utility.js
File metadata and controls
232 lines (209 loc) · 7.06 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
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
export {
createDebounced,
createThrottled,
throttledWithLast,
chainPromises,
chainRequestAnimationFrame,
doNTimes,
chainPromiseNTimes,
timeFunction,
timePromise,
memoizeAsStrings,
createTemplateTag,
bytesLengthFromString,
};
const waitTimeDefault = 150;
const createDebounced = function (functionToDebounce, waitTime = waitTimeDefault) {
/* creates a function that is de-bounced,
calling it, will eventually execute it, when you stop calling it
useful for scroll events, resize, search etc
the returned function always returns undefined */
let timeOutId = 0;
return function (...args) {
if (timeOutId !== 0) {
clearTimeout(timeOutId);
timeOutId = 0;
}
timeOutId = setTimeout(function () {
timeOutId = 0;
functionToDebounce(...args);
}, waitTime);
};
};
const minimumTimeSpaceDefault = 150;
const createThrottled = function (functionToThrottle, minimumTimeSpace = minimumTimeSpaceDefault) {
/* creates a function that is throttled,
calling it once will execute it immediately
calling it very often during a period less than minimumTimeSpace will only execute it once
the returned function always returns undefined */
let lastTime = Number.MIN_SAFE_INTEGER;
return function (...args) {
const now = Date.now();
if (minimumTimeSpace > now - lastTime) {
return;
}
lastTime = now;
functionToThrottle(...args);
};
};
const throttledWithLast = function (functionToThrottle, minimumTimeSpace = minimumTimeSpaceDefault, waitTime = waitTimeDefault) {
/* creates a function that is throttled,
calling it once will execute it immediately
calling it very often during a period less than minimumTimeSpace will only execute it twice:
the first and last call
The last call is always eventually executed
the returned function always returns undefined */
let timeOutId = 0;
let lastTime = Number.MIN_SAFE_INTEGER;
return function (...args) {
const now = Date.now();
const timeAlreadyWaited = now - lastTime;
if (timeOutId !== 0) {
clearTimeout(timeOutId);
timeOutId = 0;
}
if (minimumTimeSpace > timeAlreadyWaited) {
timeOutId = setTimeout(function () {
timeOutId = 0;
lastTime = now;
functionToThrottle(...args);
}, waitTime - timeAlreadyWaited);
return;
}
lastTime = now;
functionToThrottle(...args);
};
};
const doNTimes = function (task, times) {
for (let i = 0; i < times; i += 1) {
task();
}
};
const chainPromises = function (promiseCreators) {
/* different than Promise.all, takes an array of functions that return a promise
only executes promiseCreators sequentially
resolves with an array of values or reject with the first error*/
const length = promiseCreators.length;
const values = [];
let i = -1;
return new Promise(function (resolve, reject) {
const chainer = function (value) {
i += 1;
if (i > 0) {
values.push(value);
}
if (i < length) {
const promise = promiseCreators[i]();
promise.then(chainer);
promise.catch(reject);
} else {
resolve(values);
}
};
chainer();
});
};
const chainRequestAnimationFrame = function (functions) {
return new Promise(function (resolve, reject) {
const values = [];
const length = functions.length;
let i = 0;
const next = function () {
if (i < length) {
try {
values.push(functions[i]());
} catch (error) {
reject(error);
return;
}
i += 1;
requestAnimationFrame(next);
} else {
resolve(values);
}
};
next();
});
};
const chainPromiseNTimes = function (promiseCreator, times) {
/* different than Promise.all
only executes promiseCreator one after the previous has resolved
useful for testing
resolves with an array of values
could be made with chainPromises, but chose not to
to avoid an adapter array */
const values = [];
if (times === 0) {
return Promise.resolve(values);
}
return new Promise(function (resolve) {
let i = 0;
const chainer = function (value) {
i += 1;
values.push(value);
if (i < times) {
promiseCreator().then(chainer);
return;
}
resolve(values);
};
promiseCreator().then(chainer);
});
};
const timeFunction = function (callback, timer = Date) {
// executes callback and returns time elapsed in ms
const startTime = timer.now();
callback();
const endTime = timer.now();
return endTime - startTime;
};
const timePromise = function (promiseCreator, timer = Date) {
/* returns a Promise that resolves with
the time elapsed for the promise to resolve and its value
executes promiseCreator and waits for it to resolve */
const startTime = timer.now();
return promiseCreator().then(function (value) {
const endTime = timer.now();
return {
timeElapsed: endTime - startTime,
value,
};
});
};
const memoizeAsStrings = function (functionToMemoize, separator = `-`) {
/* joins together the args as strings to
decide if arguments are the same
fast memoizer
but infinitely growing */
const previousResults = {};
return function (...args) {
const argumentsAsStrings = args.map(String).join(separator);
/*
without .map(String) works but undefined and null become empty strings
const argumentsAsStrings = args.join(separator);
*/
if (!Object.prototype.hasOwnProperty.call(previousResults, argumentsAsStrings)) {
// not yet in cache
previousResults[argumentsAsStrings] = functionToMemoize(...args);
}
return previousResults[argumentsAsStrings];
};
};
const createTemplateTag = (mapper) => {
/* creates a template tag function
that will map the provided function on all runtime values
before constructing the string
example:
const createURLString = createTemplateTag(encodeURIComponent)
createURLString`https://example.com/id/${`slashes and spaces are properly escaped ///`}`;
// -> "https://example.com/id/slashes%20and%20spaces%20are%20properly%20escaped%20%2F%2F%2F" */
return (staticStrings, ...parts) => {
return Array.from(parts, (part, index) => {
return `${staticStrings[index]}${mapper(part)}`;
}).concat(staticStrings[staticStrings.length - 1]).join(``);
};
};
const bytesLengthFromString = string => {
const textEncoder = new TextEncoder();
return textEncoder.encode(string).length;
};