UNPKG

@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

659 lines 22.6 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayQueue = exports.findMinBy = exports.findLastMaxBy = exports.findMaxBy = exports.numberComparator = exports.compareBy = exports.splice = exports.insertInto = exports.mapFind = exports.getRandomElement = exports.asArray = exports.mapArrayOrNot = exports.pushToEnd = exports.pushToStart = exports.shuffle = exports.arrayInsert = exports.remove = exports.insert = exports.index = exports.range = exports.flatten = exports.commonPrefixLength = exports.firstOrDefault = exports.lastIndex = exports.findLast = exports.uniqueFilter = exports.distinct = exports.isNonEmptyArray = exports.isFalsyOrEmpty = exports.move = exports.coalesceInPlace = exports.coalesce = exports.topAsync = exports.top = exports.delta = exports.sortedDiff = exports.groupBy = exports.quickSelect = exports.findFirstInSorted = exports.binarySearch = exports.equals = exports.tail2 = exports.tail = void 0; const errors_1 = require("../../base/common/errors"); /** * Returns the last element of an array. * @param array The array. * @param n Which element from the end (default is zero). */ function tail(array, n = 0) { return array[array.length - (1 + n)]; } exports.tail = tail; function tail2(arr) { if (arr.length === 0) { throw new Error('Invalid tail call'); } return [arr.slice(0, arr.length - 1), arr[arr.length - 1]]; } exports.tail2 = tail2; function equals(one, other, itemEquals = (a, b) => a === b) { if (one === other) { return true; } if (!one || !other) { return false; } if (one.length !== other.length) { return false; } for (let i = 0, len = one.length; i < len; i++) { if (!itemEquals(one[i], other[i])) { return false; } } return true; } exports.equals = equals; function binarySearch(array, key, comparator) { let low = 0, high = array.length - 1; while (low <= high) { const mid = ((low + high) / 2) | 0; const comp = comparator(array[mid], key); if (comp < 0) { low = mid + 1; } else if (comp > 0) { high = mid - 1; } else { return mid; } } return -(low + 1); } exports.binarySearch = binarySearch; /** * Takes a sorted array and a function p. The array is sorted in such a way that all elements where p(x) is false * are located before all elements where p(x) is true. * @returns the least x for which p(x) is true or array.length if no element fullfills the given function. */ function findFirstInSorted(array, p) { let low = 0, high = array.length; if (high === 0) { return 0; // no children } while (low < high) { const mid = Math.floor((low + high) / 2); if (p(array[mid])) { high = mid; } else { low = mid + 1; } } return low; } exports.findFirstInSorted = findFirstInSorted; function quickSelect(nth, data, compare) { nth = nth | 0; if (nth >= data.length) { throw new TypeError('invalid index'); } let pivotValue = data[Math.floor(data.length * Math.random())]; let lower = []; let higher = []; let pivots = []; for (let value of data) { const val = compare(value, pivotValue); if (val < 0) { lower.push(value); } else if (val > 0) { higher.push(value); } else { pivots.push(value); } } if (nth < lower.length) { return quickSelect(nth, lower, compare); } else if (nth < lower.length + pivots.length) { return pivots[0]; } else { return quickSelect(nth - (lower.length + pivots.length), higher, compare); } } exports.quickSelect = quickSelect; function groupBy(data, compare) { const result = []; let currentGroup = undefined; for (const element of data.slice(0).sort(compare)) { if (!currentGroup || compare(currentGroup[0], element) !== 0) { currentGroup = [element]; result.push(currentGroup); } else { currentGroup.push(element); } } return result; } exports.groupBy = groupBy; /** * Diffs two *sorted* arrays and computes the splices which apply the diff. */ function sortedDiff(before, after, compare) { const result = []; function pushSplice(start, deleteCount, toInsert) { if (deleteCount === 0 && toInsert.length === 0) { return; } const latest = result[result.length - 1]; if (latest && latest.start + latest.deleteCount === start) { latest.deleteCount += deleteCount; latest.toInsert.push(...toInsert); } else { result.push({ start, deleteCount, toInsert }); } } let beforeIdx = 0; let afterIdx = 0; while (true) { if (beforeIdx === before.length) { pushSplice(beforeIdx, 0, after.slice(afterIdx)); break; } if (afterIdx === after.length) { pushSplice(beforeIdx, before.length - beforeIdx, []); break; } const beforeElement = before[beforeIdx]; const afterElement = after[afterIdx]; const n = compare(beforeElement, afterElement); if (n === 0) { // equal beforeIdx += 1; afterIdx += 1; } else if (n < 0) { // beforeElement is smaller -> before element removed pushSplice(beforeIdx, 1, []); beforeIdx += 1; } else if (n > 0) { // beforeElement is greater -> after element added pushSplice(beforeIdx, 0, [afterElement]); afterIdx += 1; } } return result; } exports.sortedDiff = sortedDiff; /** * Takes two *sorted* arrays and computes their delta (removed, added elements). * Finishes in `Math.min(before.length, after.length)` steps. */ function delta(before, after, compare) { const splices = sortedDiff(before, after, compare); const removed = []; const added = []; for (const splice of splices) { removed.push(...before.slice(splice.start, splice.start + splice.deleteCount)); added.push(...splice.toInsert); } return { removed, added }; } exports.delta = delta; /** * Returns the top N elements from the array. * * Faster than sorting the entire array when the array is a lot larger than N. * * @param array The unsorted array. * @param compare A sort function for the elements. * @param n The number of elements to return. * @return The first n elements from array when sorted with compare. */ function top(array, compare, n) { if (n === 0) { return []; } const result = array.slice(0, n).sort(compare); topStep(array, compare, result, n, array.length); return result; } exports.top = top; /** * Asynchronous variant of `top()` allowing for splitting up work in batches between which the event loop can run. * * Returns the top N elements from the array. * * Faster than sorting the entire array when the array is a lot larger than N. * * @param array The unsorted array. * @param compare A sort function for the elements. * @param n The number of elements to return. * @param batch The number of elements to examine before yielding to the event loop. * @return The first n elements from array when sorted with compare. */ function topAsync(array, compare, n, batch, token) { if (n === 0) { return Promise.resolve([]); } return new Promise((resolve, reject) => { (() => __awaiter(this, void 0, void 0, function* () { const o = array.length; const result = array.slice(0, n).sort(compare); for (let i = n, m = Math.min(n + batch, o); i < o; i = m, m = Math.min(m + batch, o)) { if (i > n) { yield new Promise(resolve => setTimeout(resolve)); // any other delay function would starve I/O } if (token && token.isCancellationRequested) { throw (0, errors_1.canceled)(); } topStep(array, compare, result, i, m); } return result; }))() .then(resolve, reject); }); } exports.topAsync = topAsync; function topStep(array, compare, result, i, m) { for (const n = result.length; i < m; i++) { const element = array[i]; if (compare(element, result[n - 1]) < 0) { result.pop(); const j = findFirstInSorted(result, e => compare(element, e) < 0); result.splice(j, 0, element); } } } /** * @returns New array with all falsy values removed. The original array IS NOT modified. */ function coalesce(array) { return array.filter(e => !!e); } exports.coalesce = coalesce; /** * Remove all falsy values from `array`. The original array IS modified. */ function coalesceInPlace(array) { let to = 0; for (let i = 0; i < array.length; i++) { if (!!array[i]) { array[to] = array[i]; to += 1; } } array.length = to; } exports.coalesceInPlace = coalesceInPlace; /** * @deprecated Use `Array.copyWithin` instead */ function move(array, from, to) { array.splice(to, 0, array.splice(from, 1)[0]); } exports.move = move; /** * @returns false if the provided object is an array and not empty. */ function isFalsyOrEmpty(obj) { return !Array.isArray(obj) || obj.length === 0; } exports.isFalsyOrEmpty = isFalsyOrEmpty; function isNonEmptyArray(obj) { return Array.isArray(obj) && obj.length > 0; } exports.isNonEmptyArray = isNonEmptyArray; /** * Removes duplicates from the given array. The optional keyFn allows to specify * how elements are checked for equality by returning an alternate value for each. */ function distinct(array, keyFn = value => value) { const seen = new Set(); return array.filter(element => { const key = keyFn(element); if (seen.has(key)) { return false; } seen.add(key); return true; }); } exports.distinct = distinct; function uniqueFilter(keyFn) { const seen = Object.create(null); return element => { const key = keyFn(element); if (seen[key]) { return false; } seen[key] = true; return true; }; } exports.uniqueFilter = uniqueFilter; function findLast(arr, predicate) { const idx = lastIndex(arr, predicate); if (idx === -1) { return undefined; } return arr[idx]; } exports.findLast = findLast; function lastIndex(array, fn) { for (let i = array.length - 1; i >= 0; i--) { const element = array[i]; if (fn(element)) { return i; } } return -1; } exports.lastIndex = lastIndex; function firstOrDefault(array, notFoundValue) { return array.length > 0 ? array[0] : notFoundValue; } exports.firstOrDefault = firstOrDefault; function commonPrefixLength(one, other, equals = (a, b) => a === b) { let result = 0; for (let i = 0, len = Math.min(one.length, other.length); i < len && equals(one[i], other[i]); i++) { result++; } return result; } exports.commonPrefixLength = commonPrefixLength; function flatten(arr) { return [].concat(...arr); } exports.flatten = flatten; function range(arg, to) { let from = typeof to === 'number' ? arg : 0; if (typeof to === 'number') { from = arg; } else { from = 0; to = arg; } const result = []; if (from <= to) { for (let i = from; i < to; i++) { result.push(i); } } else { for (let i = from; i > to; i--) { result.push(i); } } return result; } exports.range = range; function index(array, indexer, mapper) { return array.reduce((r, t) => { r[indexer(t)] = mapper ? mapper(t) : t; return r; }, Object.create(null)); } exports.index = index; /** * Inserts an element into an array. Returns a function which, when * called, will remove that element from the array. */ function insert(array, element) { array.push(element); return () => remove(array, element); } exports.insert = insert; /** * Removes an element from an array if it can be found. */ function remove(array, element) { const index = array.indexOf(element); if (index > -1) { array.splice(index, 1); return element; } return undefined; } exports.remove = remove; /** * Insert `insertArr` inside `target` at `insertIndex`. * Please don't touch unless you understand https://jsperf.com/inserting-an-array-within-an-array */ function arrayInsert(target, insertIndex, insertArr) { const before = target.slice(0, insertIndex); const after = target.slice(insertIndex); return before.concat(insertArr, after); } exports.arrayInsert = arrayInsert; /** * Uses Fisher-Yates shuffle to shuffle the given array */ function shuffle(array, _seed) { let rand; if (typeof _seed === 'number') { let seed = _seed; // Seeded random number generator in JS. Modified from: // https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript rand = () => { const x = Math.sin(seed++) * 179426549; // throw away most significant digits and reduce any potential bias return x - Math.floor(x); }; } else { rand = Math.random; } for (let i = array.length - 1; i > 0; i -= 1) { const j = Math.floor(rand() * (i + 1)); const temp = array[i]; array[i] = array[j]; array[j] = temp; } } exports.shuffle = shuffle; /** * Pushes an element to the start of the array, if found. */ function pushToStart(arr, value) { const index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); arr.unshift(value); } } exports.pushToStart = pushToStart; /** * Pushes an element to the end of the array, if found. */ function pushToEnd(arr, value) { const index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); arr.push(value); } } exports.pushToEnd = pushToEnd; function mapArrayOrNot(items, fn) { return Array.isArray(items) ? items.map(fn) : fn(items); } exports.mapArrayOrNot = mapArrayOrNot; function asArray(x) { return Array.isArray(x) ? x : [x]; } exports.asArray = asArray; function getRandomElement(arr) { return arr[Math.floor(Math.random() * arr.length)]; } exports.getRandomElement = getRandomElement; /** * Returns the first mapped value of the array which is not undefined. */ function mapFind(array, mapFn) { for (const value of array) { const mapped = mapFn(value); if (mapped !== undefined) { return mapped; } } return undefined; } exports.mapFind = mapFind; /** * Insert the new items in the array. * @param array The original array. * @param start The zero-based location in the array from which to start inserting elements. * @param newItems The items to be inserted */ function insertInto(array, start, newItems) { const startIdx = getActualStartIndex(array, start); const originalLength = array.length; const newItemsLength = newItems.length; array.length = originalLength + newItemsLength; // Move the items after the start index, start from the end so that we don't overwrite any value. for (let i = originalLength - 1; i >= startIdx; i--) { array[i + newItemsLength] = array[i]; } for (let i = 0; i < newItemsLength; i++) { array[i + startIdx] = newItems[i]; } } exports.insertInto = insertInto; /** * Removes elements from an array and inserts new elements in their place, returning the deleted elements. Alternative to the native Array.splice method, it * can only support limited number of items due to the maximum call stack size limit. * @param array The original array. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @returns An array containing the elements that were deleted. */ function splice(array, start, deleteCount, newItems) { const index = getActualStartIndex(array, start); const result = array.splice(index, deleteCount); insertInto(array, index, newItems); return result; } exports.splice = splice; /** * Determine the actual start index (same logic as the native splice() or slice()) * If greater than the length of the array, start will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many element as item[n*] provided. * If negative, it will begin that many elements from the end of the array. (In this case, the origin -1, meaning -n is the index of the nth last element, and is therefore equivalent to the index of array.length - n.) If array.length + start is less than 0, it will begin from index 0. * @param array The target array. * @param start The operation index. */ function getActualStartIndex(array, start) { return start < 0 ? Math.max(start + array.length, 0) : Math.min(start, array.length); } function compareBy(selector, comparator) { return (a, b) => comparator(selector(a), selector(b)); } exports.compareBy = compareBy; /** * The natural order on numbers. */ const numberComparator = (a, b) => a - b; exports.numberComparator = numberComparator; /** * Returns the first item that is equal to or greater than every other item. */ function findMaxBy(items, comparator) { if (items.length === 0) { return undefined; } let max = items[0]; for (let i = 1; i < items.length; i++) { const item = items[i]; if (comparator(item, max) > 0) { max = item; } } return max; } exports.findMaxBy = findMaxBy; /** * Returns the last item that is equal to or greater than every other item. */ function findLastMaxBy(items, comparator) { if (items.length === 0) { return undefined; } let max = items[0]; for (let i = 1; i < items.length; i++) { const item = items[i]; if (comparator(item, max) >= 0) { max = item; } } return max; } exports.findLastMaxBy = findLastMaxBy; /** * Returns the first item that is equal to or less than every other item. */ function findMinBy(items, comparator) { return findMaxBy(items, (a, b) => -comparator(a, b)); } exports.findMinBy = findMinBy; class ArrayQueue { /** * Constructs a queue that is backed by the given array. Runtime is O(1). */ constructor(items) { this.items = items; this.firstIdx = 0; this.lastIdx = this.items.length - 1; } get length() { return this.lastIdx - this.firstIdx + 1; } /** * Consumes elements from the beginning of the queue as long as the predicate returns true. * If no elements were consumed, `null` is returned. Has a runtime of O(result.length). */ takeWhile(predicate) { // P(k) := k <= this.lastIdx && predicate(this.items[k]) // Find s := min { k | k >= this.firstIdx && !P(k) } and return this.data[this.firstIdx...s) let startIdx = this.firstIdx; while (startIdx < this.items.length && predicate(this.items[startIdx])) { startIdx++; } const result = startIdx === this.firstIdx ? null : this.items.slice(this.firstIdx, startIdx); this.firstIdx = startIdx; return result; } /** * Consumes elements from the end of the queue as long as the predicate returns true. * If no elements were consumed, `null` is returned. * The result has the same order as the underlying array! */ takeFromEndWhile(predicate) { // P(k) := this.firstIdx >= k && predicate(this.items[k]) // Find s := max { k | k <= this.lastIdx && !P(k) } and return this.data(s...this.lastIdx] let endIdx = this.lastIdx; while (endIdx >= 0 && predicate(this.items[endIdx])) { endIdx--; } const result = endIdx === this.lastIdx ? null : this.items.slice(endIdx + 1, this.lastIdx + 1); this.lastIdx = endIdx; return result; } peek() { return this.items[this.firstIdx]; } dequeue() { const result = this.items[this.firstIdx]; this.firstIdx++; return result; } takeCount(count) { const result = this.items.slice(this.firstIdx, this.firstIdx + count); this.firstIdx += count; return result; } } exports.ArrayQueue = ArrayQueue; //# sourceMappingURL=arrays.js.map