data-forge
Version:
JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
1,489 lines (1,427 loc) • 422 kB
JavaScript
import Table from 'easy-table';
import moment from 'dayjs/esm';
import t from 'typy';
import JSON5 from 'json5';
import PapaParse from 'papaparse';
import customParseFormat from 'dayjs/esm/plugin/customParseFormat';
import numeral from 'numeral';
/*! *****************************************************************************
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.
***************************************************************************** */
/* global Reflect, Promise */
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);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}
//
// An iterator that returns no values.
//
var EmptyIterator = /** @class */ (function () {
function EmptyIterator() {
}
EmptyIterator.prototype.next = function () {
return {
done: true,
value: null
};
};
return EmptyIterator;
}());
//
var EmptyIterable = /** @class */ (function () {
function EmptyIterable() {
}
EmptyIterable.prototype[Symbol.iterator] = function () {
return new EmptyIterator();
};
return EmptyIterable;
}());
//
// An iterator that simply counts up from zero.
// This creates the default index in Data-Forge.
//
var CountIterator = /** @class */ (function () {
function CountIterator() {
this.index = 0;
}
CountIterator.prototype.next = function () {
return {
done: false,
value: this.index++
};
};
return CountIterator;
}());
//
var CountIterable = /** @class */ (function () {
function CountIterable() {
}
CountIterable.prototype[Symbol.iterator] = function () {
return new CountIterator();
};
return CountIterable;
}());
//
var MultiIterator = /** @class */ (function () {
function MultiIterator(iterators) {
this.iterators = iterators;
}
MultiIterator.prototype.next = function () {
if (this.iterators.length === 0) {
return {
done: true,
value: [],
};
}
var multiResult = [];
try {
for (var _a = __values(this.iterators), _b = _a.next(); !_b.done; _b = _a.next()) {
var iterator = _b.value;
var result = iterator.next();
if (result.done) {
return {
done: true,
value: [],
};
}
multiResult.push(result.value);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
return {
done: false,
value: multiResult
};
var e_1, _c;
};
return MultiIterator;
}());
//
var MultiIterable = /** @class */ (function () {
function MultiIterable(iterables) {
this.iterables = iterables;
}
MultiIterable.prototype[Symbol.iterator] = function () {
var iterators = [];
try {
for (var _a = __values(this.iterables), _b = _a.next(); !_b.done; _b = _a.next()) {
var iterable = _b.value;
iterators.push(iterable[Symbol.iterator]());
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
return new MultiIterator(iterators);
var e_1, _c;
};
return MultiIterable;
}());
//
// An iterator that applies a selector function to each item.
//
var SelectIterator = /** @class */ (function () {
function SelectIterator(iterator, selector) {
this.index = 0;
this.iterator = iterator;
this.selector = selector;
}
SelectIterator.prototype.next = function () {
var result = this.iterator.next();
if (result.done) {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
return {
done: false,
value: this.selector(result.value, this.index++)
};
};
return SelectIterator;
}());
//
var SelectIterable = /** @class */ (function () {
function SelectIterable(iterable, selector) {
this.iterable = iterable;
this.selector = selector;
}
SelectIterable.prototype[Symbol.iterator] = function () {
var iterator = this.iterable[Symbol.iterator]();
return new SelectIterator(iterator, this.selector);
};
return SelectIterable;
}());
//
// An iterator that applies a selector function to each item.
//
var SelectManyIterator = /** @class */ (function () {
function SelectManyIterator(iterator, selector) {
this.index = 0;
this.iterator = iterator;
this.selector = selector;
this.outputIterator = null;
}
SelectManyIterator.prototype.next = function () {
// eslint-disable-next-line no-constant-condition
while (true) {
if (this.outputIterator === null) {
var result = this.iterator.next();
if (result.done) {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
var outputIterable = this.selector(result.value, this.index++);
this.outputIterator = outputIterable[Symbol.iterator]();
}
var outputResult = this.outputIterator.next();
if (outputResult.done) {
this.outputIterator = null;
continue;
}
else {
return outputResult;
}
}
};
return SelectManyIterator;
}());
//
var SelectManyIterable = /** @class */ (function () {
function SelectManyIterable(iterable, selector) {
this.iterable = iterable;
this.selector = selector;
}
SelectManyIterable.prototype[Symbol.iterator] = function () {
var iterator = this.iterable[Symbol.iterator]();
return new SelectManyIterator(iterator, this.selector);
};
return SelectManyIterable;
}());
//
// An iterator that a sequence of elements while a predicate function returns true.
//
var TakeIterator = /** @class */ (function () {
function TakeIterator(childIterator, numElements) {
this.childIterator = childIterator;
this.numElements = numElements;
}
TakeIterator.prototype.next = function () {
if (this.numElements <= 0) {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
--this.numElements;
return this.childIterator.next();
};
return TakeIterator;
}());
//
var TakeIterable = /** @class */ (function () {
function TakeIterable(childIterable, numElements) {
this.childIterable = childIterable;
this.numElements = numElements;
}
TakeIterable.prototype[Symbol.iterator] = function () {
var childIterator = this.childIterable[Symbol.iterator]();
return new TakeIterator(childIterator, this.numElements);
};
return TakeIterable;
}());
//
// An iterator that takes a sequence of elements while a predicate function returns true.
//
var TakeWhileIterator = /** @class */ (function () {
function TakeWhileIterator(childIterator, predicate) {
this.done = false;
this.childIterator = childIterator;
this.predicate = predicate;
}
TakeWhileIterator.prototype.next = function () {
if (!this.done) {
var result = this.childIterator.next();
if (result.done) {
this.done = true;
}
else if (this.predicate(result.value)) {
return result;
}
else {
this.done = true;
}
}
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
};
return TakeWhileIterator;
}());
//
var TakeWhileIterable = /** @class */ (function () {
function TakeWhileIterable(childIterable, predicate) {
this.childIterable = childIterable;
this.predicate = predicate;
}
TakeWhileIterable.prototype[Symbol.iterator] = function () {
var childIterator = this.childIterable[Symbol.iterator]();
return new TakeWhileIterator(childIterator, this.predicate);
};
return TakeWhileIterable;
}());
//
// An iterator that takes elements from a child iterator based on a predicate function.
//
var WhereIterator = /** @class */ (function () {
function WhereIterator(childIterator, predicate) {
this.childIterator = childIterator;
this.predicate = predicate;
}
WhereIterator.prototype.next = function () {
// eslint-disable-next-line no-constant-condition
while (true) {
var result = this.childIterator.next();
if (result.done) {
return result;
}
if (this.predicate(result.value)) {
// It matches the predicate.
return result;
}
}
};
return WhereIterator;
}());
//
var WhereIterable = /** @class */ (function () {
function WhereIterable(childIterable, predicate) {
this.childIterable = childIterable;
this.predicate = predicate;
}
WhereIterable.prototype[Symbol.iterator] = function () {
var childIterator = this.childIterable[Symbol.iterator]();
return new WhereIterator(childIterator, this.predicate);
};
return WhereIterable;
}());
//
// An iterator that concatenates multiple iterables.
//
var ConcatIterator = /** @class */ (function () {
function ConcatIterator(iterables) {
this.curIterator = null;
this.iterables = iterables;
this.iterator = iterables[Symbol.iterator]();
this.moveToNextIterable();
}
//
// Move onto the next iterable.
//
ConcatIterator.prototype.moveToNextIterable = function () {
var nextIterable = this.iterator.next();
if (nextIterable.done) {
this.curIterator = null;
}
else {
this.curIterator = nextIterable.value[Symbol.iterator]();
}
};
ConcatIterator.prototype.next = function () {
// eslint-disable-next-line no-constant-condition
while (true) {
if (this.curIterator == null) {
// Finished iterating all sub-iterators.
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
var result = this.curIterator.next();
if (!result.done) {
return result; // Found a valid result from the current iterable.
}
// Find the next non empty iterable.
this.moveToNextIterable();
}
};
return ConcatIterator;
}());
//
var ConcatIterable = /** @class */ (function () {
function ConcatIterable(iterables) {
this.iterables = iterables;
}
ConcatIterable.prototype[Symbol.iterator] = function () {
return new ConcatIterator(this.iterables);
};
return ConcatIterable;
}());
//
var SeriesWindowIterator = /** @class */ (function () {
function SeriesWindowIterator(iterable, period, whichIndex) {
this.iterable = iterable;
this.period = period;
this.whichIndex = whichIndex;
}
SeriesWindowIterator.prototype.next = function () {
if (!this.iterator) {
this.iterator = this.iterable[Symbol.iterator]();
}
var curWindow = [];
for (var i = 0; i < this.period; ++i) {
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator is finished.
break;
}
curWindow.push(curPos.value);
}
if (curWindow.length === 0) {
// Underlying iterator doesn't have required number of elements.
return { done: true };
}
var window = new Series({
pairs: curWindow
});
return {
//TODO: The way the index is figured out could have much better performance.
value: [this.whichIndex === WhichIndex.Start ? window.getIndex().first() : window.getIndex().last(), window],
done: false,
};
};
return SeriesWindowIterator;
}());
//
var SeriesWindowIterable = /** @class */ (function () {
function SeriesWindowIterable(iterable, period, whichIndex) {
this.iterable = iterable;
this.period = period;
this.whichIndex = whichIndex;
}
SeriesWindowIterable.prototype[Symbol.iterator] = function () {
return new SeriesWindowIterator(this.iterable, this.period, this.whichIndex);
};
return SeriesWindowIterable;
}());
//
// An iterator that iterates the elements of an array.
//
var ArrayIterator = /** @class */ (function () {
function ArrayIterator(arr) {
this.index = 0;
this.arr = arr;
}
ArrayIterator.prototype.next = function () {
if (this.index < this.arr.length) {
return {
done: false,
value: this.arr[this.index++],
};
}
else {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
};
return ArrayIterator;
}());
//
var ReverseIterable = /** @class */ (function () {
function ReverseIterable(iterable) {
this.iterable = iterable;
}
ReverseIterable.prototype[Symbol.iterator] = function () {
var working = [];
try {
for (var _a = __values(this.iterable), _b = _a.next(); !_b.done; _b = _a.next()) {
var value = _b.value;
working.push(value);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
working.reverse();
return new ArrayIterator(working);
var e_1, _c;
};
return ReverseIterable;
}());
var ZipIterator = /** @class */ (function () {
function ZipIterator(iterables, zipper) {
this.iterators = iterables.map(function (iterable) { return iterable[Symbol.iterator](); });
this.zipper = zipper;
}
ZipIterator.prototype.next = function () {
var results = this.iterators.map(function (iterator) { return iterator.next(); });
try {
for (var results_1 = __values(results), results_1_1 = results_1.next(); !results_1_1.done; results_1_1 = results_1.next()) {
var result = results_1_1.value;
if (result.done) {
// If any are done we are all done.
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (results_1_1 && !results_1_1.done && (_a = results_1.return)) _a.call(results_1);
}
finally { if (e_1) throw e_1.error; }
}
var zippedValues = results.map(function (result) { return result.value; });
var zipperInput = new Series(zippedValues);
return {
done: false,
value: this.zipper(zipperInput)
};
var e_1, _a;
};
return ZipIterator;
}());
//
var ZipIterable = /** @class */ (function () {
function ZipIterable(iterables, zipper) {
this.iterables = iterables;
this.zipper = zipper;
}
ZipIterable.prototype[Symbol.iterator] = function () {
return new ZipIterator(this.iterables, this.zipper);
};
return ZipIterable;
}());
//
// An iterator that iterates the only distinct elements of another iterable.
//
var DistinctIterator = /** @class */ (function () {
function DistinctIterator(iterable, selector) {
this.valuesAlreadySeen = new Set();
this.iterator = iterable[Symbol.iterator]();
this.selector = selector;
}
DistinctIterator.prototype.next = function () {
// eslint-disable-next-line no-constant-condition
while (true) {
var result = this.iterator.next();
if (result.done) {
return { done: true };
}
var potentialOutput = void 0;
if (this.selector) {
potentialOutput = this.selector(result.value);
}
else {
potentialOutput = result.value;
}
if (this.valuesAlreadySeen.has(potentialOutput)) {
// Already seen this value.
// Skip it and continue to next item.
continue;
}
this.valuesAlreadySeen.add(potentialOutput);
return {
done: false,
value: result.value,
};
}
};
return DistinctIterator;
}());
//
var DistinctIterable = /** @class */ (function () {
function DistinctIterable(iterable, selector) {
this.iterable = iterable;
this.selector = selector;
}
DistinctIterable.prototype[Symbol.iterator] = function () {
return new DistinctIterator(this.iterable, this.selector);
};
return DistinctIterable;
}());
//
var SeriesRollingWindowIterator = /** @class */ (function () {
function SeriesRollingWindowIterator(iterable, period, whichIndex) {
this.iterable = iterable;
this.period = period;
this.whichIndex = whichIndex;
}
SeriesRollingWindowIterator.prototype.next = function () {
if (!this.curWindow) {
this.curWindow = [];
this.iterator = this.iterable[Symbol.iterator]();
for (var i = 0; i < this.period; ++i) {
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator doesn't have required number of elements.
return { done: true };
}
this.curWindow.push(curPos.value);
}
}
else {
this.curWindow.shift(); // Remove first item from window.
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator doesn't have enough elements left.
return { done: true };
}
this.curWindow.push(curPos.value); // Add next item to window.
}
var window = new Series({
pairs: this.curWindow
});
return {
//TODO: The way the index is figured out could have much better performance.
value: [this.whichIndex === WhichIndex.Start ? window.getIndex().first() : window.getIndex().last(), window],
done: false,
};
};
return SeriesRollingWindowIterator;
}());
//
var SeriesRollingWindowIterable = /** @class */ (function () {
function SeriesRollingWindowIterable(iterable, period, whichIndex) {
this.iterable = iterable;
this.period = period;
this.whichIndex = whichIndex;
}
SeriesRollingWindowIterable.prototype[Symbol.iterator] = function () {
return new SeriesRollingWindowIterator(this.iterable, this.period, this.whichIndex);
};
return SeriesRollingWindowIterable;
}());
//
var SeriesVariableWindowIterator = /** @class */ (function () {
function SeriesVariableWindowIterator(iterable, comparer) {
this.iterator = iterable[Symbol.iterator]();
this.nextValue = this.iterator.next();
this.comparer = comparer;
}
SeriesVariableWindowIterator.prototype.next = function () {
if (this.nextValue.done) {
// Nothing more to read.
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
var pairs = [
this.nextValue.value,
];
var prevValue = this.nextValue.value;
// Pull values until there is one that doesn't compare.
// eslint-disable-next-line no-constant-condition
while (true) {
this.nextValue = this.iterator.next();
if (this.nextValue.done) {
break; // No more values.
}
if (!this.comparer(prevValue[1], this.nextValue.value[1])) {
prevValue = this.nextValue.value;
break; // Doesn't compare. Start a new window.
}
pairs.push(this.nextValue.value);
prevValue = this.nextValue.value;
}
var window = new Series({
pairs: pairs,
});
return {
value: window,
done: false,
};
};
return SeriesVariableWindowIterator;
}());
//
var SeriesVariableWindowIterable = /** @class */ (function () {
function SeriesVariableWindowIterable(iterable, comparer) {
this.iterable = iterable;
this.comparer = comparer;
}
SeriesVariableWindowIterable.prototype[Symbol.iterator] = function () {
return new SeriesVariableWindowIterator(this.iterable, this.comparer);
};
return SeriesVariableWindowIterable;
}());
//
var Direction;
(function (Direction) {
Direction[Direction["Ascending"] = 0] = "Ascending";
Direction[Direction["Descending"] = 1] = "Descending";
})(Direction || (Direction = {}));
var SortOperation = /** @class */ (function () {
function SortOperation(values, sortSpec) {
this.values = values;
this.sortSpec = sortSpec;
this.keys = [];
}
SortOperation.prototype.genKeys = function () {
if (this.keys.length > 0) {
// Already cached.
return;
}
var index = 0;
try {
for (var _a = __values(this.values), _b = _a.next(); !_b.done; _b = _a.next()) {
var value = _b.value;
this.keys.push(this.sortSpec.selector(value, index));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
var e_1, _c;
};
SortOperation.prototype.compare = function (indexA, indexB) {
this.genKeys();
var keyA = this.keys[indexA];
var keyB = this.keys[indexB];
var comparison = -1;
if (keyA === keyB) {
comparison = 0;
}
else if (keyA > keyB) {
comparison = 1;
}
return (this.sortSpec.direction === Direction.Descending) ? -comparison : comparison;
};
return SortOperation;
}());
var OrderedIterable = /** @class */ (function () {
function OrderedIterable(iterable, sortSpec) {
this.iterable = iterable;
this.sortSpec = sortSpec;
}
OrderedIterable.prototype[Symbol.iterator] = function () {
var indexes = [];
var values = [];
var index = 0;
try {
for (var _a = __values(this.iterable), _b = _a.next(); !_b.done; _b = _a.next()) {
var value = _b.value;
indexes.push(index);
values.push(value);
++index;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_2) throw e_2.error; }
}
var sortOperations = [];
try {
for (var _d = __values(this.sortSpec), _e = _d.next(); !_e.done; _e = _d.next()) {
var sortSpec = _e.value;
sortOperations.push(new SortOperation(values, sortSpec));
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
}
finally { if (e_3) throw e_3.error; }
}
sortOperations[0].genKeys();
indexes.sort(function (indexA, indexB) {
try {
for (var sortOperations_1 = __values(sortOperations), sortOperations_1_1 = sortOperations_1.next(); !sortOperations_1_1.done; sortOperations_1_1 = sortOperations_1.next()) {
var sortOperation = sortOperations_1_1.value;
var comparison = sortOperation.compare(indexA, indexB);
if (comparison !== 0) {
return comparison;
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (sortOperations_1_1 && !sortOperations_1_1.done && (_a = sortOperations_1.return)) _a.call(sortOperations_1);
}
finally { if (e_4) throw e_4.error; }
}
return 0;
var e_4, _a;
});
var sortedValues = [];
try {
for (var indexes_1 = __values(indexes), indexes_1_1 = indexes_1.next(); !indexes_1_1.done; indexes_1_1 = indexes_1.next()) {
var index_1 = indexes_1_1.value;
sortedValues.push(values[index_1]);
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (indexes_1_1 && !indexes_1_1.done && (_g = indexes_1.return)) _g.call(indexes_1);
}
finally { if (e_5) throw e_5.error; }
}
return new ArrayIterator(sortedValues);
var e_2, _c, e_3, _f, e_5, _g;
};
return OrderedIterable;
}());
//
// An iterator to extact an element from an array.
//
var ExtractElementIterator = /** @class */ (function () {
function ExtractElementIterator(iterator, extractIndex) {
this.iterator = iterator;
this.extractIndex = extractIndex;
}
ExtractElementIterator.prototype.next = function () {
var result = this.iterator.next();
if (result.done) {
return result;
}
else {
return {
done: false,
value: result.value[this.extractIndex]
};
}
};
return ExtractElementIterator;
}());
//
var ExtractElementIterable = /** @class */ (function () {
function ExtractElementIterable(arrayIterable, extractIndex) {
this.arrayIterable = arrayIterable;
this.extractIndex = extractIndex;
}
ExtractElementIterable.prototype[Symbol.iterator] = function () {
var arrayIterator = this.arrayIterable[Symbol.iterator]();
return new ExtractElementIterator(arrayIterator, this.extractIndex);
};
return ExtractElementIterable;
}());
//
// An iterator that skips a number of values.
//
var SkipIterator = /** @class */ (function () {
function SkipIterator(iterator, numValues) {
this.iterator = iterator;
this.numValues = numValues;
}
SkipIterator.prototype.next = function () {
while (--this.numValues >= 0) {
var result = this.iterator.next();
if (result.done) {
return result;
}
}
return this.iterator.next();
};
return SkipIterator;
}());
//
var SkipIterable = /** @class */ (function () {
function SkipIterable(iterable, numValues) {
this.iterable = iterable;
this.numValues = numValues;
}
SkipIterable.prototype[Symbol.iterator] = function () {
var iterator = this.iterable[Symbol.iterator]();
return new SkipIterator(iterator, this.numValues);
};
return SkipIterable;
}());
//
// An iterator that skips a sequence of elements while a predicate function returns true.
//
var SkipWhileIterator = /** @class */ (function () {
function SkipWhileIterator(childIterator, predicate) {
this.doneSkipping = false;
this.childIterator = childIterator;
this.predicate = predicate;
}
SkipWhileIterator.prototype.next = function () {
// eslint-disable-next-line no-constant-condition
while (true) {
var result = this.childIterator.next();
if (result.done) {
return result; // Done.
}
if (!this.doneSkipping && this.predicate(result.value)) {
continue; // Skip it.
}
// It matches, stop skipping.
this.doneSkipping = true;
return result;
}
};
return SkipWhileIterator;
}());
//
var SkipWhileIterable = /** @class */ (function () {
function SkipWhileIterable(childIterable, predicate) {
this.childIterable = childIterable;
this.predicate = predicate;
}
SkipWhileIterable.prototype[Symbol.iterator] = function () {
var childIterator = this.childIterable[Symbol.iterator]();
return new SkipWhileIterator(childIterator, this.predicate);
};
return SkipWhileIterable;
}());
//
var DataFrameWindowIterator = /** @class */ (function () {
function DataFrameWindowIterator(columnNames, iterable, period) {
this.columnNames = columnNames;
this.iterable = iterable;
this.period = period;
}
DataFrameWindowIterator.prototype.next = function () {
if (!this.iterator) {
this.iterator = this.iterable[Symbol.iterator]();
}
var curWindow = [];
for (var i = 0; i < this.period; ++i) {
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator is finished.
break;
}
curWindow.push(curPos.value);
}
if (curWindow.length === 0) {
// Underlying iterator doesn't have required number of elements.
return { done: true };
}
var window = new DataFrame({
columnNames: this.columnNames,
pairs: curWindow
});
return {
value: window,
done: false,
};
};
return DataFrameWindowIterator;
}());
//
var DataFrameWindowIterable = /** @class */ (function () {
function DataFrameWindowIterable(columnNames, iterable, period) {
this.columnNames = columnNames;
this.iterable = iterable;
this.period = period;
}
DataFrameWindowIterable.prototype[Symbol.iterator] = function () {
return new DataFrameWindowIterator(this.columnNames, this.iterable, this.period);
};
return DataFrameWindowIterable;
}());
//
// An iterator that iterates the rows of a CSV file.
//
var CsvRowsIterator = /** @class */ (function () {
function CsvRowsIterator(columnNames, rowsIterable) {
this.index = 0;
this.columnNames = Array.from(columnNames);
this.rowsIterator = rowsIterable[Symbol.iterator]();
}
CsvRowsIterator.prototype.next = function () {
var result = this.rowsIterator.next();
if (result.done) {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
var row = result.value;
var value = {};
for (var cellIndex = 0; cellIndex < this.columnNames.length; ++cellIndex) {
var columnName = this.columnNames[cellIndex];
value[columnName] = row[cellIndex];
}
return {
done: false,
value: value,
};
};
return CsvRowsIterator;
}());
//
var CsvRowsIterable = /** @class */ (function () {
function CsvRowsIterable(columnNames, rows) {
this.columnNames = columnNames;
this.rows = rows;
}
CsvRowsIterable.prototype[Symbol.iterator] = function () {
return new CsvRowsIterator(this.columnNames, this.rows);
};
return CsvRowsIterable;
}());
//
var DataFrameRollingWindowIterator = /** @class */ (function () {
function DataFrameRollingWindowIterator(columnNames, iterable, period) {
this.columnNames = columnNames;
this.iterable = iterable;
this.period = period;
}
DataFrameRollingWindowIterator.prototype.next = function () {
if (!this.curWindow) {
this.curWindow = [];
this.iterator = this.iterable[Symbol.iterator]();
for (var i = 0; i < this.period; ++i) {
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator doesn't have required number of elements.
return { done: true };
}
this.curWindow.push(curPos.value);
}
}
else {
this.curWindow.shift(); // Remove first item from window.
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator doesn't have enough elements left.
return { done: true };
}
this.curWindow.push(curPos.value); // Add next item to window.
}
var window = new DataFrame({
columnNames: this.columnNames,
pairs: this.curWindow
});
return {
value: window,
done: false,
};
};
return DataFrameRollingWindowIterator;
}());
//
var DataFrameRollingWindowIterable = /** @class */ (function () {
function DataFrameRollingWindowIterable(columnNames, iterable, period) {
this.columnNames = columnNames;
this.iterable = iterable;
this.period = period;
}
DataFrameRollingWindowIterable.prototype[Symbol.iterator] = function () {
return new DataFrameRollingWindowIterator(this.columnNames, this.iterable, this.period);
};
return DataFrameRollingWindowIterable;
}());
//
var DataFrameVariableWindowIterator = /** @class */ (function () {
function DataFrameVariableWindowIterator(columnNames, iterable, comparer) {
this.columnNames = columnNames;
this.iterator = iterable[Symbol.iterator]();
this.nextValue = this.iterator.next();
this.comparer = comparer;
}
DataFrameVariableWindowIterator.prototype.next = function () {
if (this.nextValue.done) {
// Nothing more to read.
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
var pairs = [
this.nextValue.value,
];
var prevValue = this.nextValue.value;
// Pull values until there is one that doesn't compare.
// eslint-disable-next-line no-constant-condition
while (true) {
this.nextValue = this.iterator.next();
if (this.nextValue.done) {
break; // No more values.
}
if (!this.comparer(prevValue[1], this.nextValue.value[1])) {
prevValue = this.nextValue.value;
break; // Doesn't compare. Start a new window.
}
pairs.push(this.nextValue.value);
prevValue = this.nextValue.value;
}
var window = new DataFrame({
columnNames: this.columnNames,
pairs: pairs,
});
return {
value: window,
done: false,
};
};
return DataFrameVariableWindowIterator;
}());
//
var DataFrameVariableWindowIterable = /** @class */ (function () {
function DataFrameVariableWindowIterable(columnNames, iterable, comparer) {
this.columnNames = columnNames;
this.iterable = iterable;
this.comparer = comparer;
}
DataFrameVariableWindowIterable.prototype[Symbol.iterator] = function () {
return new DataFrameVariableWindowIterator(this.columnNames, this.iterable, this.comparer);
};
return DataFrameVariableWindowIterable;
}());
//
// An iterator that iterates the elements of an iterable multiple times.
// Implementation similar to https://numpy.org/doc/stable/reference/generated/numpy.repeat.html
//
var RepeatIterator = /** @class */ (function () {
function RepeatIterator(iterable, count) {
this.repetition = 0;
this.iterator = iterable[Symbol.iterator]();
this.count = count;
this.result = this.iterator.next();
}
RepeatIterator.prototype.next = function () {
if (this.count == 0) {
return { done: true };
}
if (this.repetition == this.count) {
this.result = this.iterator.next();
this.repetition = 0;
}
this.repetition += 1;
if (this.result.done) {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
return {
done: false,
value: this.result.value
};
};
return RepeatIterator;
}());
//
var RepeatIterable = /** @class */ (function () {
function RepeatIterable(iterable, count) {
this.iterable = iterable;
this.count = count;
}
RepeatIterable.prototype[Symbol.iterator] = function () {
return new RepeatIterator(this.iterable, this.count);
};
return RepeatIterable;
}());
//
// An iterator that iterates the elements of an iterable multiple times.
// Implementation similar to - https://numpy.org/doc/stable/reference/generated/numpy.tile.html
//
var TileIterator = /** @class */ (function () {
function TileIterator(iterable, count) {
this.count = 0;
this.repetition = 0;
this.firstIteration = true;
this.iterable = iterable;
this.iterator = iterable[Symbol.iterator]();
this.count = count;
}
TileIterator.prototype.next = function () {
var result = this.iterator.next();
// Return done for empty iterable
if (this.firstIteration && result.done) {
return { done: true };
}
this.firstIteration = false;
if (result.done) {
this.repetition += 1;
// Reinitialize iterator once iterated completely
this.iterator = this.iterable[Symbol.iterator]();
result = this.iterator.next();
}
if (this.repetition < this.count) {
return {
done: false,
value: result.value,
};
}
else {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
};
return TileIterator;
}());
//
var TileIterable = /** @class */ (function () {
function TileIterable(iterable, count) {
this.iterable = iterable;
this.count = count;
}
TileIterable.prototype[Symbol.iterator] = function () {
return new TileIterator(this.iterable, this.count);
};
return TileIterable;
}());
//
// An iterator that produces a contiguous flattened array generated from each set of elements in child iterables.
// Implementation similar to https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html with order 'C'
//
// Note that numpy does not work with arrays of varying lengths. However, the dataforge implementation ignores
// that requirement due to the iterator architecture. Moreover, since the RavelIterator is only used internally
// such cases will not occur.
//
var RavelIterator = /** @class */ (function () {
function RavelIterator(iterables) {
this.iteratorIndex = 0;
this.iterators = iterables.map(function (iterable) { return iterable[Symbol.iterator](); });
}
RavelIterator.prototype.next = function () {
if (this.iterators.length > 0) {
var result = this.iterators[this.iteratorIndex].next();
while (result.done) {
this.iteratorIndex += 1;
if (this.iteratorIndex < this.iterators.length) {
result = this.iterators[this.iteratorIndex].next();
}
else {
// https://github.com/Microsoft/TypeScript/issues/8938
return { done: true }; // <= explicit cast here!;
}
}
return {
done: false,
value: result.value,
};
}
// Return done if empty array passed
return { done: true };
};
return RavelIterator;
}());
//
var RavelIterable = /** @class */ (function () {
function RavelIterable(iterables) {
this.iterables = iterables;
}
RavelIterable.prototype[Symbol.iterator] = function () {
return new RavelIterator(this.iterables);
};
return RavelIterable;
}());
//
var ColumnNamesIterator = /** @class */ (function () {
function ColumnNamesIterator(values, considerAllRows) {
this.columnNamesIterator = null;
this.values = values;
this.considerAllRows = considerAllRows;
}
ColumnNamesIterator.prototype.next = function () {
if (this.columnNamesIterator === null) {
if (this.considerAllRows) {
var combinedFields = {};
try {
// Check all items.
for (var _a = __values(this.values), _b = _a.next(); !_b.done; _b = _a.next()) {
var value = _b.value;
try {
for (var _c = __values(Object.keys(value)), _d = _c.next(); !_d.done; _d = _c.next()) {
var fieldName = _d.value;
combinedFields[fieldName] = true;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_d && !_d.done && (_e = _c.return)) _e.call(_c);
}
finally { if (e_1) throw e_1.error; }
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
}
finally { if (e_2) throw e_2.error; }
}
this.columnNamesIterator = new ArrayIterator(Object.keys(combinedFields));
}
else {
// Just check the first item.
var valuesIterator = this.values[Symbol.iterator]();
var firstResult = valuesIterator.next();
if (firstResult.done) {
return {
done: true,
value: "",
};
}
this.columnNamesIterator = new ArrayIterator(Object.keys(firstResult.value));
}
}
return this.columnNamesIterator.next();
var e_2, _f, e_1, _e;
};
return ColumnNamesIterator;
}());
//
var ColumnNamesIterable = /** @class */ (function () {
function ColumnNamesIterable(values, considerAllRows) {
this.values = values;
this.considerAllRows = considerAllRows;
}
ColumnNamesIterable.prototype[Symbol.iterator] = function () {
return new ColumnNamesIterator(this.values, this.considerAllRows);
};
return ColumnNamesIterable;
}());
//
// Helper function to only return distinct items.
//
function makeDistinct(items, selector) {
var set = {};
var output = [];
try {
for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
var item = items_1_1.value;
var key = selector && selector(item) || item;
if (!set[key]) {
// Haven't yet seen this key.
set[key] = true;
output.push(item);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
}
finally { if (e_1) throw e_1.error; }
}
return output;
var e_1, _a;
}
//
// Helper function to map an array of objects.
//
function toMap(items, keySelector, valueSelector) {
var output = {};
try {
for (var items_2 = __values(items), items_2_1 = items_2.next(); !items_2_1.done; items_2_1 = items_2.next()) {
var item = items_2_1.value;
var key = keySelector(item);
output[key] = valueSelector(item);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (items_2_1 && !items_2_1.done && (_a = items_2.return)) _a.call(items_2);
}
finally { if (e_2) throw e_2.error; }
}
return output;
var e_2, _a;
}
//
// Helper function to map an array of objects.
//
function toMap2(items, keySelector, valueSelector) {
var output = new Map();
try {
for (var items_3 = __values(items), items_3_1 = items_3.next(); !items_3_1.done; items_3_1 = items_3.next()) {
var item = items_3_1.value;
output.set(keySelector(item), valueSelector(item));
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (items_3_1 && !items_3_1.done && (_a = items_3.return)) _a.call(items_3);
}
finally { if (e_3) throw e_3.error; }
}
return output;
var e_3, _a;
}
//
// Determine the type of a value.
//
function determineType(value) {
if (value === undefined) {
return "undefined";
}
else if (isNumber(value)) {
return "number";
}
else if (isString(value)) {
return "string";
}
else if (value instanceof Date) {
return "date";
}
else if (isBoolean(value)) {
return "boolean";
}
else {
return "unsupported";
}
}
function isObject(v) {
return t(v).isObject && !isDate(v);
}
fu