UNPKG

9.64 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const util_1 = __importDefault(require("util"));
7const symbol_observable_1 = __importDefault(require("symbol-observable"));
8const toString = Object.prototype.toString;
9const isOfType = (type) => (value) => typeof value === type;
10const getObjectType = (value) => {
11 const objectName = toString.call(value).slice(8, -1);
12 if (objectName) {
13 return objectName;
14 }
15 return null;
16};
17const isObjectOfType = (type) => (value) => getObjectType(value) === type;
18function is(value) {
19 switch (value) {
20 case null:
21 return "null" /* null */;
22 case true:
23 case false:
24 return "boolean" /* boolean */;
25 default:
26 }
27 switch (typeof value) {
28 case 'undefined':
29 return "undefined" /* undefined */;
30 case 'string':
31 return "string" /* string */;
32 case 'number':
33 return "number" /* number */;
34 case 'symbol':
35 return "symbol" /* symbol */;
36 default:
37 }
38 if (is.function_(value)) {
39 return "Function" /* Function */;
40 }
41 if (is.observable(value)) {
42 return "Observable" /* Observable */;
43 }
44 if (Array.isArray(value)) {
45 return "Array" /* Array */;
46 }
47 if (Buffer.isBuffer(value)) {
48 return "Buffer" /* Buffer */;
49 }
50 const tagType = getObjectType(value);
51 if (tagType) {
52 return tagType;
53 }
54 if (value instanceof String || value instanceof Boolean || value instanceof Number) {
55 throw new TypeError('Please don\'t use object wrappers for primitive types');
56 }
57 return "Object" /* Object */;
58}
59(function (is) {
60 const isObject = (value) => typeof value === 'object';
61 // tslint:disable:variable-name
62 is.undefined = isOfType('undefined');
63 is.string = isOfType('string');
64 is.number = isOfType('number');
65 is.function_ = isOfType('function');
66 is.null_ = (value) => value === null;
67 is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
68 is.boolean = (value) => value === true || value === false;
69 is.symbol = isOfType('symbol');
70 // tslint:enable:variable-name
71 is.array = Array.isArray;
72 is.buffer = Buffer.isBuffer;
73 is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
74 is.object = (value) => !is.nullOrUndefined(value) && (is.function_(value) || isObject(value));
75 is.iterable = (value) => !is.nullOrUndefined(value) && is.function_(value[Symbol.iterator]);
76 is.asyncIterable = (value) => !is.nullOrUndefined(value) && is.function_(value[Symbol.asyncIterator]);
77 is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
78 is.nativePromise = (value) => isObjectOfType("Promise" /* Promise */)(value);
79 const hasPromiseAPI = (value) => !is.null_(value) &&
80 isObject(value) &&
81 is.function_(value.then) &&
82 is.function_(value.catch);
83 is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
84 is.generatorFunction = isObjectOfType("GeneratorFunction" /* GeneratorFunction */);
85 is.asyncFunction = isObjectOfType("AsyncFunction" /* AsyncFunction */);
86 is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype');
87 is.regExp = isObjectOfType("RegExp" /* RegExp */);
88 is.date = isObjectOfType("Date" /* Date */);
89 is.error = isObjectOfType("Error" /* Error */);
90 is.map = (value) => isObjectOfType("Map" /* Map */)(value);
91 is.set = (value) => isObjectOfType("Set" /* Set */)(value);
92 is.weakMap = (value) => isObjectOfType("WeakMap" /* WeakMap */)(value);
93 is.weakSet = (value) => isObjectOfType("WeakSet" /* WeakSet */)(value);
94 is.int8Array = isObjectOfType("Int8Array" /* Int8Array */);
95 is.uint8Array = isObjectOfType("Uint8Array" /* Uint8Array */);
96 is.uint8ClampedArray = isObjectOfType("Uint8ClampedArray" /* Uint8ClampedArray */);
97 is.int16Array = isObjectOfType("Int16Array" /* Int16Array */);
98 is.uint16Array = isObjectOfType("Uint16Array" /* Uint16Array */);
99 is.int32Array = isObjectOfType("Int32Array" /* Int32Array */);
100 is.uint32Array = isObjectOfType("Uint32Array" /* Uint32Array */);
101 is.float32Array = isObjectOfType("Float32Array" /* Float32Array */);
102 is.float64Array = isObjectOfType("Float64Array" /* Float64Array */);
103 is.arrayBuffer = isObjectOfType("ArrayBuffer" /* ArrayBuffer */);
104 is.sharedArrayBuffer = isObjectOfType("SharedArrayBuffer" /* SharedArrayBuffer */);
105 is.dataView = isObjectOfType("DataView" /* DataView */);
106 is.directInstanceOf = (instance, klass) => Object.getPrototypeOf(instance) === klass.prototype;
107 is.urlInstance = (value) => isObjectOfType("URL" /* URL */)(value);
108 is.truthy = (value) => Boolean(value);
109 is.falsy = (value) => !value;
110 is.nan = (value) => Number.isNaN(value);
111 const primitiveTypes = new Set([
112 'undefined',
113 'string',
114 'number',
115 'boolean',
116 'symbol'
117 ]);
118 is.primitive = (value) => is.null_(value) || primitiveTypes.has(typeof value);
119 is.integer = (value) => Number.isInteger(value);
120 is.safeInteger = (value) => Number.isSafeInteger(value);
121 is.plainObject = (value) => {
122 // From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
123 let prototype;
124 return getObjectType(value) === "Object" /* Object */ &&
125 (prototype = Object.getPrototypeOf(value), prototype === null || // tslint:disable-line:ban-comma-operator
126 prototype === Object.getPrototypeOf({}));
127 };
128 const typedArrayTypes = new Set([
129 "Int8Array" /* Int8Array */,
130 "Uint8Array" /* Uint8Array */,
131 "Uint8ClampedArray" /* Uint8ClampedArray */,
132 "Int16Array" /* Int16Array */,
133 "Uint16Array" /* Uint16Array */,
134 "Int32Array" /* Int32Array */,
135 "Uint32Array" /* Uint32Array */,
136 "Float32Array" /* Float32Array */,
137 "Float64Array" /* Float64Array */
138 ]);
139 is.typedArray = (value) => {
140 const objectType = getObjectType(value);
141 if (objectType === null) {
142 return false;
143 }
144 return typedArrayTypes.has(objectType);
145 };
146 const isValidLength = (value) => is.safeInteger(value) && value > -1;
147 is.arrayLike = (value) => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength(value.length);
148 is.inRange = (value, range) => {
149 if (is.number(range)) {
150 return value >= Math.min(0, range) && value <= Math.max(range, 0);
151 }
152 if (is.array(range) && range.length === 2) {
153 return value >= Math.min(...range) && value <= Math.max(...range);
154 }
155 throw new TypeError(`Invalid range: ${util_1.default.inspect(range)}`);
156 };
157 const NODE_TYPE_ELEMENT = 1;
158 const DOM_PROPERTIES_TO_CHECK = [
159 'innerHTML',
160 'ownerDocument',
161 'style',
162 'attributes',
163 'nodeValue'
164 ];
165 is.domElement = (value) => is.object(value) && value.nodeType === NODE_TYPE_ELEMENT && is.string(value.nodeName) &&
166 !is.plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
167 is.observable = (value) => Boolean(value && value[symbol_observable_1.default] && value === value[symbol_observable_1.default]());
168 is.nodeStream = (value) => !is.nullOrUndefined(value) && isObject(value) && is.function_(value.pipe) && !is.observable(value);
169 is.infinite = (value) => value === Infinity || value === -Infinity;
170 const isAbsoluteMod2 = (value) => (rem) => is.integer(rem) && Math.abs(rem % 2) === value;
171 is.even = isAbsoluteMod2(0);
172 is.odd = isAbsoluteMod2(1);
173 const isWhiteSpaceString = (value) => is.string(value) && /\S/.test(value) === false;
174 const isEmptyStringOrArray = (value) => (is.string(value) || is.array(value)) && value.length === 0;
175 const isEmptyObject = (value) => !is.map(value) && !is.set(value) && is.object(value) && Object.keys(value).length === 0;
176 const isEmptyMapOrSet = (value) => (is.map(value) || is.set(value)) && value.size === 0;
177 is.empty = (value) => is.falsy(value) || isEmptyStringOrArray(value) || isEmptyObject(value) || isEmptyMapOrSet(value);
178 is.emptyOrWhitespace = (value) => is.empty(value) || isWhiteSpaceString(value);
179 const predicateOnArray = (method, predicate, values) => {
180 if (is.function_(predicate) === false) {
181 throw new TypeError(`Invalid predicate: ${util_1.default.inspect(predicate)}`);
182 }
183 if (values.length === 0) {
184 throw new TypeError('Invalid number of values');
185 }
186 return method.call(values, predicate);
187 };
188 // tslint:disable variable-name
189 is.any = (predicate, ...values) => predicateOnArray(Array.prototype.some, predicate, values);
190 is.all = (predicate, ...values) => predicateOnArray(Array.prototype.every, predicate, values);
191 // tslint:enable variable-name
192})(is || (is = {}));
193// Some few keywords are reserved, but we'll populate them for Node.js users
194// See https://github.com/Microsoft/TypeScript/issues/2536
195Object.defineProperties(is, {
196 class: {
197 value: is.class_
198 },
199 function: {
200 value: is.function_
201 },
202 null: {
203 value: is.null_
204 }
205});
206exports.default = is;
207// For CommonJS default export support
208module.exports = is;
209module.exports.default = is;
210//# sourceMappingURL=index.js.map
\No newline at end of file