instagram-graph-api
Version:
A library to help perform requests to the Instagram Graph API.
216 lines (215 loc) • 6.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComplexMetric = void 0;
const AbstractMetric_1 = require("./AbstractMetric");
/**
* Class to represent a complex metric.
*
* As a metric class, it expects to receive an array of values.
* However, for complex metrics that array has a single element.
* This class assumes that to always be true to simplify the access the values.
*
* @author Tiago Grosso <tiagogrosso99@gmail.com>
* @since 0.1.0
*/
class ComplexMetric extends AbstractMetric_1.AbstractMetric {
/**
* The constructor.
*
* @param data the metric data.
*/
constructor(data) {
super(data);
this.value = this.metricData.values[0].value;
this.endTime = new Date(this.metricData.values[0].end_time);
}
/**
* Gets the value of the metric.
*
* @returns the value of the metric.
*/
getValue() {
return this.value;
}
/**
* Gets the end time of the metric value.
*
* @returns the end time of the metric value.
*/
getEndTime() {
return this.endTime;
}
/**
* Gets the keys that match the provided expression.
*
* @param expression expression to match the values to.
*
* @returns the keys that match the provided expression.
*/
getKeysByExpression(expression) {
return Object.entries(this.value)
.filter(expression)
.map(([key]) => key);
}
/**
* Gets the keys of the values that are greater than the provided limit.
*
* @param limit the limit.
*
* @returns the keys of the values that are greater than the provided limit.
*/
getKeysByGreaterThan(limit) {
return this.getKeysByExpression(([, value]) => value > limit);
}
/**
* Gets the keys of the values that are greater than or equal to the provided limit.
*
* @param limit the limit.
*
* @returns the keys of the values that are greater than or equal to the provided limit.
*/
getKeysByGreaterThanOrEqualTo(limit) {
return this.getKeysByExpression(([, value]) => value >= limit);
}
/**
* Gets the keys of the values that are smaller than to the provided limit.
*
* @param limit the limit.
*
* @returns the keys of the values that are smaller than to the provided limit.
*/
getKeysByLessThan(limit) {
return this.getKeysByExpression(([, value]) => value < limit);
}
/**
* Gets the keys of the values that are smaller than or equal to the provided limit.
*
* @param limit the limit.
*
* @returns the keys of the values that are smaller than or equal to the provided limit.
*/
getKeysByLessThanOrEqualTo(limit) {
return this.getKeysByExpression(([, value]) => value <= limit);
}
/**
* Gets the keys of the values that are equal to the provided value.
*
* @param valueToCompare the value to compare to.
*
* @returns the keys of the values that are equal to the provided value.
*/
getKeysByEqualTo(valueToCompare) {
return this.getKeysByExpression(([, value]) => value === valueToCompare);
}
/**
* Gets the entries that match the provided expression.
*
* @param expression expression to match the values to.
*
* @returns the entries that match the provided expression.
*/
getByExpression(expression) {
const filtered = {};
Object.entries(this.value)
.filter(expression)
.forEach(([key, value]) => (filtered[key] = value));
return filtered;
}
/**
* Gets the entries of the values that are greater than the provided limit.
*
* @param limit the limit.
*
* @returns the entries of the values that are greater than the provided limit.
*/
getByGreaterThan(limit) {
return this.getByExpression(([, value]) => value > limit);
}
/**
* Gets the entries of the values that are greater than or equal to the provided limit.
*
* @param limit the limit.
*
* @returns the entries of the values that are greater than or equal to the provided limit.
*/
getByGreaterThanOrEqualTo(limit) {
return this.getByExpression(([, value]) => value >= limit);
}
/**
* Gets the entries of the values that are equal than the provided limit.
*
* @param limit the limit.
*
* @returns the entries of the values that are equal than the provided limit.
*/
getByLessThan(limit) {
return this.getByExpression(([, value]) => value < limit);
}
/**
* Gets the entries of the values that are greater than or equal to the provided limit.
*
* @param limit the limit.
*
* @returns the entries of the values that are greater than or equal to the provided limit.
*/
getByLessThanOrEqualTo(limit) {
return this.getByExpression(([, value]) => value <= limit);
}
/**
* Gets the entries of the provided keys.
*
* @param limit the limit.
*
* @returns the entries of the provided keys.
*/
getByKeys(...keys) {
return this.getByExpression(([key]) => keys.includes(key));
}
/**
* Returns the highest value of the metric. In case of a tie, the first value found is returned.
*
* @returns the highest value of the metric.
*/
getHighest() {
let highest;
Object.entries(this.value).forEach(([key, value]) => {
if (highest == undefined || value > highest.value) {
highest = { key: key, value: value };
}
});
return highest;
}
/**
* Returns the key of the highest value of the metric. In case of a tie, the key of the first value found is returned.
*
* @returns the key of highest value of the metric.
*/
getHighestKey() {
var _a;
return (_a = this.getHighest()) === null || _a === void 0 ? void 0 : _a.key;
}
/**
* Returns the highest value of the metric. In case of a tie, the first value found is returned.
*
* @returns the highest value of the metric.
*/
getLowest() {
let lowest;
Object.entries(this.value).forEach(([key, value]) => {
if (lowest == undefined || value < lowest.value) {
lowest = { key: key, value: value };
}
});
return lowest;
}
/**
* Returns the key of the lowest value of the metric. In case of a tie, the key of the first value found is returned.
*
* @returns the key of lowest value of the metric.
*/
getLowestKey() {
var _a;
return (_a = this.getLowest()) === null || _a === void 0 ? void 0 : _a.key;
}
}
exports.ComplexMetric = ComplexMetric;