MCPcopy Create free account
hub / github.com/microsoft/SandDance / newInterval

Function newInterval

docs/tests/v2/es6/js/sanddance.js:81155–81224  ·  view source on GitHub ↗
(floori, offseti, count, field)

Source from the content-addressed store, hash-verified

81153 t1 = new Date();
81154
81155function newInterval(floori, offseti, count, field) {
81156 function interval(date) {
81157 return floori(date = arguments.length === 0 ? new Date() : new Date(+date)), date;
81158 }
81159
81160 interval.floor = function (date) {
81161 return floori(date = new Date(+date)), date;
81162 };
81163
81164 interval.ceil = function (date) {
81165 return floori(date = new Date(date - 1)), offseti(date, 1), floori(date), date;
81166 };
81167
81168 interval.round = function (date) {
81169 var d0 = interval(date),
81170 d1 = interval.ceil(date);
81171 return date - d0 < d1 - date ? d0 : d1;
81172 };
81173
81174 interval.offset = function (date, step) {
81175 return offseti(date = new Date(+date), step == null ? 1 : Math.floor(step)), date;
81176 };
81177
81178 interval.range = function (start, stop, step) {
81179 var range = [],
81180 previous;
81181 start = interval.ceil(start);
81182 step = step == null ? 1 : Math.floor(step);
81183 if (!(start < stop) || !(step > 0)) return range; // also handles Invalid Date
81184
81185 do range.push(previous = new Date(+start)), offseti(start, step), floori(start); while (previous < start && start < stop);
81186
81187 return range;
81188 };
81189
81190 interval.filter = function (test) {
81191 return newInterval(function (date) {
81192 if (date >= date) while (floori(date), !test(date)) date.setTime(date - 1);
81193 }, function (date, step) {
81194 if (date >= date) {
81195 if (step < 0) while (++step <= 0) {
81196 while (offseti(date, -1), !test(date)) {} // eslint-disable-line no-empty
81197
81198 } else while (--step >= 0) {
81199 while (offseti(date, +1), !test(date)) {} // eslint-disable-line no-empty
81200
81201 }
81202 }
81203 });
81204 };
81205
81206 if (count) {
81207 interval.count = function (start, end) {
81208 t0.setTime(+start), t1.setTime(+end);
81209 floori(t0), floori(t1);
81210 return Math.floor(count(t0, t1));
81211 };
81212

Callers

nothing calls this directly

Calls 5

intervalFunction · 0.70
countFunction · 0.70
testFunction · 0.50
fieldFunction · 0.50
filterMethod · 0.45

Tested by

no test coverage detected