@flourish/interpreter
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -5,3 +5,3 @@ (function (global, factory) { | ||
(global = global || self, global.createInterpreter = factory()); | ||
}(this, function () { 'use strict'; | ||
}(this, (function () { 'use strict'; | ||
@@ -14,6 +14,8 @@ var t0 = new Date, | ||
function interval(date) { | ||
return floori(date = new Date(+date)), date; | ||
return floori(date = arguments.length === 0 ? new Date : new Date(+date)), date; | ||
} | ||
interval.floor = interval; | ||
interval.floor = function(date) { | ||
return floori(date = new Date(+date)), date; | ||
}; | ||
@@ -78,60 +80,6 @@ interval.ceil = function(date) { | ||
var millisecond = newInterval(function() { | ||
// noop | ||
}, function(date, step) { | ||
date.setTime(+date + step); | ||
}, function(start, end) { | ||
return end - start; | ||
}); | ||
// An optimized implementation for this simple case. | ||
millisecond.every = function(k) { | ||
k = Math.floor(k); | ||
if (!isFinite(k) || !(k > 0)) return null; | ||
if (!(k > 1)) return millisecond; | ||
return newInterval(function(date) { | ||
date.setTime(Math.floor(date / k) * k); | ||
}, function(date, step) { | ||
date.setTime(+date + step * k); | ||
}, function(start, end) { | ||
return (end - start) / k; | ||
}); | ||
}; | ||
var durationSecond = 1e3; | ||
var durationMinute = 6e4; | ||
var durationHour = 36e5; | ||
var durationDay = 864e5; | ||
var durationWeek = 6048e5; | ||
var second = newInterval(function(date) { | ||
date.setTime(date - date.getMilliseconds()); | ||
}, function(date, step) { | ||
date.setTime(+date + step * durationSecond); | ||
}, function(start, end) { | ||
return (end - start) / durationSecond; | ||
}, function(date) { | ||
return date.getUTCSeconds(); | ||
}); | ||
var minute = newInterval(function(date) { | ||
date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond); | ||
}, function(date, step) { | ||
date.setTime(+date + step * durationMinute); | ||
}, function(start, end) { | ||
return (end - start) / durationMinute; | ||
}, function(date) { | ||
return date.getMinutes(); | ||
}); | ||
var hour = newInterval(function(date) { | ||
date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond - date.getMinutes() * durationMinute); | ||
}, function(date, step) { | ||
date.setTime(+date + step * durationHour); | ||
}, function(start, end) { | ||
return (end - start) / durationHour; | ||
}, function(date) { | ||
return date.getHours(); | ||
}); | ||
var day = newInterval(function(date) { | ||
@@ -166,13 +114,2 @@ date.setHours(0, 0, 0, 0); | ||
var month = newInterval(function(date) { | ||
date.setDate(1); | ||
date.setHours(0, 0, 0, 0); | ||
}, function(date, step) { | ||
date.setMonth(date.getMonth() + step); | ||
}, function(start, end) { | ||
return end.getMonth() - start.getMonth() + (end.getFullYear() - start.getFullYear()) * 12; | ||
}, function(date) { | ||
return date.getMonth(); | ||
}); | ||
var year = newInterval(function(date) { | ||
@@ -200,22 +137,2 @@ date.setMonth(0, 1); | ||
var utcMinute = newInterval(function(date) { | ||
date.setUTCSeconds(0, 0); | ||
}, function(date, step) { | ||
date.setTime(+date + step * durationMinute); | ||
}, function(start, end) { | ||
return (end - start) / durationMinute; | ||
}, function(date) { | ||
return date.getUTCMinutes(); | ||
}); | ||
var utcHour = newInterval(function(date) { | ||
date.setUTCMinutes(0, 0, 0); | ||
}, function(date, step) { | ||
date.setTime(+date + step * durationHour); | ||
}, function(start, end) { | ||
return (end - start) / durationHour; | ||
}, function(date) { | ||
return date.getUTCHours(); | ||
}); | ||
var utcDay = newInterval(function(date) { | ||
@@ -250,13 +167,2 @@ date.setUTCHours(0, 0, 0, 0); | ||
var utcMonth = newInterval(function(date) { | ||
date.setUTCDate(1); | ||
date.setUTCHours(0, 0, 0, 0); | ||
}, function(date, step) { | ||
date.setUTCMonth(date.getUTCMonth() + step); | ||
}, function(start, end) { | ||
return end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12; | ||
}, function(date) { | ||
return date.getUTCMonth(); | ||
}); | ||
var utcYear = newInterval(function(date) { | ||
@@ -302,4 +208,4 @@ date.setUTCMonth(0, 1); | ||
function newYear(y) { | ||
return {y: y, m: 0, d: 1, H: 0, M: 0, S: 0, L: 0}; | ||
function newDate(y, m, d) { | ||
return {y: y, m: m, d: d, H: 0, M: 0, S: 0, L: 0}; | ||
} | ||
@@ -344,2 +250,3 @@ | ||
"p": formatPeriod, | ||
"q": formatQuarter, | ||
"Q": formatUnixTimestamp, | ||
@@ -377,2 +284,3 @@ "s": formatUnixTimestampSeconds, | ||
"p": formatUTCPeriod, | ||
"q": formatUTCQuarter, | ||
"Q": formatUnixTimestamp, | ||
@@ -410,2 +318,3 @@ "s": formatUnixTimestampSeconds, | ||
"p": parsePeriod, | ||
"q": parseQuarter, | ||
"Q": parseUnixTimestamp, | ||
@@ -463,5 +372,5 @@ "s": parseUnixTimestampSeconds, | ||
function newParse(specifier, newDate) { | ||
function newParse(specifier, Z) { | ||
return function(string) { | ||
var d = newYear(1900), | ||
var d = newDate(1900, undefined, 1), | ||
i = parseSpecifier(d, specifier, string += "", 0), | ||
@@ -473,6 +382,13 @@ week, day$1; | ||
if ("Q" in d) return new Date(d.Q); | ||
if ("s" in d) return new Date(d.s * 1000 + ("L" in d ? d.L : 0)); | ||
// If this is utcParse, never use the local timezone. | ||
if (Z && !("Z" in d)) d.Z = 0; | ||
// The am-pm flag is 0 for AM, and 1 for PM. | ||
if ("p" in d) d.H = d.H % 12 + d.p * 12; | ||
// If the month was not specified, inherit from the quarter. | ||
if (d.m === undefined) d.m = "q" in d ? d.q : 0; | ||
// Convert day-of-week and week-of-year to day-of-year. | ||
@@ -483,3 +399,3 @@ if ("V" in d) { | ||
if ("Z" in d) { | ||
week = utcDate(newYear(d.y)), day$1 = week.getUTCDay(); | ||
week = utcDate(newDate(d.y, 0, 1)), day$1 = week.getUTCDay(); | ||
week = day$1 > 4 || day$1 === 0 ? utcMonday.ceil(week) : utcMonday(week); | ||
@@ -491,3 +407,3 @@ week = utcDay.offset(week, (d.V - 1) * 7); | ||
} else { | ||
week = newDate(newYear(d.y)), day$1 = week.getDay(); | ||
week = localDate(newDate(d.y, 0, 1)), day$1 = week.getDay(); | ||
week = day$1 > 4 || day$1 === 0 ? monday.ceil(week) : monday(week); | ||
@@ -501,3 +417,3 @@ week = day.offset(week, (d.V - 1) * 7); | ||
if (!("w" in d)) d.w = "u" in d ? d.u % 7 : "W" in d ? 1 : 0; | ||
day$1 = "Z" in d ? utcDate(newYear(d.y)).getUTCDay() : newDate(newYear(d.y)).getDay(); | ||
day$1 = "Z" in d ? utcDate(newDate(d.y, 0, 1)).getUTCDay() : localDate(newDate(d.y, 0, 1)).getDay(); | ||
d.m = 0; | ||
@@ -516,3 +432,3 @@ d.d = "W" in d ? (d.w + 6) % 7 + d.W * 7 - (day$1 + 5) % 7 : d.w + d.U * 7 - (day$1 + 6) % 7; | ||
// Otherwise, all fields are in local time. | ||
return newDate(d); | ||
return localDate(d); | ||
}; | ||
@@ -600,2 +516,6 @@ } | ||
function formatQuarter(d) { | ||
return 1 + ~~(d.getMonth() / 3); | ||
} | ||
function formatUTCShortWeekday(d) { | ||
@@ -621,2 +541,6 @@ return locale_shortWeekdays[d.getUTCDay()]; | ||
function formatUTCQuarter(d) { | ||
return 1 + ~~(d.getUTCMonth() / 3); | ||
} | ||
return { | ||
@@ -629,3 +553,3 @@ format: function(specifier) { | ||
parse: function(specifier) { | ||
var p = newParse(specifier += "", localDate); | ||
var p = newParse(specifier += "", false); | ||
p.toString = function() { return specifier; }; | ||
@@ -640,3 +564,3 @@ return p; | ||
utcParse: function(specifier) { | ||
var p = newParse(specifier, utcDate); | ||
var p = newParse(specifier += "", true); | ||
p.toString = function() { return specifier; }; | ||
@@ -714,2 +638,7 @@ return p; | ||
function parseQuarter(d, string, i) { | ||
var n = numberRe.exec(string.slice(i, i + 1)); | ||
return n ? (d.q = n[0] * 3 - 3, i + n[0].length) : -1; | ||
} | ||
function parseMonthNumber(d, string, i) { | ||
@@ -767,3 +696,3 @@ var n = numberRe.exec(string.slice(i, i + 2)); | ||
var n = numberRe.exec(string.slice(i)); | ||
return n ? (d.Q = (+n[0]) * 1000, i + n[0].length) : -1; | ||
return n ? (d.s = +n[0], i + n[0].length) : -1; | ||
} | ||
@@ -813,3 +742,3 @@ | ||
function formatWeekNumberSunday(d, p) { | ||
return pad(sunday.count(year(d), d), p, 2); | ||
return pad(sunday.count(year(d) - 1, d), p, 2); | ||
} | ||
@@ -828,3 +757,3 @@ | ||
function formatWeekNumberMonday(d, p) { | ||
return pad(monday.count(year(d), d), p, 2); | ||
return pad(monday.count(year(d) - 1, d), p, 2); | ||
} | ||
@@ -889,3 +818,3 @@ | ||
function formatUTCWeekNumberSunday(d, p) { | ||
return pad(utcSunday.count(utcYear(d), d), p, 2); | ||
return pad(utcSunday.count(utcYear(d) - 1, d), p, 2); | ||
} | ||
@@ -904,3 +833,3 @@ | ||
function formatUTCWeekNumberMonday(d, p) { | ||
return pad(utcMonday.count(utcYear(d), d), p, 2); | ||
return pad(utcMonday.count(utcYear(d) - 1, d), p, 2); | ||
} | ||
@@ -958,21 +887,2 @@ | ||
var isoSpecifier = "%Y-%m-%dT%H:%M:%S.%LZ"; | ||
function formatIsoNative(date) { | ||
return date.toISOString(); | ||
} | ||
var formatIso = Date.prototype.toISOString | ||
? formatIsoNative | ||
: utcFormat(isoSpecifier); | ||
function parseIsoNative(string) { | ||
var date = new Date(string); | ||
return isNaN(date) ? null : date; | ||
} | ||
var parseIso = +new Date("2000-01-01T00:00:00.000Z") | ||
? parseIsoNative | ||
: utcParse(isoSpecifier); | ||
function notAStringError(not_a_str) { | ||
@@ -1200,3 +1110,3 @@ throw new TypeError("Expected a value of type string but got a value of type " + (typeof not_a_str)); | ||
var include_string = interpretations_array.indexOf("string" !== -1); | ||
var include_string = interpretations_array.indexOf("string") !== -1; | ||
@@ -1264,2 +1174,2 @@ var options = OPTION_KEYS.reduce(function(obj, key) { | ||
})); | ||
}))); |
{ | ||
"name": "@flourish/interpreter", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Does a best guess at the type of data supplied", | ||
@@ -22,13 +22,13 @@ "main": "interpreter.js", | ||
"dependencies": { | ||
"d3-time-format": "^2.0.5" | ||
"d3-time-format": "^2.2.1" | ||
}, | ||
"devDependencies": { | ||
"@flourish/eslint-plugin-flourish": "^0.7.2", | ||
"chai": "^4.2.0", | ||
"@flourish/eslint-plugin-flourish": "^0.7.2", | ||
"eslint": "^6.4.0", | ||
"mocha": "^6.2.0", | ||
"rollup": "^1.21.4", | ||
"eslint": "^6.5.1", | ||
"mocha": "^6.2.2", | ||
"pre-commit": "^1.2.2", | ||
"rollup": "^1.25.2", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"pre-commit": "^1.2.2", | ||
"uglify-js": "^2.7.5" | ||
"uglify-js": "^3.6.4" | ||
}, | ||
@@ -35,0 +35,0 @@ "precommit": [ |
@@ -0,1 +1,4 @@ | ||
# 2.0.1 | ||
* Fix bug with including string interpreter | ||
# 2.0.0 | ||
@@ -2,0 +5,0 @@ * `parse` and `test` functions throw error if argument is not a string |
@@ -55,3 +55,3 @@ import { datetime_interpretations } from "./types/datetime"; | ||
var include_string = interpretations_array.indexOf("string" !== -1); | ||
var include_string = interpretations_array.indexOf("string") !== -1; | ||
@@ -58,0 +58,0 @@ var options = OPTION_KEYS.reduce(function(obj, key) { |
57645
1342
Updatedd3-time-format@^2.2.1