UNPKG

5.22 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.isUndefinedOrNull = exports.isDefined = exports.unreachable = exports.nullToUndefined = exports.isStringArray = exports.isArray = exports.isUndefined = exports.isObject = exports.isEmptyObject = exports.isFunction = exports.isErrorLike = exports.isError = exports.isNumber = exports.isString = exports.isBoolean = exports.Prioritizeable = exports.ArrayUtils = void 0;
19var array_utils_1 = require("./array-utils");
20Object.defineProperty(exports, "ArrayUtils", { enumerable: true, get: function () { return array_utils_1.ArrayUtils; } });
21var prioritizeable_1 = require("./prioritizeable");
22Object.defineProperty(exports, "Prioritizeable", { enumerable: true, get: function () { return prioritizeable_1.Prioritizeable; } });
23function isBoolean(value) {
24 return value === true || value === false;
25}
26exports.isBoolean = isBoolean;
27function isString(value) {
28 return typeof value === 'string' || value instanceof String;
29}
30exports.isString = isString;
31function isNumber(value) {
32 return typeof value === 'number' || value instanceof Number;
33}
34exports.isNumber = isNumber;
35function isError(value) {
36 return value instanceof Error;
37}
38exports.isError = isError;
39function isErrorLike(value) {
40 return isObject(value) && isString(value.name) && isString(value.message) && (isUndefined(value.stack) || isString(value.stack));
41}
42exports.isErrorLike = isErrorLike;
43// eslint-disable-next-line space-before-function-paren
44function isFunction(value) {
45 return typeof value === 'function';
46}
47exports.isFunction = isFunction;
48/**
49 * @returns whether the provided parameter is an empty JavaScript Object or not.
50 */
51function isEmptyObject(obj) {
52 if (!isObject(obj)) {
53 return false;
54 }
55 for (const key in obj) {
56 if (Object.prototype.hasOwnProperty.call(obj, key)) {
57 return false;
58 }
59 }
60 return true;
61}
62exports.isEmptyObject = isEmptyObject;
63function isObject(value) {
64 // eslint-disable-next-line no-null/no-null
65 return typeof value === 'object' && value !== null;
66}
67exports.isObject = isObject;
68function isUndefined(value) {
69 return typeof value === 'undefined';
70}
71exports.isUndefined = isUndefined;
72/**
73 * @param value value to check.
74 * @param every optional predicate ran on every element of the array.
75 * @param thisArg value to substitute `this` with when invoking in the predicate.
76 * @returns whether or not `value` is an array.
77 */
78function isArray(value, every, thisArg) {
79 return Array.isArray(value) && (!isFunction(every) || value.every(every, thisArg));
80}
81exports.isArray = isArray;
82function isStringArray(value) {
83 return isArray(value, isString);
84}
85exports.isStringArray = isStringArray;
86/**
87 * Creates a shallow copy with all ownkeys of the original object that are `null` made `undefined`
88 */
89function nullToUndefined(nullable) {
90 const undefinable = { ...nullable };
91 for (const key in nullable) {
92 // eslint-disable-next-line no-null/no-null
93 if (nullable[key] === null && Object.prototype.hasOwnProperty.call(nullable, key)) {
94 undefinable[key] = undefined;
95 }
96 }
97 return undefinable;
98}
99exports.nullToUndefined = nullToUndefined;
100/**
101 * Throws when called and statically makes sure that all variants of a type were consumed.
102 */
103function unreachable(_never, message = 'unhandled case') {
104 throw new Error(message);
105}
106exports.unreachable = unreachable;
107/*---------------------------------------------------------------------------------------------
108 * Copyright (c) Microsoft Corporation and others. All rights reserved.
109 * Licensed under the MIT License. See https://github.com/Microsoft/vscode/blob/master/LICENSE.txt for license information.
110 *--------------------------------------------------------------------------------------------*/
111// Copied from https://github.com/microsoft/vscode/blob/1.72.2/src/vs/base/common/types.ts
112/**
113 * @returns whether the provided parameter is defined.
114 */
115function isDefined(arg) {
116 return !isUndefinedOrNull(arg);
117}
118exports.isDefined = isDefined;
119/**
120 * @returns whether the provided parameter is undefined or null.
121 */
122function isUndefinedOrNull(obj) {
123 // eslint-disable-next-line no-null/no-null
124 return (isUndefined(obj) || obj === null);
125}
126exports.isUndefinedOrNull = isUndefinedOrNull;
127//# sourceMappingURL=types.js.map
\No newline at end of file