@pkerschbaum/code-oss-file-service
Version:
VS Code ([microsoft/vscode](https://github.com/microsoft/vscode)) includes a rich "`FileService`" and "`DiskFileSystemProvider`" abstraction built on top of Node.js core modules (`fs`, `path`) and Electron's `shell` module. This package allows to use that
249 lines • 8.66 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPromise = exports.assertNever = exports.NotImplementedProxy = exports.withUndefinedAsNull = exports.withNullAsUndefined = exports.createProxyObject = exports.getAllMethodNames = exports.getAllPropertyNames = exports.validateConstraint = exports.validateConstraints = exports.areFunctions = exports.isFunction = exports.isEmptyObject = exports.assertAllDefined = exports.assertIsDefined = exports.assertType = exports.isUndefinedOrNull = exports.isDefined = exports.isUndefined = exports.isBoolean = exports.isIterable = exports.isNumber = exports.isObject = exports.isStringArray = exports.isString = exports.isArray = void 0;
/**
* @returns whether the provided parameter is a JavaScript Array or not.
*/
function isArray(array) {
return Array.isArray(array);
}
exports.isArray = isArray;
/**
* @returns whether the provided parameter is a JavaScript String or not.
*/
function isString(str) {
return (typeof str === 'string');
}
exports.isString = isString;
/**
* @returns whether the provided parameter is a JavaScript Array and each element in the array is a string.
*/
function isStringArray(value) {
return Array.isArray(value) && value.every(elem => isString(elem));
}
exports.isStringArray = isStringArray;
/**
*
* @returns whether the provided parameter is of type `object` but **not**
* `null`, an `array`, a `regexp`, nor a `date`.
*/
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);
}
exports.isObject = isObject;
/**
* In **contrast** to just checking `typeof` this will return `false` for `NaN`.
* @returns whether the provided parameter is a JavaScript Number or not.
*/
function isNumber(obj) {
return (typeof obj === 'number' && !isNaN(obj));
}
exports.isNumber = isNumber;
/**
* @returns whether the provided parameter is an Iterable, casting to the given generic
*/
function isIterable(obj) {
return !!obj && typeof obj[Symbol.iterator] === 'function';
}
exports.isIterable = isIterable;
/**
* @returns whether the provided parameter is a JavaScript Boolean or not.
*/
function isBoolean(obj) {
return (obj === true || obj === false);
}
exports.isBoolean = isBoolean;
/**
* @returns whether the provided parameter is undefined.
*/
function isUndefined(obj) {
return (typeof obj === 'undefined');
}
exports.isUndefined = isUndefined;
/**
* @returns whether the provided parameter is defined.
*/
function isDefined(arg) {
return !isUndefinedOrNull(arg);
}
exports.isDefined = isDefined;
/**
* @returns whether the provided parameter is undefined or null.
*/
function isUndefinedOrNull(obj) {
return (isUndefined(obj) || obj === null);
}
exports.isUndefinedOrNull = isUndefinedOrNull;
function assertType(condition, type) {
if (!condition) {
throw new Error(type ? `Unexpected type, expected '${type}'` : 'Unexpected type');
}
}
exports.assertType = assertType;
/**
* Asserts that the argument passed in is neither undefined nor null.
*/
function assertIsDefined(arg) {
if (isUndefinedOrNull(arg)) {
throw new Error('Assertion Failed: argument is undefined or null');
}
return arg;
}
exports.assertIsDefined = assertIsDefined;
function assertAllDefined(...args) {
const result = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (isUndefinedOrNull(arg)) {
throw new Error(`Assertion Failed: argument at index ${i} is undefined or null`);
}
result.push(arg);
}
return result;
}
exports.assertAllDefined = assertAllDefined;
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* @returns whether the provided parameter is an empty JavaScript Object or not.
*/
function isEmptyObject(obj) {
if (!isObject(obj)) {
return false;
}
for (let key in obj) {
if (hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}
exports.isEmptyObject = isEmptyObject;
/**
* @returns whether the provided parameter is a JavaScript Function or not.
*/
function isFunction(obj) {
return (typeof obj === 'function');
}
exports.isFunction = isFunction;
/**
* @returns whether the provided parameters is are JavaScript Function or not.
*/
function areFunctions(...objects) {
return objects.length > 0 && objects.every(isFunction);
}
exports.areFunctions = areFunctions;
function validateConstraints(args, constraints) {
const len = Math.min(args.length, constraints.length);
for (let i = 0; i < len; i++) {
validateConstraint(args[i], constraints[i]);
}
}
exports.validateConstraints = validateConstraints;
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 (_a) {
// 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`);
}
}
exports.validateConstraint = validateConstraint;
function getAllPropertyNames(obj) {
let res = [];
let proto = Object.getPrototypeOf(obj);
while (Object.prototype !== proto) {
res = res.concat(Object.getOwnPropertyNames(proto));
proto = Object.getPrototypeOf(proto);
}
return res;
}
exports.getAllPropertyNames = getAllPropertyNames;
function getAllMethodNames(obj) {
const methods = [];
for (const prop of getAllPropertyNames(obj)) {
if (typeof obj[prop] === 'function') {
methods.push(prop);
}
}
return methods;
}
exports.getAllMethodNames = getAllMethodNames;
function createProxyObject(methodNames, invoke) {
const createProxyMethod = (method) => {
return function () {
const args = Array.prototype.slice.call(arguments, 0);
return invoke(method, args);
};
};
let result = {};
for (const methodName of methodNames) {
result[methodName] = createProxyMethod(methodName);
}
return result;
}
exports.createProxyObject = createProxyObject;
/**
* Converts null to undefined, passes all other values through.
*/
function withNullAsUndefined(x) {
return x === null ? undefined : x;
}
exports.withNullAsUndefined = withNullAsUndefined;
/**
* Converts undefined to null, passes all other values through.
*/
function withUndefinedAsNull(x) {
return typeof x === 'undefined' ? null : x;
}
exports.withUndefinedAsNull = withUndefinedAsNull;
function NotImplementedProxy(name) {
return class {
constructor() {
return new Proxy({}, {
get(target, prop) {
if (target[prop]) {
return target[prop];
}
throw new Error(`Not Implemented: ${name}->${String(prop)}`);
}
});
}
};
}
exports.NotImplementedProxy = NotImplementedProxy;
function assertNever(value, message = 'Unreachable') {
throw new Error(message);
}
exports.assertNever = assertNever;
function isPromise(obj) {
return !!obj && typeof obj.then === 'function' && typeof obj.catch === 'function';
}
exports.isPromise = isPromise;
//# sourceMappingURL=types.js.map
;