///
///
///
///
import symbolObservable from 'symbol-observable';
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
type Primitive = null | undefined | string | number | boolean | Symbol;
export interface ArrayLike {
length: number;
}
export interface Class {
new(...args: any[]): T;
}
type DomElement = object & { nodeType: 1; nodeName: string };
type NodeStream = object & { pipe: Function };
export const enum TypeName {
null = 'null',
boolean = 'boolean',
undefined = 'undefined',
string = 'string',
number = 'number',
symbol = 'symbol',
Function = 'Function',
GeneratorFunction = 'GeneratorFunction',
AsyncFunction = 'AsyncFunction',
Observable = 'Observable',
Array = 'Array',
Buffer = 'Buffer',
Object = 'Object',
RegExp = 'RegExp',
Date = 'Date',
Error = 'Error',
Map = 'Map',
Set = 'Set',
WeakMap = 'WeakMap',
WeakSet = 'WeakSet',
Int8Array = 'Int8Array',
Uint8Array = 'Uint8Array',
Uint8ClampedArray = 'Uint8ClampedArray',
Int16Array = 'Int16Array',
Uint16Array = 'Uint16Array',
Int32Array = 'Int32Array',
Uint32Array = 'Uint32Array',
Float32Array = 'Float32Array',
Float64Array = 'Float64Array',
ArrayBuffer = 'ArrayBuffer',
SharedArrayBuffer = 'SharedArrayBuffer',
DataView = 'DataView',
Promise = 'Promise',
URL = 'URL'
}
const toString = Object.prototype.toString;
const isOfType = (type: string) => (value: any): value is T => typeof value === type;
const isBuffer = (input: any): input is Buffer => !is.nullOrUndefined(input) && !is.nullOrUndefined(input.constructor) && is.function_(input.constructor.isBuffer) && input.constructor.isBuffer(input);
const getObjectType = (value: any): TypeName | null => {
const objectName = toString.call(value).slice(8, -1) as string;
if (objectName) {
return objectName as TypeName;
}
return null;
};
const isObjectOfType = (type: TypeName) => (value: any): value is T => getObjectType(value) === type;
function is(value: any): TypeName { // tslint:disable-line:only-arrow-functions
switch (value) {
case null:
return TypeName.null;
case true:
case false:
return TypeName.boolean;
default:
}
switch (typeof value) {
case 'undefined':
return TypeName.undefined;
case 'string':
return TypeName.string;
case 'number':
return TypeName.number;
case 'symbol':
return TypeName.symbol;
default:
}
if (is.function_(value)) {
return TypeName.Function;
}
if (is.observable(value)) {
return TypeName.Observable;
}
if (Array.isArray(value)) {
return TypeName.Array;
}
if (isBuffer(value)) {
return TypeName.Buffer;
}
const tagType = getObjectType(value);
if (tagType) {
return tagType;
}
if (value instanceof String || value instanceof Boolean || value instanceof Number) {
throw new TypeError('Please don\'t use object wrappers for primitive types');
}
return TypeName.Object;
}
namespace is { // tslint:disable-line:no-namespace
const isObject = (value: any): value is object => typeof value === 'object';
// tslint:disable:variable-name
export const undefined = isOfType('undefined');
export const string = isOfType('string');
export const number = isOfType('number');
export const function_ = isOfType('function');
export const null_ = (value: any): value is null => value === null;
export const class_ = (value: any): value is Class => function_(value) && value.toString().startsWith('class ');
export const boolean = (value: any): value is boolean => value === true || value === false;
export const symbol = isOfType('symbol');
// tslint:enable:variable-name
export const array = Array.isArray;
export const buffer = isBuffer;
export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value);
export const object = (value: any): value is object => !nullOrUndefined(value) && (function_(value) || isObject(value));
export const iterable = (value: any): value is IterableIterator => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
export const asyncIterable = (value: any): value is AsyncIterableIterator => !nullOrUndefined(value) && function_(value[Symbol.asyncIterator]);
export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw);
export const nativePromise = (value: any): value is Promise =>
isObjectOfType>(TypeName.Promise)(value);
const hasPromiseAPI = (value: any): value is Promise =>
!null_(value) &&
isObject(value) as any &&
function_(value.then) &&
function_(value.catch);
export const promise = (value: any): value is Promise => nativePromise(value) || hasPromiseAPI(value);
export const generatorFunction = isObjectOfType(TypeName.GeneratorFunction);
export const asyncFunction = isObjectOfType(TypeName.AsyncFunction);
export const boundFunction = (value: any): value is Function => function_(value) && !value.hasOwnProperty('prototype');
export const regExp = isObjectOfType(TypeName.RegExp);
export const date = isObjectOfType(TypeName.Date);
export const error = isObjectOfType(TypeName.Error);
export const map = (value: any): value is Map => isObjectOfType