UNPKG

@tidyjs/tidy

Version:

Tidy up your data with JavaScript, inspired by dplyr and the tidyverse

71 lines (66 loc) 2.8 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var d3Array = require('d3-array'); function vectorSeq(values, period = 1) { let [min, max] = d3Array.extent(values); const sequence = []; let value = min; while (value <= max) { sequence.push(value); value += period; } return sequence; } function vectorSeqDate(values, granularity = "day", period = 1) { let [min, max] = d3Array.extent(values); const sequence = []; let value = new Date(min); while (value <= max) { sequence.push(new Date(value)); if (granularity === "second" || granularity === "s" || granularity === "seconds") { value.setUTCSeconds(value.getUTCSeconds() + 1 * period); } else if (granularity === "minute" || granularity === "min" || granularity === "minutes") { value.setUTCMinutes(value.getUTCMinutes() + 1 * period); } else if (granularity === "day" || granularity === "d" || granularity === "days") { value.setUTCDate(value.getUTCDate() + 1 * period); } else if (granularity === "week" || granularity === "w" || granularity === "weeks") { value.setUTCDate(value.getUTCDate() + 7 * period); } else if (granularity === "month" || granularity === "m" || granularity === "months") { value.setUTCMonth(value.getUTCMonth() + 1 * period); } else if (granularity === "year" || granularity === "y" || granularity === "years") { value.setUTCFullYear(value.getUTCFullYear() + 1 * period); } else { throw new Error("Invalid granularity for date sequence: " + granularity); } } return sequence; } function fullSeq(key, period) { return function fullSeqInner(items) { period = period != null ? period : 1; const keyFn = typeof key === "function" ? key : (d) => d[key]; return vectorSeq(items.map(keyFn), period); }; } function fullSeqDate(key, granularity, period) { return function fullSeqDateInner(items) { granularity = granularity != null ? granularity : "day"; period = period != null ? period : 1; const keyFn = typeof key === "function" ? key : (d) => d[key]; return vectorSeqDate(items.map(keyFn), granularity, period); }; } function fullSeqDateISOString(key, granularity, period) { return function fullSeqDateISOStringInner(items) { granularity = granularity != null ? granularity : "day"; period = period != null ? period : 1; const keyFn = typeof key === "function" ? key : (d) => d[key]; return vectorSeqDate(items.map((d) => new Date(keyFn(d))), granularity, period).map((date) => date.toISOString()); }; } exports.fullSeq = fullSeq; exports.fullSeqDate = fullSeqDate; exports.fullSeqDateISOString = fullSeqDateISOString; exports.vectorSeq = vectorSeq; exports.vectorSeqDate = vectorSeqDate; //# sourceMappingURL=fullSeq.js.map