-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
123 lines (99 loc) · 3.15 KB
/
helpers.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
'use strict';
const { assert } = require('./utils.js');
function IsPropertyKey(argument) {
return typeof argument === 'string' || typeof argument === 'symbol';
}
exports.typeIsObject = x => (typeof x === 'object' && x !== null) || typeof x === 'function';
exports.createDataProperty = (o, p, v) => {
assert(exports.typeIsObject(o));
Object.defineProperty(o, p, { value: v, writable: true, enumerable: true, configurable: true });
};
exports.createArrayFromList = elements => {
// We use arrays to represent lists, so this is basically a no-op.
// Do a slice though just in case we happen to depend on the unique-ness.
return elements.slice();
};
exports.ArrayBufferCopy = (dest, destOffset, src, srcOffset, n) => {
new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
};
exports.CreateIterResultObject = (value, done) => {
assert(typeof done === 'boolean');
const obj = {};
Object.defineProperty(obj, 'value', { value, enumerable: true, writable: true, configurable: true });
Object.defineProperty(obj, 'done', { value: done, enumerable: true, writable: true, configurable: true });
return obj;
};
exports.IsFiniteNonNegativeNumber = v => {
if (Number.isNaN(v)) {
return false;
}
if (v === Infinity) {
return false;
}
if (v < 0) {
return false;
}
return true;
};
function Call(F, V, args) {
if (typeof F !== 'function') {
throw new TypeError('Argument is not a function');
}
return Function.prototype.apply.call(F, V, args);
}
exports.InvokeOrNoop = (O, P, args) => {
assert(O !== undefined);
assert(IsPropertyKey(P));
assert(Array.isArray(args));
const method = O[P];
if (method === undefined) {
return undefined;
}
return Call(method, O, args);
};
exports.PromiseInvokeOrNoop = (O, P, args) => {
assert(O !== undefined);
assert(IsPropertyKey(P));
assert(Array.isArray(args));
try {
return Promise.resolve(exports.InvokeOrNoop(O, P, args));
} catch (returnValueE) {
return Promise.reject(returnValueE);
}
};
exports.PromiseInvokeOrPerformFallback = (O, P, args, F, argsF) => {
assert(O !== undefined);
assert(IsPropertyKey(P));
assert(Array.isArray(args));
assert(Array.isArray(argsF));
let method;
try {
method = O[P];
} catch (methodE) {
return Promise.reject(methodE);
}
if (method === undefined) {
return F.apply(null, argsF);
}
try {
return Promise.resolve(Call(method, O, args));
} catch (e) {
return Promise.reject(e);
}
};
// Not implemented correctly
exports.TransferArrayBuffer = O => O.slice();
exports.ValidateAndNormalizeHighWaterMark = highWaterMark => {
highWaterMark = Number(highWaterMark);
if (Number.isNaN(highWaterMark) || highWaterMark < 0) {
throw new RangeError('highWaterMark property of a queuing strategy must be non-negative and non-NaN');
}
return highWaterMark;
};
exports.ValidateAndNormalizeQueuingStrategy = (size, highWaterMark) => {
if (size !== undefined && typeof size !== 'function') {
throw new TypeError('size property of a queuing strategy must be a function');
}
highWaterMark = exports.ValidateAndNormalizeHighWaterMark(highWaterMark);
return { size, highWaterMark };
};