source: trip-planner-front/node_modules/is-what/dist/index.cjs.js@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.1 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5/**
6 * Returns the object type of the given payload
7 *
8 * @param {*} payload
9 * @returns {string}
10 */
11function getType(payload) {
12 return Object.prototype.toString.call(payload).slice(8, -1);
13}
14/**
15 * Returns whether the payload is undefined
16 *
17 * @param {*} payload
18 * @returns {payload is undefined}
19 */
20function isUndefined(payload) {
21 return getType(payload) === 'Undefined';
22}
23/**
24 * Returns whether the payload is null
25 *
26 * @param {*} payload
27 * @returns {payload is null}
28 */
29function isNull(payload) {
30 return getType(payload) === 'Null';
31}
32/**
33 * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
34 *
35 * @param {*} payload
36 * @returns {payload is PlainObject}
37 */
38function isPlainObject(payload) {
39 if (getType(payload) !== 'Object')
40 return false;
41 return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype;
42}
43/**
44 * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
45 *
46 * @param {*} payload
47 * @returns {payload is PlainObject}
48 */
49function isObject(payload) {
50 return isPlainObject(payload);
51}
52/**
53 * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
54 *
55 * @param {*} payload
56 * @returns {payload is { [K in any]: never }}
57 */
58function isEmptyObject(payload) {
59 return isPlainObject(payload) && Object.keys(payload).length === 0;
60}
61/**
62 * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
63 *
64 * @param {*} payload
65 * @returns {payload is PlainObject}
66 */
67function isFullObject(payload) {
68 return isPlainObject(payload) && Object.keys(payload).length > 0;
69}
70/**
71 * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
72 *
73 * @param {*} payload
74 * @returns {payload is PlainObject}
75 */
76function isAnyObject(payload) {
77 return getType(payload) === 'Object';
78}
79/**
80 * Returns whether the payload is an object like a type passed in < >
81 *
82 * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
83 *
84 * @template T this must be passed in < >
85 * @param {*} payload
86 * @returns {payload is T}
87 */
88function isObjectLike(payload) {
89 return isAnyObject(payload);
90}
91/**
92 * Returns whether the payload is a function (regular or async)
93 *
94 * @param {*} payload
95 * @returns {payload is AnyFunction}
96 */
97function isFunction(payload) {
98 return typeof payload === 'function';
99}
100/**
101 * Returns whether the payload is an array
102 *
103 * @param {any} payload
104 * @returns {payload is any[]}
105 */
106function isArray(payload) {
107 return getType(payload) === 'Array';
108}
109/**
110 * Returns whether the payload is a an array with at least 1 item
111 *
112 * @param {*} payload
113 * @returns {payload is any[]}
114 */
115function isFullArray(payload) {
116 return isArray(payload) && payload.length > 0;
117}
118/**
119 * Returns whether the payload is a an empty array
120 *
121 * @param {*} payload
122 * @returns {payload is []}
123 */
124function isEmptyArray(payload) {
125 return isArray(payload) && payload.length === 0;
126}
127/**
128 * Returns whether the payload is a string
129 *
130 * @param {*} payload
131 * @returns {payload is string}
132 */
133function isString(payload) {
134 return getType(payload) === 'String';
135}
136/**
137 * Returns whether the payload is a string, BUT returns false for ''
138 *
139 * @param {*} payload
140 * @returns {payload is string}
141 */
142function isFullString(payload) {
143 return isString(payload) && payload !== '';
144}
145/**
146 * Returns whether the payload is ''
147 *
148 * @param {*} payload
149 * @returns {payload is string}
150 */
151function isEmptyString(payload) {
152 return payload === '';
153}
154/**
155 * Returns whether the payload is a number (but not NaN)
156 *
157 * This will return `false` for `NaN`!!
158 *
159 * @param {*} payload
160 * @returns {payload is number}
161 */
162function isNumber(payload) {
163 return getType(payload) === 'Number' && !isNaN(payload);
164}
165/**
166 * Returns whether the payload is a boolean
167 *
168 * @param {*} payload
169 * @returns {payload is boolean}
170 */
171function isBoolean(payload) {
172 return getType(payload) === 'Boolean';
173}
174/**
175 * Returns whether the payload is a regular expression (RegExp)
176 *
177 * @param {*} payload
178 * @returns {payload is RegExp}
179 */
180function isRegExp(payload) {
181 return getType(payload) === 'RegExp';
182}
183/**
184 * Returns whether the payload is a Map
185 *
186 * @param {*} payload
187 * @returns {payload is Map<any, any>}
188 */
189function isMap(payload) {
190 return getType(payload) === 'Map';
191}
192/**
193 * Returns whether the payload is a WeakMap
194 *
195 * @param {*} payload
196 * @returns {payload is WeakMap<any, any>}
197 */
198function isWeakMap(payload) {
199 return getType(payload) === 'WeakMap';
200}
201/**
202 * Returns whether the payload is a Set
203 *
204 * @param {*} payload
205 * @returns {payload is Set<any>}
206 */
207function isSet(payload) {
208 return getType(payload) === 'Set';
209}
210/**
211 * Returns whether the payload is a WeakSet
212 *
213 * @param {*} payload
214 * @returns {payload is WeakSet<any>}
215 */
216function isWeakSet(payload) {
217 return getType(payload) === 'WeakSet';
218}
219/**
220 * Returns whether the payload is a Symbol
221 *
222 * @param {*} payload
223 * @returns {payload is symbol}
224 */
225function isSymbol(payload) {
226 return getType(payload) === 'Symbol';
227}
228/**
229 * Returns whether the payload is a Date, and that the date is valid
230 *
231 * @param {*} payload
232 * @returns {payload is Date}
233 */
234function isDate(payload) {
235 return getType(payload) === 'Date' && !isNaN(payload);
236}
237/**
238 * Returns whether the payload is a Blob
239 *
240 * @param {*} payload
241 * @returns {payload is Blob}
242 */
243function isBlob(payload) {
244 return getType(payload) === 'Blob';
245}
246/**
247 * Returns whether the payload is a File
248 *
249 * @param {*} payload
250 * @returns {payload is File}
251 */
252function isFile(payload) {
253 return getType(payload) === 'File';
254}
255/**
256 * Returns whether the payload is a Promise
257 *
258 * @param {*} payload
259 * @returns {payload is Promise<any>}
260 */
261function isPromise(payload) {
262 return getType(payload) === 'Promise';
263}
264/**
265 * Returns whether the payload is an Error
266 *
267 * @param {*} payload
268 * @returns {payload is Error}
269 */
270function isError(payload) {
271 return getType(payload) === 'Error';
272}
273/**
274 * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
275 *
276 * @param {*} payload
277 * @returns {payload is typeof NaN}
278 */
279function isNaNValue(payload) {
280 return getType(payload) === 'Number' && isNaN(payload);
281}
282/**
283 * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
284 *
285 * @param {*} payload
286 * @returns {(payload is boolean | null | undefined | number | string | symbol)}
287 */
288function isPrimitive(payload) {
289 return (isBoolean(payload) ||
290 isNull(payload) ||
291 isUndefined(payload) ||
292 isNumber(payload) ||
293 isString(payload) ||
294 isSymbol(payload));
295}
296/**
297 * Returns true whether the payload is null or undefined
298 *
299 * @param {*} payload
300 * @returns {(payload is null | undefined)}
301 */
302var isNullOrUndefined = isOneOf(isNull, isUndefined);
303function isOneOf(a, b, c, d, e) {
304 return function (value) {
305 return a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
306 };
307}
308/**
309 * Does a generic check to check that the given payload is of a given type.
310 * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!);
311 * It will, however, differentiate between object and null
312 *
313 * @template T
314 * @param {*} payload
315 * @param {T} type
316 * @throws {TypeError} Will throw type error if type is an invalid type
317 * @returns {payload is T}
318 */
319function isType(payload, type) {
320 if (!(type instanceof Function)) {
321 throw new TypeError('Type must be a function');
322 }
323 if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
324 throw new TypeError('Type is not a class');
325 }
326 // Classes usually have names (as functions usually have names)
327 var name = type.name;
328 return getType(payload) === name || Boolean(payload && payload.constructor === type);
329}
330
331exports.getType = getType;
332exports.isAnyObject = isAnyObject;
333exports.isArray = isArray;
334exports.isBlob = isBlob;
335exports.isBoolean = isBoolean;
336exports.isDate = isDate;
337exports.isEmptyArray = isEmptyArray;
338exports.isEmptyObject = isEmptyObject;
339exports.isEmptyString = isEmptyString;
340exports.isError = isError;
341exports.isFile = isFile;
342exports.isFullArray = isFullArray;
343exports.isFullObject = isFullObject;
344exports.isFullString = isFullString;
345exports.isFunction = isFunction;
346exports.isMap = isMap;
347exports.isNaNValue = isNaNValue;
348exports.isNull = isNull;
349exports.isNullOrUndefined = isNullOrUndefined;
350exports.isNumber = isNumber;
351exports.isObject = isObject;
352exports.isObjectLike = isObjectLike;
353exports.isOneOf = isOneOf;
354exports.isPlainObject = isPlainObject;
355exports.isPrimitive = isPrimitive;
356exports.isPromise = isPromise;
357exports.isRegExp = isRegExp;
358exports.isSet = isSet;
359exports.isString = isString;
360exports.isSymbol = isSymbol;
361exports.isType = isType;
362exports.isUndefined = isUndefined;
363exports.isWeakMap = isWeakMap;
364exports.isWeakSet = isWeakSet;
Note: See TracBrowser for help on using the repository browser.