2020-03-11 10:49:53 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-03-30 13:45:37 -04:00
|
|
|
export type TypedArray =
|
|
|
|
| Int8Array
|
|
|
|
| Uint8Array
|
|
|
|
| Uint8ClampedArray
|
|
|
|
| Int16Array
|
|
|
|
| Uint16Array
|
|
|
|
| Int32Array
|
|
|
|
| Uint32Array
|
|
|
|
| Float32Array
|
|
|
|
| Float64Array;
|
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
// @internal
|
2020-03-11 10:49:53 -04:00
|
|
|
export function isTypedArray(x: unknown): x is TypedArray {
|
2020-03-30 13:45:37 -04:00
|
|
|
return (
|
|
|
|
x instanceof Int8Array ||
|
|
|
|
x instanceof Uint8Array ||
|
|
|
|
x instanceof Uint8ClampedArray ||
|
|
|
|
x instanceof Int16Array ||
|
|
|
|
x instanceof Uint16Array ||
|
|
|
|
x instanceof Int32Array ||
|
|
|
|
x instanceof Uint32Array ||
|
|
|
|
x instanceof Float32Array ||
|
|
|
|
x instanceof Float64Array
|
|
|
|
);
|
2020-03-11 10:49:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function requiredArguments(
|
|
|
|
name: string,
|
|
|
|
length: number,
|
|
|
|
required: number
|
|
|
|
): void {
|
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${name} requires at least ${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
|
|
|
}, but only ${length} present`;
|
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function immutableDefine(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
o: any,
|
|
|
|
p: string | number | symbol,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
value: any
|
|
|
|
): void {
|
|
|
|
Object.defineProperty(o, p, {
|
|
|
|
value,
|
|
|
|
configurable: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
writable: false,
|
2020-03-11 10:49:53 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
// @internal
|
|
|
|
export function hasOwnProperty(obj: unknown, v: PropertyKey): boolean {
|
2020-03-11 10:49:53 -04:00
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, v);
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
|
|
|
|
/** Returns whether o is iterable.
|
|
|
|
*
|
|
|
|
* @internal */
|
|
|
|
export function isIterable<T, P extends keyof T, K extends T[P]>(
|
|
|
|
o: T
|
|
|
|
): o is T & Iterable<[P, K]> {
|
|
|
|
// checks for null and undefined
|
|
|
|
if (o == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
typeof ((o as unknown) as Iterable<[P, K]>)[Symbol.iterator] === "function"
|
|
|
|
);
|
|
|
|
}
|
2020-04-11 11:42:02 -04:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
interface GenericConstructor<T = any> {
|
|
|
|
prototype: T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A helper function which ensures accessors are enumerable, as they normally
|
|
|
|
* are not. */
|
|
|
|
export function defineEnumerableProps(
|
|
|
|
Ctor: GenericConstructor,
|
|
|
|
props: string[]
|
|
|
|
): void {
|
|
|
|
for (const prop of props) {
|
|
|
|
Reflect.defineProperty(Ctor.prototype, prop, { enumerable: true });
|
|
|
|
}
|
|
|
|
}
|