typescript
Version:
TypeScript is a language for application scale JavaScript development
1,371 lines (1,368 loc) • 8.86 MB
JavaScript
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/* @internal */
var ts;
(function (ts) {
function createMapShim() {
/** Create a MapLike with good performance. */
function createDictionaryObject() {
var map = Object.create(/*prototype*/ null); // eslint-disable-line no-null/no-null
// Using 'delete' on an object causes V8 to put the object in dictionary mode.
// This disables creation of hidden classes, which are expensive when an object is
// constantly changing shape.
map.__ = undefined;
delete map.__;
return map;
}
var MapIterator = /** @class */ (function () {
function MapIterator(currentEntry, selector) {
this.currentEntry = currentEntry;
this.selector = selector;
}
MapIterator.prototype.next = function () {
// Navigate to the next entry.
while (this.currentEntry) {
var skipNext = !!this.currentEntry.skipNext;
this.currentEntry = this.currentEntry.nextEntry;
if (!skipNext) {
break;
}
}
if (this.currentEntry) {
return { value: this.selector(this.currentEntry.key, this.currentEntry.value), done: false };
}
else {
return { value: undefined, done: true };
}
};
return MapIterator;
}());
return /** @class */ (function () {
function class_1() {
this.data = createDictionaryObject();
this.size = 0;
// Create a first (stub) map entry that will not contain a key
// and value but serves as starting point for iterators.
this.firstEntry = {};
// When the map is empty, the last entry is the same as the
// first one.
this.lastEntry = this.firstEntry;
}
class_1.prototype.get = function (key) {
var entry = this.data[key];
return entry && entry.value;
};
class_1.prototype.set = function (key, value) {
if (!this.has(key)) {
this.size++;
// Create a new entry that will be appended at the
// end of the linked list.
var newEntry = {
key: key,
value: value
};
this.data[key] = newEntry;
// Adjust the references.
var previousLastEntry = this.lastEntry;
previousLastEntry.nextEntry = newEntry;
newEntry.previousEntry = previousLastEntry;
this.lastEntry = newEntry;
}
else {
this.data[key].value = value;
}
return this;
};
class_1.prototype.has = function (key) {
// eslint-disable-next-line no-in-operator
return key in this.data;
};
class_1.prototype.delete = function (key) {
if (this.has(key)) {
this.size--;
var entry = this.data[key];
delete this.data[key];
// Adjust the linked list references of the neighbor entries.
var previousEntry = entry.previousEntry;
previousEntry.nextEntry = entry.nextEntry;
if (entry.nextEntry) {
entry.nextEntry.previousEntry = previousEntry;
}
// When the deleted entry was the last one, we need to
// adjust the lastEntry reference.
if (this.lastEntry === entry) {
this.lastEntry = previousEntry;
}
// Adjust the forward reference of the deleted entry
// in case an iterator still references it. This allows us
// to throw away the entry, but when an active iterator
// (which points to the current entry) continues, it will
// navigate to the entry that originally came before the
// current one and skip it.
entry.previousEntry = undefined;
entry.nextEntry = previousEntry;
entry.skipNext = true;
return true;
}
return false;
};
class_1.prototype.clear = function () {
this.data = createDictionaryObject();
this.size = 0;
// Reset the linked list. Note that we must adjust the forward
// references of the deleted entries to ensure iterators stuck
// in the middle of the list don't continue with deleted entries,
// but can continue with new entries added after the clear()
// operation.
var firstEntry = this.firstEntry;
var currentEntry = firstEntry.nextEntry;
while (currentEntry) {
var nextEntry = currentEntry.nextEntry;
currentEntry.previousEntry = undefined;
currentEntry.nextEntry = firstEntry;
currentEntry.skipNext = true;
currentEntry = nextEntry;
}
firstEntry.nextEntry = undefined;
this.lastEntry = firstEntry;
};
class_1.prototype.keys = function () {
return new MapIterator(this.firstEntry, function (key) { return key; });
};
class_1.prototype.values = function () {
return new MapIterator(this.firstEntry, function (_key, value) { return value; });
};
class_1.prototype.entries = function () {
return new MapIterator(this.firstEntry, function (key, value) { return [key, value]; });
};
class_1.prototype.forEach = function (action) {
var iterator = this.entries();
while (true) {
var iterResult = iterator.next();
if (iterResult.done) {
break;
}
var _a = iterResult.value, key = _a[0], value = _a[1];
action(value, key);
}
};
return class_1;
}());
}
ts.createMapShim = createMapShim;
})(ts || (ts = {}));
var ts;
(function (ts) {
// WARNING: The script `configurePrerelease.ts` uses a regexp to parse out these values.
// If changing the text in this section, be sure to test `configurePrerelease` too.
ts.versionMajorMinor = "3.9";
/** The version of the TypeScript compiler release */
ts.version = "3.9.3";
/**
* Returns the native Map implementation if it is available and compatible (i.e. supports iteration).
*/
/* @internal */
function tryGetNativeMap() {
// eslint-disable-next-line no-in-operator
return typeof Map !== "undefined" && "entries" in Map.prototype ? Map : undefined;
}
ts.tryGetNativeMap = tryGetNativeMap;
/* @internal */
ts.Map = tryGetNativeMap() || (function () {
// NOTE: createMapShim will be defined for typescriptServices.js but not for tsc.js, so we must test for it.
if (typeof ts.createMapShim === "function") {
return ts.createMapShim();
}
throw new Error("TypeScript requires an environment that provides a compatible native Map implementation.");
})();
/* @internal */
var Comparison;
(function (Comparison) {
Comparison[Comparison["LessThan"] = -1] = "LessThan";
Comparison[Comparison["EqualTo"] = 0] = "EqualTo";
Comparison[Comparison["GreaterThan"] = 1] = "GreaterThan";
})(Comparison = ts.Comparison || (ts.Comparison = {}));
})(ts || (ts = {}));
/* @internal */
var ts;
(function (ts) {
ts.emptyArray = [];
/** Create a new map. */
function createMap() {
return new ts.Map();
}
ts.createMap = createMap;
/** Create a new map from an array of entries. */
function createMapFromEntries(entries) {
var map = createMap();
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
var _a = entries_1[_i], key = _a[0], value = _a[1];
map.set(key, value);
}
return map;
}
ts.createMapFromEntries = createMapFromEntries;
/** Create a new map from a template object is provided, the map will copy entries from it. */
function createMapFromTemplate(template) {
var map = new ts.Map();
// Copies keys/values from template. Note that for..in will not throw if
// template is undefined, and instead will just exit the loop.
for (var key in template) {
if (hasOwnProperty.call(template, key)) {
map.set(key, template[key]);
}
}
return map;
}
ts.createMapFromTemplate = createMapFromTemplate;
function length(array) {
return array ? array.length : 0;
}
ts.length = length;
/**
* Iterates through 'array' by index and performs the callback on each element of array until the callback
* returns a truthy value, then returns that value.
* If no such value is found, the callback is applied to each element of array and undefined is returned.
*/
function forEach(array, callback) {
if (array) {
for (var i = 0; i < array.length; i++) {
var result = callback(array[i], i);
if (result) {
return result;
}
}
}
return undefined;
}
ts.forEach = forEach;
/**
* Like `forEach`, but iterates in reverse order.
*/
function forEachRight(array, callback) {
if (array) {
for (var i = array.length - 1; i >= 0; i--) {
var result = callback(array[i], i);
if (result) {
return result;
}
}
}
return undefined;
}
ts.forEachRight = forEachRight;
/** Like `forEach`, but suitable for use with numbers and strings (which may be falsy). */
function firstDefined(array, callback) {
if (array === undefined) {
return undefined;
}
for (var i = 0; i < array.length; i++) {
var result = callback(array[i], i);
if (result !== undefined) {
return result;
}
}
return undefined;
}
ts.firstDefined = firstDefined;
function firstDefinedIterator(iter, callback) {
while (true) {
var iterResult = iter.next();
if (iterResult.done) {
return undefined;
}
var result = callback(iterResult.value);
if (result !== undefined) {
return result;
}
}
}
ts.firstDefinedIterator = firstDefinedIterator;
function zipWith(arrayA, arrayB, callback) {
var result = [];
ts.Debug.assertEqual(arrayA.length, arrayB.length);
for (var i = 0; i < arrayA.length; i++) {
result.push(callback(arrayA[i], arrayB[i], i));
}
return result;
}
ts.zipWith = zipWith;
function zipToIterator(arrayA, arrayB) {
ts.Debug.assertEqual(arrayA.length, arrayB.length);
var i = 0;
return {
next: function () {
if (i === arrayA.length) {
return { value: undefined, done: true };
}
i++;
return { value: [arrayA[i - 1], arrayB[i - 1]], done: false };
}
};
}
ts.zipToIterator = zipToIterator;
function zipToMap(keys, values) {
ts.Debug.assert(keys.length === values.length);
var map = createMap();
for (var i = 0; i < keys.length; ++i) {
map.set(keys[i], values[i]);
}
return map;
}
ts.zipToMap = zipToMap;
/**
* Iterates through `array` by index and performs the callback on each element of array until the callback
* returns a falsey value, then returns false.
* If no such value is found, the callback is applied to each element of array and `true` is returned.
*/
function every(array, callback) {
if (array) {
for (var i = 0; i < array.length; i++) {
if (!callback(array[i], i)) {
return false;
}
}
}
return true;
}
ts.every = every;
function find(array, predicate) {
for (var i = 0; i < array.length; i++) {
var value = array[i];
if (predicate(value, i)) {
return value;
}
}
return undefined;
}
ts.find = find;
function findLast(array, predicate) {
for (var i = array.length - 1; i >= 0; i--) {
var value = array[i];
if (predicate(value, i)) {
return value;
}
}
return undefined;
}
ts.findLast = findLast;
/** Works like Array.prototype.findIndex, returning `-1` if no element satisfying the predicate is found. */
function findIndex(array, predicate, startIndex) {
for (var i = startIndex || 0; i < array.length; i++) {
if (predicate(array[i], i)) {
return i;
}
}
return -1;
}
ts.findIndex = findIndex;
function findLastIndex(array, predicate, startIndex) {
for (var i = startIndex === undefined ? array.length - 1 : startIndex; i >= 0; i--) {
if (predicate(array[i], i)) {
return i;
}
}
return -1;
}
ts.findLastIndex = findLastIndex;
/**
* Returns the first truthy result of `callback`, or else fails.
* This is like `forEach`, but never returns undefined.
*/
function findMap(array, callback) {
for (var i = 0; i < array.length; i++) {
var result = callback(array[i], i);
if (result) {
return result;
}
}
return ts.Debug.fail();
}
ts.findMap = findMap;
function contains(array, value, equalityComparer) {
if (equalityComparer === void 0) { equalityComparer = equateValues; }
if (array) {
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
var v = array_1[_i];
if (equalityComparer(v, value)) {
return true;
}
}
}
return false;
}
ts.contains = contains;
function arraysEqual(a, b, equalityComparer) {
if (equalityComparer === void 0) { equalityComparer = equateValues; }
return a.length === b.length && a.every(function (x, i) { return equalityComparer(x, b[i]); });
}
ts.arraysEqual = arraysEqual;
function indexOfAnyCharCode(text, charCodes, start) {
for (var i = start || 0; i < text.length; i++) {
if (contains(charCodes, text.charCodeAt(i))) {
return i;
}
}
return -1;
}
ts.indexOfAnyCharCode = indexOfAnyCharCode;
function countWhere(array, predicate) {
var count = 0;
if (array) {
for (var i = 0; i < array.length; i++) {
var v = array[i];
if (predicate(v, i)) {
count++;
}
}
}
return count;
}
ts.countWhere = countWhere;
function filter(array, f) {
if (array) {
var len = array.length;
var i = 0;
while (i < len && f(array[i]))
i++;
if (i < len) {
var result = array.slice(0, i);
i++;
while (i < len) {
var item = array[i];
if (f(item)) {
result.push(item);
}
i++;
}
return result;
}
}
return array;
}
ts.filter = filter;
function filterMutate(array, f) {
var outIndex = 0;
for (var i = 0; i < array.length; i++) {
if (f(array[i], i, array)) {
array[outIndex] = array[i];
outIndex++;
}
}
array.length = outIndex;
}
ts.filterMutate = filterMutate;
function clear(array) {
array.length = 0;
}
ts.clear = clear;
function map(array, f) {
var result;
if (array) {
result = [];
for (var i = 0; i < array.length; i++) {
result.push(f(array[i], i));
}
}
return result;
}
ts.map = map;
function mapIterator(iter, mapFn) {
return {
next: function () {
var iterRes = iter.next();
return iterRes.done ? iterRes : { value: mapFn(iterRes.value), done: false };
}
};
}
ts.mapIterator = mapIterator;
function sameMap(array, f) {
if (array) {
for (var i = 0; i < array.length; i++) {
var item = array[i];
var mapped = f(item, i);
if (item !== mapped) {
var result = array.slice(0, i);
result.push(mapped);
for (i++; i < array.length; i++) {
result.push(f(array[i], i));
}
return result;
}
}
}
return array;
}
ts.sameMap = sameMap;
/**
* Flattens an array containing a mix of array or non-array elements.
*
* @param array The array to flatten.
*/
function flatten(array) {
var result = [];
for (var _i = 0, array_2 = array; _i < array_2.length; _i++) {
var v = array_2[_i];
if (v) {
if (isArray(v)) {
addRange(result, v);
}
else {
result.push(v);
}
}
}
return result;
}
ts.flatten = flatten;
/**
* Maps an array. If the mapped value is an array, it is spread into the result.
*
* @param array The array to map.
* @param mapfn The callback used to map the result into one or more values.
*/
function flatMap(array, mapfn) {
var result;
if (array) {
for (var i = 0; i < array.length; i++) {
var v = mapfn(array[i], i);
if (v) {
if (isArray(v)) {
result = addRange(result, v);
}
else {
result = append(result, v);
}
}
}
}
return result || ts.emptyArray;
}
ts.flatMap = flatMap;
function flatMapToMutable(array, mapfn) {
var result = [];
if (array) {
for (var i = 0; i < array.length; i++) {
var v = mapfn(array[i], i);
if (v) {
if (isArray(v)) {
addRange(result, v);
}
else {
result.push(v);
}
}
}
}
return result;
}
ts.flatMapToMutable = flatMapToMutable;
function flatMapIterator(iter, mapfn) {
var first = iter.next();
if (first.done) {
return ts.emptyIterator;
}
var currentIter = getIterator(first.value);
return {
next: function () {
while (true) {
var currentRes = currentIter.next();
if (!currentRes.done) {
return currentRes;
}
var iterRes = iter.next();
if (iterRes.done) {
return iterRes;
}
currentIter = getIterator(iterRes.value);
}
},
};
function getIterator(x) {
var res = mapfn(x);
return res === undefined ? ts.emptyIterator : isArray(res) ? arrayIterator(res) : res;
}
}
ts.flatMapIterator = flatMapIterator;
function sameFlatMap(array, mapfn) {
var result;
if (array) {
for (var i = 0; i < array.length; i++) {
var item = array[i];
var mapped = mapfn(item, i);
if (result || item !== mapped || isArray(mapped)) {
if (!result) {
result = array.slice(0, i);
}
if (isArray(mapped)) {
addRange(result, mapped);
}
else {
result.push(mapped);
}
}
}
}
return result || array;
}
ts.sameFlatMap = sameFlatMap;
function mapAllOrFail(array, mapFn) {
var result = [];
for (var i = 0; i < array.length; i++) {
var mapped = mapFn(array[i], i);
if (mapped === undefined) {
return undefined;
}
result.push(mapped);
}
return result;
}
ts.mapAllOrFail = mapAllOrFail;
function mapDefined(array, mapFn) {
var result = [];
if (array) {
for (var i = 0; i < array.length; i++) {
var mapped = mapFn(array[i], i);
if (mapped !== undefined) {
result.push(mapped);
}
}
}
return result;
}
ts.mapDefined = mapDefined;
function mapDefinedIterator(iter, mapFn) {
return {
next: function () {
while (true) {
var res = iter.next();
if (res.done) {
return res;
}
var value = mapFn(res.value);
if (value !== undefined) {
return { value: value, done: false };
}
}
}
};
}
ts.mapDefinedIterator = mapDefinedIterator;
function mapDefinedMap(map, mapValue, mapKey) {
if (mapKey === void 0) { mapKey = identity; }
var result = createMap();
map.forEach(function (value, key) {
var mapped = mapValue(value, key);
if (mapped !== undefined) {
result.set(mapKey(key), mapped);
}
});
return result;
}
ts.mapDefinedMap = mapDefinedMap;
ts.emptyIterator = { next: function () { return ({ value: undefined, done: true }); } };
function singleIterator(value) {
var done = false;
return {
next: function () {
var wasDone = done;
done = true;
return wasDone ? { value: undefined, done: true } : { value: value, done: false };
}
};
}
ts.singleIterator = singleIterator;
function spanMap(array, keyfn, mapfn) {
var result;
if (array) {
result = [];
var len = array.length;
var previousKey = void 0;
var key = void 0;
var start = 0;
var pos = 0;
while (start < len) {
while (pos < len) {
var value = array[pos];
key = keyfn(value, pos);
if (pos === 0) {
previousKey = key;
}
else if (key !== previousKey) {
break;
}
pos++;
}
if (start < pos) {
var v = mapfn(array.slice(start, pos), previousKey, start, pos);
if (v) {
result.push(v);
}
start = pos;
}
previousKey = key;
pos++;
}
}
return result;
}
ts.spanMap = spanMap;
function mapEntries(map, f) {
if (!map) {
return undefined;
}
var result = createMap();
map.forEach(function (value, key) {
var _a = f(key, value), newKey = _a[0], newValue = _a[1];
result.set(newKey, newValue);
});
return result;
}
ts.mapEntries = mapEntries;
function some(array, predicate) {
if (array) {
if (predicate) {
for (var _i = 0, array_3 = array; _i < array_3.length; _i++) {
var v = array_3[_i];
if (predicate(v)) {
return true;
}
}
}
else {
return array.length > 0;
}
}
return false;
}
ts.some = some;
/** Calls the callback with (start, afterEnd) index pairs for each range where 'pred' is true. */
function getRangesWhere(arr, pred, cb) {
var start;
for (var i = 0; i < arr.length; i++) {
if (pred(arr[i])) {
start = start === undefined ? i : start;
}
else {
if (start !== undefined) {
cb(start, i);
start = undefined;
}
}
}
if (start !== undefined)
cb(start, arr.length);
}
ts.getRangesWhere = getRangesWhere;
function concatenate(array1, array2) {
if (!some(array2))
return array1;
if (!some(array1))
return array2;
return __spreadArrays(array1, array2);
}
ts.concatenate = concatenate;
function selectIndex(_, i) {
return i;
}
function indicesOf(array) {
return array.map(selectIndex);
}
ts.indicesOf = indicesOf;
function deduplicateRelational(array, equalityComparer, comparer) {
// Perform a stable sort of the array. This ensures the first entry in a list of
// duplicates remains the first entry in the result.
var indices = indicesOf(array);
stableSortIndices(array, indices, comparer);
var last = array[indices[0]];
var deduplicated = [indices[0]];
for (var i = 1; i < indices.length; i++) {
var index = indices[i];
var item = array[index];
if (!equalityComparer(last, item)) {
deduplicated.push(index);
last = item;
}
}
// restore original order
deduplicated.sort();
return deduplicated.map(function (i) { return array[i]; });
}
function deduplicateEquality(array, equalityComparer) {
var result = [];
for (var _i = 0, array_4 = array; _i < array_4.length; _i++) {
var item = array_4[_i];
pushIfUnique(result, item, equalityComparer);
}
return result;
}
/**
* Deduplicates an unsorted array.
* @param equalityComparer An `EqualityComparer` used to determine if two values are duplicates.
* @param comparer An optional `Comparer` used to sort entries before comparison, though the
* result will remain in the original order in `array`.
*/
function deduplicate(array, equalityComparer, comparer) {
return array.length === 0 ? [] :
array.length === 1 ? array.slice() :
comparer ? deduplicateRelational(array, equalityComparer, comparer) :
deduplicateEquality(array, equalityComparer);
}
ts.deduplicate = deduplicate;
/**
* Deduplicates an array that has already been sorted.
*/
function deduplicateSorted(array, comparer) {
if (array.length === 0)
return ts.emptyArray;
var last = array[0];
var deduplicated = [last];
for (var i = 1; i < array.length; i++) {
var next = array[i];
switch (comparer(next, last)) {
// equality comparison
case true:
// relational comparison
// falls through
case 0 /* EqualTo */:
continue;
case -1 /* LessThan */:
// If `array` is sorted, `next` should **never** be less than `last`.
return ts.Debug.fail("Array is unsorted.");
}
deduplicated.push(last = next);
}
return deduplicated;
}
function insertSorted(array, insert, compare) {
if (array.length === 0) {
array.push(insert);
return;
}
var insertIndex = binarySearch(array, insert, identity, compare);
if (insertIndex < 0) {
array.splice(~insertIndex, 0, insert);
}
}
ts.insertSorted = insertSorted;
function sortAndDeduplicate(array, comparer, equalityComparer) {
return deduplicateSorted(sort(array, comparer), equalityComparer || comparer || compareStringsCaseSensitive);
}
ts.sortAndDeduplicate = sortAndDeduplicate;
function arrayIsEqualTo(array1, array2, equalityComparer) {
if (equalityComparer === void 0) { equalityComparer = equateValues; }
if (!array1 || !array2) {
return array1 === array2;
}
if (array1.length !== array2.length) {
return false;
}
for (var i = 0; i < array1.length; i++) {
if (!equalityComparer(array1[i], array2[i], i)) {
return false;
}
}
return true;
}
ts.arrayIsEqualTo = arrayIsEqualTo;
function compact(array) {
var result;
if (array) {
for (var i = 0; i < array.length; i++) {
var v = array[i];
if (result || !v) {
if (!result) {
result = array.slice(0, i);
}
if (v) {
result.push(v);
}
}
}
}
return result || array;
}
ts.compact = compact;
/**
* Gets the relative complement of `arrayA` with respect to `arrayB`, returning the elements that
* are not present in `arrayA` but are present in `arrayB`. Assumes both arrays are sorted
* based on the provided comparer.
*/
function relativeComplement(arrayA, arrayB, comparer) {
if (!arrayB || !arrayA || arrayB.length === 0 || arrayA.length === 0)
return arrayB;
var result = [];
loopB: for (var offsetA = 0, offsetB = 0; offsetB < arrayB.length; offsetB++) {
if (offsetB > 0) {
// Ensure `arrayB` is properly sorted.
ts.Debug.assertGreaterThanOrEqual(comparer(arrayB[offsetB], arrayB[offsetB - 1]), 0 /* EqualTo */);
}
loopA: for (var startA = offsetA; offsetA < arrayA.length; offsetA++) {
if (offsetA > startA) {
// Ensure `arrayA` is properly sorted. We only need to perform this check if
// `offsetA` has changed since we entered the loop.
ts.Debug.assertGreaterThanOrEqual(comparer(arrayA[offsetA], arrayA[offsetA - 1]), 0 /* EqualTo */);
}
switch (comparer(arrayB[offsetB], arrayA[offsetA])) {
case -1 /* LessThan */:
// If B is less than A, B does not exist in arrayA. Add B to the result and
// move to the next element in arrayB without changing the current position
// in arrayA.
result.push(arrayB[offsetB]);
continue loopB;
case 0 /* EqualTo */:
// If B is equal to A, B exists in arrayA. Move to the next element in
// arrayB without adding B to the result or changing the current position
// in arrayA.
continue loopB;
case 1 /* GreaterThan */:
// If B is greater than A, we need to keep looking for B in arrayA. Move to
// the next element in arrayA and recheck.
continue loopA;
}
}
}
return result;
}
ts.relativeComplement = relativeComplement;
function sum(array, prop) {
var result = 0;
for (var _i = 0, array_5 = array; _i < array_5.length; _i++) {
var v = array_5[_i];
result += v[prop];
}
return result;
}
ts.sum = sum;
function append(to, value) {
if (value === undefined)
return to;
if (to === undefined)
return [value];
to.push(value);
return to;
}
ts.append = append;
function combine(xs, ys) {
if (xs === undefined)
return ys;
if (ys === undefined)
return xs;
if (isArray(xs))
return isArray(ys) ? concatenate(xs, ys) : append(xs, ys);
if (isArray(ys))
return append(ys, xs);
return [xs, ys];
}
ts.combine = combine;
/**
* Gets the actual offset into an array for a relative offset. Negative offsets indicate a
* position offset from the end of the array.
*/
function toOffset(array, offset) {
return offset < 0 ? array.length + offset : offset;
}
function addRange(to, from, start, end) {
if (from === undefined || from.length === 0)
return to;
if (to === undefined)
return from.slice(start, end);
start = start === undefined ? 0 : toOffset(from, start);
end = end === undefined ? from.length : toOffset(from, end);
for (var i = start; i < end && i < from.length; i++) {
if (from[i] !== undefined) {
to.push(from[i]);
}
}
return to;
}
ts.addRange = addRange;
/**
* @return Whether the value was added.
*/
function pushIfUnique(array, toAdd, equalityComparer) {
if (contains(array, toAdd, equalityComparer)) {
return false;
}
else {
array.push(toAdd);
return true;
}
}
ts.pushIfUnique = pushIfUnique;
/**
* Unlike `pushIfUnique`, this can take `undefined` as an input, and returns a new array.
*/
function appendIfUnique(array, toAdd, equalityComparer) {
if (array) {
pushIfUnique(array, toAdd, equalityComparer);
return array;
}
else {
return [toAdd];
}
}
ts.appendIfUnique = appendIfUnique;
function stableSortIndices(array, indices, comparer) {
// sort indices by value then position
indices.sort(function (x, y) { return comparer(array[x], array[y]) || compareValues(x, y); });
}
/**
* Returns a new sorted array.
*/
function sort(array, comparer) {
return (array.length === 0 ? array : array.slice().sort(comparer));
}
ts.sort = sort;
function arrayIterator(array) {
var i = 0;
return { next: function () {
if (i === array.length) {
return { value: undefined, done: true };
}
else {
i++;
return { value: array[i - 1], done: false };
}
} };
}
ts.arrayIterator = arrayIterator;
function arrayReverseIterator(array) {
var i = array.length;
return {
next: function () {
if (i === 0) {
return { value: undefined, done: true };
}
else {
i--;
return { value: array[i], done: false };
}
}
};
}
ts.arrayReverseIterator = arrayReverseIterator;
/**
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
*/
function stableSort(array, comparer) {
var indices = indicesOf(array);
stableSortIndices(array, indices, comparer);
return indices.map(function (i) { return array[i]; });
}
ts.stableSort = stableSort;
function rangeEquals(array1, array2, pos, end) {
while (pos < end) {
if (array1[pos] !== array2[pos]) {
return false;
}
pos++;
}
return true;
}
ts.rangeEquals = rangeEquals;
/**
* Returns the element at a specific offset in an array if non-empty, `undefined` otherwise.
* A negative offset indicates the element should be retrieved from the end of the array.
*/
function elementAt(array, offset) {
if (array) {
offset = toOffset(array, offset);
if (offset < array.length) {
return array[offset];
}
}
return undefined;
}
ts.elementAt = elementAt;
/**
* Returns the first element of an array if non-empty, `undefined` otherwise.
*/
function firstOrUndefined(array) {
return array.length === 0 ? undefined : array[0];
}
ts.firstOrUndefined = firstOrUndefined;
function first(array) {
ts.Debug.assert(array.length !== 0);
return array[0];
}
ts.first = first;
/**
* Returns the last element of an array if non-empty, `undefined` otherwise.
*/
function lastOrUndefined(array) {
return array.length === 0 ? undefined : array[array.length - 1];
}
ts.lastOrUndefined = lastOrUndefined;
function last(array) {
ts.Debug.assert(array.length !== 0);
return array[array.length - 1];
}
ts.last = last;
/**
* Returns the only element of an array if it contains only one element, `undefined` otherwise.
*/
function singleOrUndefined(array) {
return array && array.length === 1
? array[0]
: undefined;
}
ts.singleOrUndefined = singleOrUndefined;
function singleOrMany(array) {
return array && array.length === 1
? array[0]
: array;
}
ts.singleOrMany = singleOrMany;
function replaceElement(array, index, value) {
var result = array.slice(0);
result[index] = value;
return result;
}
ts.replaceElement = replaceElement;
/**
* Performs a binary search, finding the index at which `value` occurs in `array`.
* If no such index is found, returns the 2's-complement of first index at which
* `array[index]` exceeds `value`.
* @param array A sorted array whose first element must be no larger than number
* @param value The value to be searched for in the array.
* @param keySelector A callback used to select the search key from `value` and each element of
* `array`.
* @param keyComparer A callback used to compare two keys in a sorted array.
* @param offset An offset into `array` at which to start the search.
*/
function binarySearch(array, value, keySelector, keyComparer, offset) {
return binarySearchKey(array, keySelector(value), keySelector, keyComparer, offset);
}
ts.binarySearch = binarySearch;
/**
* Performs a binary search, finding the index at which an object with `key` occurs in `array`.
* If no such index is found, returns the 2's-complement of first index at which
* `array[index]` exceeds `key`.
* @param array A sorted array whose first element must be no larger than number
* @param key The key to be searched for in the array.
* @param keySelector A callback used to select the search key from each element of `array`.
* @param keyComparer A callback used to compare two keys in a sorted array.
* @param offset An offset into `array` at which to start the search.
*/
function binarySearchKey(array, key, keySelector, keyComparer, offset) {
if (!some(array)) {
return -1;
}
var low = offset || 0;
var high = array.length - 1;
while (low <= high) {
var middle = low + ((high - low) >> 1);
var midKey = keySelector(array[middle]);
switch (keyComparer(midKey, key)) {
case -1 /* LessThan */:
low = middle + 1;
break;
case 0 /* EqualTo */:
return middle;
case 1 /* GreaterThan */:
high = middle - 1;
break;
}
}
return ~low;
}
ts.binarySearchKey = binarySearchKey;
function reduceLeft(array, f, initial, start, count) {
if (array && array.length > 0) {
var size = array.length;
if (size > 0) {
var pos = start === undefined || start < 0 ? 0 : start;
var end = count === undefined || pos + count > size - 1 ? size - 1 : pos + count;
var result = void 0;
if (arguments.length <= 2) {
result = array[pos];
pos++;
}
else {
result = initial;
}
while (pos <= end) {
result = f(result, array[pos], pos);
pos++;
}
return result;
}
}
return initial;
}
ts.reduceLeft = reduceLeft;
var hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* Indicates whether a map-like contains an own property with the specified key.
*
* @param map A map-like.
* @param key A property key.
*/
function hasProperty(map, key) {
return hasOwnProperty.call(map, key);
}
ts.hasProperty = hasProperty;
/**
* Gets the value of an owned property in a map-like.
*
* @param map A map-like.
* @param key A property key.
*/
function getProperty(map, key) {
return hasOwnProperty.call(map, key) ? map[key] : undefined;
}
ts.getProperty = getProperty;
/**
* Gets the owned, enumerable property keys of a map-like.
*/
function getOwnKeys(map) {
var keys = [];
for (var key in map) {
if (hasOwnProperty.call(map, key)) {
keys.push(key);
}
}
return keys;
}
ts.getOwnKeys = getOwnKeys;
function getAllKeys(obj) {
var result = [];
do {
var names = Object.getOwnPropertyNames(obj);
for (var _i = 0, names_1 = names; _i < names_1.length; _i++) {
var name = names_1[_i];
pushIfUnique(result, name);
}
} while (obj = Object.getPrototypeOf(obj));
return result;
}
ts.getAllKeys = getAllKeys;
function getOwnValues(sparseArray) {
var values = [];
for (var key in sparseArray) {
if (hasOwnProperty.call(sparseArray, key)) {
values.push(sparseArray[key]);
}
}
return values;
}
ts.getOwnValues = getOwnValues;
function arrayFrom(iterator, map) {
var result = [];
for (var iterResult = iterator.next(); !iterResult.done; iterResult = iterator.next()) {
result.push(map ? map(iterResult.value) : iterResult.value);
}
return result;
}
ts.arrayFrom = arrayFrom;
function assign(t) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
var arg = args_1[_a];
if (arg === undefined)
continue;
for (var p in arg) {
if (hasProperty(arg, p)) {
t[p] = arg[p];
}
}
}
return t;
}
ts.assign = assign;
/**
* Performs a shallow equality comparison of the contents of two map-likes.
*
* @param left A map-like whose properties should be compared.
* @param right A map-like whose properties should be compared.
*/
function equalOwnProperties(left, right, equalityComparer) {
if (equalityComparer === voi