@showr/core
Version:
Core library for Showr
98 lines (97 loc) • 2.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dataset = void 0;
const _1 = require("./");
/**
* Creates a dataset out of data, where data is an array of any numeric values.
*/
class Dataset {
_value;
/**
* Creates a dataset after type-casting given data values to quotes.
* @param data - Array of `any` type of values or `Quote`.
* @param [symbol] - If provided, each array item will be converted into a { key: value } pair where `key` would be a given symbol.
*/
constructor(data) {
if (data) {
this._value = data.map((d) => {
if (d instanceof _1.Quote) {
return d;
}
return new _1.Quote(d);
});
}
else {
this._value = [];
}
}
get value() {
return this._value.map((q) => q.value);
}
get quotes() {
return this._value.map((q) => q);
}
/**
* Get quote at the given zero based position
* @param position - number, where 0 is first index, and -1 is the last index.
* @returns - `Quote` if found or undefined.
*/
at(position) {
if (position < 0) {
return this.quotes[this._value.length + position];
}
else {
return this.quotes[position];
}
}
/**
* Adds a given quote to the end of the dataset.
* @param quote - `Quote`.
* @returns self reference.
*/
add(quote) {
this._value.push(quote);
return this;
}
/**
* Updates the last quote of the dataset with the given quote.
* @param quote - `Quote`.
* @returns self reference.
*/
update(quote) {
this._value[this._value.length - 1] = quote;
return this;
}
/**
* Applies given indicators to every quotes of the dataset.
* @param indicators - Array of `Indicator`.
* @returns self reference.
*/
apply(...indicators) {
indicators.forEach((i) => {
i.spread(this);
});
return this;
}
/**
* Flatten quote over an attribute
* @param attribute - The attribute.
* @returns flatten array.
*/
flatten(attribute) {
return this.quotes.map((q) => attribute ? q.getAttribute(attribute) : q.value);
}
/**
* get Value of the quote at the given position
* @param position - number, where 0 is first index, and -1 is the last index.
* @param attribute - Value of a specific attribute if any (Optional)
* @returns value.
*/
valueAt(position, attribute) {
const relativePosition = position < 0 ? this._value.length + position : position;
return attribute
? this.quotes[relativePosition].getAttribute(attribute)
: this.quotes[relativePosition].value;
}
}
exports.Dataset = Dataset;