monaco-editor
Version:
A browser based code editor
158 lines • 5.15 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { assert } from './assert.js';
/**
* @returns whether the provided parameter is a JavaScript String or not.
*/
export function isString(str) {
return (typeof str === 'string');
}
/**
* @returns whether the provided parameter is of type `object` but **not**
* `null`, an `array`, a `regexp`, nor a `date`.
*/
export function isObject(obj) {
// The method can't do a type cast since there are type (like strings) which
// are subclasses of any put not positvely matched by the function. Hence type
// narrowing results in wrong results.
return typeof obj === 'object'
&& obj !== null
&& !Array.isArray(obj)
&& !(obj instanceof RegExp)
&& !(obj instanceof Date);
}
/**
* @returns whether the provided parameter is of type `Buffer` or Uint8Array dervived type
*/
export function isTypedArray(obj) {
const TypedArray = Object.getPrototypeOf(Uint8Array);
return typeof obj === 'object'
&& obj instanceof TypedArray;
}
/**
* In **contrast** to just checking `typeof` this will return `false` for `NaN`.
* @returns whether the provided parameter is a JavaScript Number or not.
*/
export function isNumber(obj) {
return (typeof obj === 'number' && !isNaN(obj));
}
/**
* @returns whether the provided parameter is an Iterable, casting to the given generic
*/
export function isIterable(obj) {
return !!obj && typeof obj[Symbol.iterator] === 'function';
}
/**
* @returns whether the provided parameter is a JavaScript Boolean or not.
*/
export function isBoolean(obj) {
return (obj === true || obj === false);
}
/**
* @returns whether the provided parameter is undefined.
*/
export function isUndefined(obj) {
return (typeof obj === 'undefined');
}
/**
* @returns whether the provided parameter is defined.
*/
export function isDefined(arg) {
return !isUndefinedOrNull(arg);
}
/**
* @returns whether the provided parameter is undefined or null.
*/
export function isUndefinedOrNull(obj) {
return (isUndefined(obj) || obj === null);
}
export function assertType(condition, type) {
if (!condition) {
throw new Error(type ? `Unexpected type, expected '${type}'` : 'Unexpected type');
}
}
/**
* Asserts that the argument passed in is neither undefined nor null.
*
* @see {@link assertDefined} for a similar utility that leverages TS assertion functions to narrow down the type of `arg` to be non-nullable.
*/
export function assertReturnsDefined(arg) {
assert(arg !== null && arg !== undefined, 'Argument is `undefined` or `null`.');
return arg;
}
/**
* Checks if the provided value is one of the vales in the provided list.
*
* ## Examples
*
* ```typescript
* // note! item type is a `subset of string`
* type TItem = ':' | '.' | '/';
*
* // note! item is type of `string` here
* const item: string = ':';
* // list of the items to check against
* const list: TItem[] = [':', '.'];
*
* // ok
* assert(
* isOneOf(item, list),
* 'Must succeed.',
* );
*
* // `item` is of `TItem` type now
* ```
*/
export const isOneOf = (value, validValues) => {
// note! it is OK to type cast here, because we rely on the includes
// utility to check if the value is present in the provided list
return validValues.includes(value);
};
/**
* @returns whether the provided parameter is a JavaScript Function or not.
*/
export function isFunction(obj) {
return (typeof obj === 'function');
}
export function validateConstraints(args, constraints) {
const len = Math.min(args.length, constraints.length);
for (let i = 0; i < len; i++) {
validateConstraint(args[i], constraints[i]);
}
}
export function validateConstraint(arg, constraint) {
if (isString(constraint)) {
if (typeof arg !== constraint) {
throw new Error(`argument does not match constraint: typeof ${constraint}`);
}
}
else if (isFunction(constraint)) {
try {
if (arg instanceof constraint) {
return;
}
}
catch {
// ignore
}
if (!isUndefinedOrNull(arg) && arg.constructor === constraint) {
return;
}
if (constraint.length === 1 && constraint.call(undefined, arg) === true) {
return;
}
throw new Error(`argument does not match one of these constraints: arg instanceof constraint, arg.constructor === constraint, nor constraint(arg) === true`);
}
}
/**
* Helper type assertion that safely upcasts a type to a supertype.
*
* This can be used to make sure the argument correctly conforms to the subtype while still being able to pass it
* to contexts that expects the supertype.
*/
export function upcast(x) {
return x;
}
//# sourceMappingURL=types.js.map