import dateTimeDrill from './dateTimeDrill'; import dateTimeFeature from './dateTimeFeature'; function bind(a: any[], b: any[]) { return a.map((v, i) => [v, b[i]]); } const getParams = (value: T) => [ { type: 'field', value: 'date', } as const, { type: 'value', value, } as const, { type: 'displayOffset', value: 0, } as const, ]; const date = [ '2010-01-01T07:41:08.000Z', '2010-04-22T03:21:58.000Z', '2010-12-31T15:38:24.000Z', '2011-01-01T08:55:46.000Z', '2011-08-15T14:23:12.000Z', '2011-12-31T04:45:38.000Z', '2012-01-01T21:53:07.000Z', '2012-02-20T14:33:22.000Z', '2012-12-31T01:49:05.000Z', '2013-01-01T17:06:33.000Z', '2013-10-05T02:11:42.000Z', '2013-12-31T12:42:53.000Z', '2014-01-01T13:45:49.000Z', '2014-07-04T08:33:12.000Z', '2014-12-30T16:39:56.000Z', '2015-01-01T20:09:42.000Z', '2015-11-11T18:25:08.000Z', '2015-12-31T06:45:31.000Z', '2016-01-01T11:33:45.000Z', '2016-09-09T15:01:40.000Z', '2016-12-31T04:03:11.000Z', '2017-01-01T13:22:04.000Z', '2017-06-03T09:38:09.000Z', '2017-12-31T12:14:58.000Z', '2018-01-01T22:54:44.000Z', '2018-05-05T17:43:32.000Z', '2018-12-31T04:45:25.000Z', ]; // export const DATE_TIME_DRILL_LEVELS = [ // "year", "quarter", "month", "week", "day", "hour", "minute", "second", 'iso_year', 'iso_week', // ] as const; describe('drill', () => { test('year', () => { const { result } = dateTimeDrill('result', getParams('year'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T00:00:00.000Z', '2010-01-01T00:00:00.000Z', '2010-01-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', ]) ); }); test('quarter', () => { const { result } = dateTimeDrill( 'result', getParams('quarter'), { date } ); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T00:00:00.000Z', '2010-04-01T00:00:00.000Z', '2010-10-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-07-01T00:00:00.000Z', '2011-10-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-10-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-10-01T00:00:00.000Z', '2013-10-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-07-01T00:00:00.000Z', '2014-10-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-10-01T00:00:00.000Z', '2015-10-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-07-01T00:00:00.000Z', '2016-10-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-04-01T00:00:00.000Z', '2017-10-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-04-01T00:00:00.000Z', '2018-10-01T00:00:00.000Z', ]) ); }); test('month', () => { const { result } = dateTimeDrill('result', getParams('month'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T00:00:00.000Z', '2010-04-01T00:00:00.000Z', '2010-12-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-08-01T00:00:00.000Z', '2011-12-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-02-01T00:00:00.000Z', '2012-12-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-10-01T00:00:00.000Z', '2013-12-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-07-01T00:00:00.000Z', '2014-12-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-11-01T00:00:00.000Z', '2015-12-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-09-01T00:00:00.000Z', '2016-12-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-06-01T00:00:00.000Z', '2017-12-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-05-01T00:00:00.000Z', '2018-12-01T00:00:00.000Z', ]) ); }); test('week', () => { const { result } = dateTimeDrill('result', getParams('week'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2009-12-27T00:00:00.000Z', '2010-04-18T00:00:00.000Z', '2010-12-26T00:00:00.000Z', '2010-12-26T00:00:00.000Z', '2011-08-14T00:00:00.000Z', '2011-12-25T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-02-19T00:00:00.000Z', '2012-12-30T00:00:00.000Z', '2012-12-30T00:00:00.000Z', '2013-09-29T00:00:00.000Z', '2013-12-29T00:00:00.000Z', '2013-12-29T00:00:00.000Z', '2014-06-29T00:00:00.000Z', '2014-12-28T00:00:00.000Z', '2014-12-28T00:00:00.000Z', '2015-11-08T00:00:00.000Z', '2015-12-27T00:00:00.000Z', '2015-12-27T00:00:00.000Z', '2016-09-04T00:00:00.000Z', '2016-12-25T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-05-28T00:00:00.000Z', '2017-12-31T00:00:00.000Z', '2017-12-31T00:00:00.000Z', '2018-04-29T00:00:00.000Z', '2018-12-30T00:00:00.000Z', ]) ); }); test('day', () => { const { result } = dateTimeDrill('result', getParams('day'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T00:00:00.000Z', '2010-04-22T00:00:00.000Z', '2010-12-31T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-08-15T00:00:00.000Z', '2011-12-31T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2012-02-20T00:00:00.000Z', '2012-12-31T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-10-05T00:00:00.000Z', '2013-12-31T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-07-04T00:00:00.000Z', '2014-12-30T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-11-11T00:00:00.000Z', '2015-12-31T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-09-09T00:00:00.000Z', '2016-12-31T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-06-03T00:00:00.000Z', '2017-12-31T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-05-05T00:00:00.000Z', '2018-12-31T00:00:00.000Z', ]) ); }); test('hour', () => { const { result } = dateTimeDrill('result', getParams('hour'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T07:00:00.000Z', '2010-04-22T03:00:00.000Z', '2010-12-31T15:00:00.000Z', '2011-01-01T08:00:00.000Z', '2011-08-15T14:00:00.000Z', '2011-12-31T04:00:00.000Z', '2012-01-01T21:00:00.000Z', '2012-02-20T14:00:00.000Z', '2012-12-31T01:00:00.000Z', '2013-01-01T17:00:00.000Z', '2013-10-05T02:00:00.000Z', '2013-12-31T12:00:00.000Z', '2014-01-01T13:00:00.000Z', '2014-07-04T08:00:00.000Z', '2014-12-30T16:00:00.000Z', '2015-01-01T20:00:00.000Z', '2015-11-11T18:00:00.000Z', '2015-12-31T06:00:00.000Z', '2016-01-01T11:00:00.000Z', '2016-09-09T15:00:00.000Z', '2016-12-31T04:00:00.000Z', '2017-01-01T13:00:00.000Z', '2017-06-03T09:00:00.000Z', '2017-12-31T12:00:00.000Z', '2018-01-01T22:00:00.000Z', '2018-05-05T17:00:00.000Z', '2018-12-31T04:00:00.000Z', ]) ); }); test('minute', () => { const { result } = dateTimeDrill('result', getParams('minute'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T07:41:00.000Z', '2010-04-22T03:21:00.000Z', '2010-12-31T15:38:00.000Z', '2011-01-01T08:55:00.000Z', '2011-08-15T14:23:00.000Z', '2011-12-31T04:45:00.000Z', '2012-01-01T21:53:00.000Z', '2012-02-20T14:33:00.000Z', '2012-12-31T01:49:00.000Z', '2013-01-01T17:06:00.000Z', '2013-10-05T02:11:00.000Z', '2013-12-31T12:42:00.000Z', '2014-01-01T13:45:00.000Z', '2014-07-04T08:33:00.000Z', '2014-12-30T16:39:00.000Z', '2015-01-01T20:09:00.000Z', '2015-11-11T18:25:00.000Z', '2015-12-31T06:45:00.000Z', '2016-01-01T11:33:00.000Z', '2016-09-09T15:01:00.000Z', '2016-12-31T04:03:00.000Z', '2017-01-01T13:22:00.000Z', '2017-06-03T09:38:00.000Z', '2017-12-31T12:14:00.000Z', '2018-01-01T22:54:00.000Z', '2018-05-05T17:43:00.000Z', '2018-12-31T04:45:00.000Z', ]) ); }); test('second', () => { const { result } = dateTimeDrill('result', getParams('second'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2010-01-01T07:41:08.000Z', '2010-04-22T03:21:58.000Z', '2010-12-31T15:38:24.000Z', '2011-01-01T08:55:46.000Z', '2011-08-15T14:23:12.000Z', '2011-12-31T04:45:38.000Z', '2012-01-01T21:53:07.000Z', '2012-02-20T14:33:22.000Z', '2012-12-31T01:49:05.000Z', '2013-01-01T17:06:33.000Z', '2013-10-05T02:11:42.000Z', '2013-12-31T12:42:53.000Z', '2014-01-01T13:45:49.000Z', '2014-07-04T08:33:12.000Z', '2014-12-30T16:39:56.000Z', '2015-01-01T20:09:42.000Z', '2015-11-11T18:25:08.000Z', '2015-12-31T06:45:31.000Z', '2016-01-01T11:33:45.000Z', '2016-09-09T15:01:40.000Z', '2016-12-31T04:03:11.000Z', '2017-01-01T13:22:04.000Z', '2017-06-03T09:38:09.000Z', '2017-12-31T12:14:58.000Z', '2018-01-01T22:54:44.000Z', '2018-05-05T17:43:32.000Z', '2018-12-31T04:45:25.000Z', ]) ); }); test('iso_year', () => { const { result } = dateTimeDrill('result', getParams('iso_year'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2009-01-01T00:00:00.000Z', '2010-01-01T00:00:00.000Z', '2010-01-01T00:00:00.000Z', '2010-01-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2011-01-01T00:00:00.000Z', '2012-01-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2013-01-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2014-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2015-01-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2016-01-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2019-01-01T00:00:00.000Z', ]) ); }); test('iso_week', () => { const { result } = dateTimeDrill('result', getParams('iso_week'), { date }); expect( bind( date, result.map((x) => new Date(x).toISOString()) ) ).toEqual( bind(date, [ '2009-12-28T00:00:00.000Z', '2010-04-19T00:00:00.000Z', '2010-12-27T00:00:00.000Z', '2010-12-27T00:00:00.000Z', '2011-08-15T00:00:00.000Z', '2011-12-26T00:00:00.000Z', '2011-12-26T00:00:00.000Z', '2012-02-20T00:00:00.000Z', '2012-12-31T00:00:00.000Z', '2012-12-31T00:00:00.000Z', '2013-09-30T00:00:00.000Z', '2013-12-30T00:00:00.000Z', '2013-12-30T00:00:00.000Z', '2014-06-30T00:00:00.000Z', '2014-12-29T00:00:00.000Z', '2014-12-29T00:00:00.000Z', '2015-11-09T00:00:00.000Z', '2015-12-28T00:00:00.000Z', '2015-12-28T00:00:00.000Z', '2016-09-05T00:00:00.000Z', '2016-12-26T00:00:00.000Z', '2016-12-26T00:00:00.000Z', '2017-05-29T00:00:00.000Z', '2017-12-25T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-04-30T00:00:00.000Z', '2018-12-31T00:00:00.000Z', ]) ); }); }); describe('feature', () => { test('year', () => { const { result } = dateTimeFeature('result', getParams('year'), { date }); expect(result).toEqual([ 2010, 2010, 2010, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, ]); }); test('quarter', () => { const { result } = dateTimeFeature('result', getParams('quarter'), { date }); expect(bind(date, result)).toEqual(bind(date, [1, 2, 4, 1, 3, 4, 1, 1, 4, 1, 4, 4, 1, 3, 4, 1, 4, 4, 1, 3, 4, 1, 2, 4, 1, 2, 4])); }); test('month', () => { const { result } = dateTimeFeature('result', getParams('month'), { date }); expect(bind(date, result)).toEqual(bind(date, [1, 4, 12, 1, 8, 12, 1, 2, 12, 1, 10, 12, 1, 7, 12, 1, 11, 12, 1, 9, 12, 1, 6, 12, 1, 5, 12])); }); test('week', () => { const { result } = dateTimeFeature('result', getParams('week'), { date }); expect(bind(date, result)).toEqual(bind(date, [0, 16, 52, 0, 33, 52, 1, 8, 53, 0, 39, 52, 0, 26, 52, 0, 45, 52, 0, 36, 52, 1, 22, 53, 0, 17, 52])); }); test('weekday', () => { const { result } = dateTimeFeature('result', getParams('weekday'), { date }); expect(bind(date, result)).toEqual(bind(date, [5, 4, 5, 6, 1, 6, 0, 1, 1, 2, 6, 2, 3, 5, 2, 4, 3, 4, 5, 5, 6, 0, 6, 0, 1, 6, 1])); }); test('day', () => { const { result } = dateTimeFeature('result', getParams('day'), { date }); expect(bind(date, result)).toEqual(bind(date, [1, 22, 31, 1, 15, 31, 1, 20, 31, 1, 5, 31, 1, 4, 30, 1, 11, 31, 1, 9, 31, 1, 3, 31, 1, 5, 31])); }); test('hour', () => { const { result } = dateTimeFeature('result', getParams('hour'), { date }); expect(bind(date, result)).toEqual(bind(date, [7, 3, 15, 8, 14, 4, 21, 14, 1, 17, 2, 12, 13, 8, 16, 20, 18, 6, 11, 15, 4, 13, 9, 12, 22, 17, 4])); }); test('minute', () => { const { result } = dateTimeFeature('result', getParams('minute'), { date }); expect(bind(date, result)).toEqual( bind(date, [41, 21, 38, 55, 23, 45, 53, 33, 49, 6, 11, 42, 45, 33, 39, 9, 25, 45, 33, 1, 3, 22, 38, 14, 54, 43, 45]) ); }); test('second', () => { const { result } = dateTimeFeature('result', getParams('second'), { date }); expect(bind(date, result)).toEqual(bind(date, [8, 58, 24, 46, 12, 38, 7, 22, 5, 33, 42, 53, 49, 12, 56, 42, 8, 31, 45, 40, 11, 4, 9, 58, 44, 32, 25])); }); test('iso_year', () => { const { result } = dateTimeFeature('result', getParams('iso_year'), { date }); expect(bind(date, result)).toEqual( bind( date, [ 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2018, 2018, 2019, ] ) ); }); test('iso_week', () => { const { result } = dateTimeFeature('result', getParams('iso_week'), { date }); expect(bind(date, result)).toEqual(bind(date, [53, 16, 52, 52, 33, 52, 52, 8, 1, 1, 40, 1, 1, 27, 1, 1, 46, 53, 53, 36, 52, 52, 22, 52, 1, 18, 1])); }); test('iso_weekday', () => { const { result } = dateTimeFeature('result', getParams('iso_weekday'), { date }); expect(bind(date, result)).toEqual(bind(date, [5, 4, 5, 6, 1, 6, 7, 1, 1, 2, 6, 2, 3, 5, 2, 4, 3, 4, 5, 5, 6, 7, 6, 7, 1, 6, 1])); }); }); describe('edge', () => { const edgeDates = [ '2012-12-31T00:00:00.000Z', '2016-01-03T00:00:00.000Z', '2016-01-04T00:00:00.000Z', '2017-01-01T00:00:00.000Z', '2017-01-02T00:00:00.000Z', '2017-01-03T00:00:00.000Z', '2017-12-31T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-01-02T00:00:00.000Z', ]; test('week', () => { const { result } = dateTimeFeature('result', getParams('week'), { date: edgeDates }); expect(bind(edgeDates, result)).toEqual(bind(edgeDates, [53, 1, 1, 1, 1, 1, 53, 0, 0])); }); test('iso_week', () => { const { result } = dateTimeFeature('result', getParams('iso_week'), { date: edgeDates }); expect(bind(edgeDates, result)).toEqual(bind(edgeDates, [1, 53, 1, 52, 1, 1, 52, 1, 1])); }); });