-
Notifications
You must be signed in to change notification settings - Fork 161
/
helpers.js
158 lines (128 loc) · 3.84 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
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
'use strict';
const assert = require('better-assert');
const isFakeDetached = Symbol('is "detached" for our purposes');
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.IsFiniteNonNegativeNumber = v => {
if (exports.IsNonNegativeNumber(v) === false) {
return false;
}
if (v === Infinity) {
return false;
}
return true;
};
exports.IsNonNegativeNumber = v => {
if (typeof v !== 'number') {
return false;
}
if (Number.isNaN(v)) {
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.Call = Call;
exports.CreateAlgorithmFromUnderlyingMethod = (underlyingObject, methodName, algoArgCount, extraArgs) => {
assert(underlyingObject !== undefined);
assert(IsPropertyKey(methodName));
assert(algoArgCount === 0 || algoArgCount === 1);
assert(Array.isArray(extraArgs));
const method = underlyingObject[methodName];
if (method !== undefined) {
if (typeof method !== 'function') {
throw new TypeError(`${method} is not a method`);
}
switch (algoArgCount) {
case 0: {
return () => {
return PromiseCall(method, underlyingObject, extraArgs);
};
}
case 1: {
return arg => {
const fullArgs = [arg].concat(extraArgs);
return PromiseCall(method, underlyingObject, fullArgs);
};
}
}
}
return () => Promise.resolve();
};
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);
};
function PromiseCall(F, V, args) {
assert(typeof F === 'function');
assert(V !== undefined);
assert(Array.isArray(args));
try {
return Promise.resolve(Call(F, V, args));
} catch (value) {
return Promise.reject(value);
}
}
exports.PromiseCall = PromiseCall;
// Not implemented correctly
exports.TransferArrayBuffer = O => {
assert(!exports.IsDetachedBuffer(O));
const transferredIshVersion = O.slice();
// This is specifically to fool tests that test "is transferred" by taking a non-zero-length
// ArrayBuffer and checking if its byteLength starts returning 0.
Object.defineProperty(O, 'byteLength', {
get() {
return 0;
}
});
O[isFakeDetached] = true;
return transferredIshVersion;
};
// Not implemented correctly
exports.IsDetachedBuffer = O => {
return isFakeDetached in O;
};
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.MakeSizeAlgorithmFromSizeFunction = size => {
if (size === undefined) {
return () => 1;
}
if (typeof size !== 'function') {
throw new TypeError('size property of a queuing strategy must be a function');
}
return chunk => size(chunk);
};