Skip to content

Commit

Permalink
refactor: prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
standy committed May 5, 2020
1 parent d8c59c4 commit 516d7e4
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 207 deletions.
10 changes: 5 additions & 5 deletions locale/da/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as assert from 'power-assert';
import {newValidDate} from '../../src/create/create-ts-date';
import {format, parse} from './index';

describe('da locale', function() {
it('correct formatting', function() {
describe('da locale', function () {
it('correct formatting', function () {
const date = newValidDate(2017, Month.Oct, 1, 12, 34, 56, 789);
const FORMATS = [
['MMMM MMM dd ddd dddd A Do Wo Mo DDDo do Qo', 'oktober okt sø søn søndag PM 1. 39. 10. 274. 0. 4.'],
Expand All @@ -21,15 +21,15 @@ describe('da locale', function() {
assert.equal(result, correctResult, `format "${template}"`);
}
});
it('correct ordinal', function() {
it('correct ordinal', function () {
const d1 = newValidDate(2017, Month.Oct, 12);
const d2 = newValidDate(2017, Month.Oct, 22);
const d3 = newValidDate(2017, Month.Oct, 23);
assert.equal(format(d1, 'Do MMMM'), '12. oktober');
assert.equal(format(d2, 'Do MMMM'), '22. oktober');
assert.equal(format(d3, 'Do MMMM'), '23. oktober');
});
it('correct day part format', function() {
it('correct day part format', function () {
const d1 = newValidDate(2017, Month.Oct, 1, 0, 42, 12);
const d2 = newValidDate(2017, Month.Oct, 1, 18, 42, 12);
assert.equal(format(d1, 'h A'), '12 AM');
Expand All @@ -41,7 +41,7 @@ describe('da locale', function() {
assert.equal(format(d1, 'HH:mm:ss'), '00:42:12');
assert.equal(format(d2, 'HH:mm:ss'), '18:42:12');
});
it('correct parsing', function() {
it('correct parsing', function () {
const FORMATS = [
{
template: 'D MMMM YY [Year]',
Expand Down
18 changes: 9 additions & 9 deletions locale/da/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ const meridiemFull = ['a.m.', 'p.m.'];

export const formatters: FormatterObj = {
// Month: jan, feb, ..., dec
MMM: date => months3char[date.getMonth()],
MMM: (date) => months3char[date.getMonth()],

// Month: januar, februar, ..., december
MMMM: date => monthsFull[date.getMonth()],
MMMM: (date) => monthsFull[date.getMonth()],

// Day of week: sø, ma, ..., lø
dd: date => weekdays2char[date.getDay()],
dd: (date) => weekdays2char[date.getDay()],

// Day of week: søn, man, ..., lør
ddd: date => weekdays3char[date.getDay()],
ddd: (date) => weekdays3char[date.getDay()],

// Day of week: søndag, mandag, ..., lørdag
dddd: date => weekdaysFull[date.getDay()],
dddd: (date) => weekdaysFull[date.getDay()],

// AM, PM
A: date => meridiemUppercase[date.getHours() < 12 ? 0 : 1],
A: (date) => meridiemUppercase[date.getHours() < 12 ? 0 : 1],

// am, pm
a: date => meridiemLowercase[date.getHours() < 12 ? 0 : 1],
a: (date) => meridiemLowercase[date.getHours() < 12 ? 0 : 1],

// a.m., p.m.
aa: date => meridiemFull[date.getHours() < 12 ? 0 : 1],
aa: (date) => meridiemFull[date.getHours() < 12 ? 0 : 1],

Mo: ordinalFormatter('M'),
Do: ordinalFormatter('D'),
Expand All @@ -72,7 +72,7 @@ export const formatters: FormatterObj = {

// Generate ordinal version of formatters: M -> Mo, D -> Do, etc.
function ordinalFormatter(formatterToken: string): Formatter {
return date => ordinal(defaultFormatters[formatterToken](date) as number);
return (date) => ordinal(defaultFormatters[formatterToken](date) as number);
}

function ordinal(number: number) {
Expand Down
10 changes: 5 additions & 5 deletions locale/en/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as assert from 'power-assert';
import {newValidDate} from '../../src/create/create-ts-date';
import {format, parse} from './index';

describe('ru locale', function() {
it('correct formatting', function() {
describe('ru locale', function () {
it('correct formatting', function () {
const date = newValidDate(2017, Month.Aug, 1, 12, 34, 56, 789);
const FORMATS = [
['MMMM MMM dd ddd dddd A Do Wo Mo DDDo do Qo', 'August Aug Tu Tue Tuesday PM 1st 31st 8th 213th 2nd 3rd'],
Expand All @@ -21,15 +21,15 @@ describe('ru locale', function() {
assert.equal(result, correctResult, `format "${template}"`);
}
});
it('correct ordinal', function() {
it('correct ordinal', function () {
const d1 = newValidDate(2017, Month.Aug, 12);
const d2 = newValidDate(2017, Month.Aug, 22);
const d3 = newValidDate(2017, Month.Aug, 23);
assert.equal(format(d1, 'Do MMMM'), '12th August');
assert.equal(format(d2, 'Do MMMM'), '22nd August');
assert.equal(format(d3, 'Do MMMM'), '23rd August');
});
it('correct day part format', function() {
it('correct day part format', function () {
const d1 = newValidDate(2017, Month.Aug, 1, 0);
const d2 = newValidDate(2017, Month.Aug, 1, 18);
assert.equal(format(d1, 'h A'), '12 AM');
Expand All @@ -39,7 +39,7 @@ describe('ru locale', function() {
assert.equal(format(d1, 'h aa'), '12 a.m.');
assert.equal(format(d2, 'h aa'), '6 p.m.');
});
it('correct parsing', function() {
it('correct parsing', function () {
const FORMATS = [
{
template: 'D MMMM YY [Year]',
Expand Down
18 changes: 9 additions & 9 deletions locale/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ const meridiemFull = ['a.m.', 'p.m.'];

export const formatters: FormatterObj = {
// Month: Jan, Feb, ..., Dec
MMM: date => months3char[date.getMonth()],
MMM: (date) => months3char[date.getMonth()],

// Month: January, February, ..., December
MMMM: date => monthsFull[date.getMonth()],
MMMM: (date) => monthsFull[date.getMonth()],

// Day of week: Su, Mo, ..., Sa
dd: date => weekdays2char[date.getDay()],
dd: (date) => weekdays2char[date.getDay()],

// Day of week: Sun, Mon, ..., Sat
ddd: date => weekdays3char[date.getDay()],
ddd: (date) => weekdays3char[date.getDay()],

// Day of week: Sunday, Monday, ..., Saturday
dddd: date => weekdaysFull[date.getDay()],
dddd: (date) => weekdaysFull[date.getDay()],

// AM, PM
A: date => meridiemUppercase[date.getHours() < 12 ? 0 : 1],
A: (date) => meridiemUppercase[date.getHours() < 12 ? 0 : 1],

// am, pm
a: date => meridiemLowercase[date.getHours() < 12 ? 0 : 1],
a: (date) => meridiemLowercase[date.getHours() < 12 ? 0 : 1],

// a.m., p.m.
aa: date => meridiemFull[date.getHours() < 12 ? 0 : 1],
aa: (date) => meridiemFull[date.getHours() < 12 ? 0 : 1],

Mo: ordinalFormatter('M'),
Do: ordinalFormatter('D'),
Expand All @@ -75,7 +75,7 @@ export const formatters: FormatterObj = {

// Generate ordinal version of formatters: M -> Mo, D -> Do, etc.
function ordinalFormatter(formatterToken: string): Formatter {
return date => ordinal(defaultFormatters[formatterToken](date) as number);
return (date) => ordinal(defaultFormatters[formatterToken](date) as number);
}

function ordinal(number: number) {
Expand Down
10 changes: 5 additions & 5 deletions locale/ru/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as assert from 'power-assert';
import {newValidDate, newValidDateOrThrow} from '../../src/create/create-ts-date';
import {format, parse, createCustomFormat, formatters, ValidDate} from './index';

describe('ru locale', function() {
it('correct formatting', function() {
describe('ru locale', function () {
it('correct formatting', function () {
const date = newValidDate(2017, Month.Aug, 1, 12, 34, 56, 789);
const FORMATS = [
['MMMM MMM dd ddd dddd A Do Wo Mo DDDo do Qo', 'август авг. вт втр вторник дня 1-е 31-й 8-й 213-й 2-й 3-й'],
Expand All @@ -23,7 +23,7 @@ describe('ru locale', function() {
assert.equal(result, correctResult, `format "${template}"`);
}
});
it('correct createCustomFormat', function() {
it('correct createCustomFormat', function () {
const customFormat = createCustomFormat({
MMM: (d, index, tokens) => {
const m: string = formatters['MMMM'](d, index, tokens) + '';
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('ru locale', function() {
assert.equal(result, correctResult, `customFormat "${format}" "${date}"`);
}
});
it('correct day part format', function() {
it('correct day part format', function () {
const d1 = newValidDate(2017, Month.Aug, 1, 0);
const d2 = newValidDate(2017, Month.Aug, 1, 6);
const d3 = newValidDate(2017, Month.Aug, 1, 12);
Expand All @@ -78,7 +78,7 @@ describe('ru locale', function() {
assert.equal(format(d3, template), '12 дня');
assert.equal(format(d4, template), '6 вечера');
});
it('correct parsing', function() {
it('correct parsing', function () {
const FORMATS = [
{
template: 'Do MMMM YY [года]',
Expand Down
24 changes: 12 additions & 12 deletions locale/ru/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function timeOfDay(date: ValidDate) {

export const formatters: FormatterObj = {
// Month: янв., фев., ..., дек.
MMM: date => monthsShort[date.getMonth()],
MMM: (date) => monthsShort[date.getMonth()],

// Month: январь, февраль, ..., декабрь
MMMM: (date, index, tokens) => {
Expand All @@ -84,28 +84,28 @@ export const formatters: FormatterObj = {
},

// Day of week: вс, пн, ..., сб
dd: date => weekdays2char[date.getDay()],
dd: (date) => weekdays2char[date.getDay()],

// Day of week: вск, пнд, ..., суб
ddd: date => weekdays3char[date.getDay()],
ddd: (date) => weekdays3char[date.getDay()],

// Day of week: воскресенье, понедельник, ..., суббота
dddd: date => weekdaysFull[date.getDay()],
dddd: (date) => weekdaysFull[date.getDay()],

// Time of day: ночи, утра, дня, вечера
A: timeOfDay,
a: timeOfDay,
aa: timeOfDay,

// Generate ordinal version of formatters: M -> Mo, DDD -> DDDo, etc.
Do: date => defaultFormatters['D'](date) + '-е',
Wo: date => defaultFormatters['W'](date) + '-й',
Mo: date => defaultFormatters['M'](date) + '-й',
DDDo: date => defaultFormatters['DDD'](date) + '-й',
do: date => defaultFormatters['d'](date) + '-й',
Qo: date => defaultFormatters['Q'](date) + '-й',

'Do MMMM': date => formatters['Do'](date) + ' ' + monthsGenitive[date.getMonth()],
Do: (date) => defaultFormatters['D'](date) + '-е',
Wo: (date) => defaultFormatters['W'](date) + '-й',
Mo: (date) => defaultFormatters['M'](date) + '-й',
DDDo: (date) => defaultFormatters['DDD'](date) + '-й',
do: (date) => defaultFormatters['d'](date) + '-й',
Qo: (date) => defaultFormatters['Q'](date) + '-й',

'Do MMMM': (date) => formatters['Do'](date) + ' ' + monthsGenitive[date.getMonth()],
};

const parsers: ParserObj = {
Expand Down
30 changes: 15 additions & 15 deletions src/add/add-unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,68 @@ import * as assert from 'power-assert';
import {newValidDate} from '../create/create-ts-date';
import {addMilliseconds, addSeconds, addMinutes, addHours, addDate, addMonth, addYear} from './add-unit';

describe('addUnits', function() {
describe('add given amount of units', function() {
it('addMilliseconds', function() {
describe('addUnits', function () {
describe('add given amount of units', function () {
it('addMilliseconds', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
const result = addMilliseconds(tsDate, 2);
assert.deepEqual(result, new Date(2017, Month.Jun, 29, 12, 30, 59, 102));
});

it('addSeconds', function() {
it('addSeconds', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
const result = addSeconds(tsDate, 2);
assert.deepEqual(result, new Date(2017, Month.Jun, 29, 12, 31, 1, 100));
});

it('addMinutes', function() {
it('addMinutes', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
const result = addMinutes(tsDate, 2);
assert.deepEqual(result, new Date(2017, Month.Jun, 29, 12, 32, 59, 100));
});

it('addHours', function() {
it('addHours', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
const result = addHours(tsDate, 2);
assert.deepEqual(result, new Date(2017, Month.Jun, 29, 14, 30, 59, 100));
});

it('addDate', function() {
it('addDate', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
const result = addDate(tsDate, 2);
assert.deepEqual(result, new Date(2017, Month.Jul, 1, 12, 30, 59, 100));
});

it('addMonth', function() {
it('addMonth', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
const result = addMonth(tsDate, 2);
assert.deepEqual(result, new Date(2017, Month.Aug, 29, 12, 30, 59, 100));
});

it('addYear', function() {
it('addYear', function () {
const tsDate = newValidDate(2017, Month.Jun, 29);
const result = addYear(tsDate, 2);
assert.deepEqual(result, new Date(2019, Month.Jun, 29));
});
});

describe('edge cases', function() {
it('falls to null with invalid date', function() {
describe('edge cases', function () {
it('falls to null with invalid date', function () {
const result = addYear(null, 2);
assert.deepEqual(result, null);
const resultHours = addHours(null, 2);
assert.deepEqual(resultHours, null);
});

it('no changes with "Infinity" value', function() {
it('no changes with "Infinity" value', function () {
const tsDate = newValidDate(2017, Month.Jun, 29);
const result = addYear(tsDate, Infinity);
assert.deepEqual(result, tsDate);
const resultHours = addHours(tsDate, Infinity);
assert.deepEqual(resultHours, tsDate);
});

it('no changes with "NaN" value', function() {
it('no changes with "NaN" value', function () {
const tsDate = newValidDate(2017, Month.Jun, 29);
const result = addYear(tsDate, NaN);
assert.deepEqual(result, tsDate);
Expand All @@ -73,9 +73,9 @@ describe('addUnits', function() {
});
});

it('does not mutate the original date', function() {
it('does not mutate the original date', function () {
const tsDate = newValidDate(2017, Month.Jun, 29, 12, 30, 59, 100);
[addMilliseconds, addSeconds, addMinutes, addHours, addDate, addMonth, addYear].forEach(method =>
[addMilliseconds, addSeconds, addMinutes, addHours, addDate, addMonth, addYear].forEach((method) =>
method(tsDate, 1),
);
assert.deepEqual(tsDate, new Date(2017, Month.Jun, 29, 12, 30, 59, 100));
Expand Down
8 changes: 4 additions & 4 deletions src/create/create-ts-date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import * as assert from 'power-assert';
import {Month} from '../utils/basic-types';
import {newValidDate, newValidDateOrThrow, fromDate, fromDateOrThrow} from './create-ts-date';

describe('create', function() {
it('create date', function() {
describe('create', function () {
it('create date', function () {
assert.ok(fromDate(new Date()));
assert.ok(fromDateOrThrow(new Date()));
assert.ok(newValidDate(+new Date()));
});

it('correct null handling', function() {
it('correct null handling', function () {
assert.equal(fromDate(NaN), null);
assert.equal(newValidDate(NaN), null);
assert.throws(() => fromDateOrThrow(NaN));
assert.throws(() => newValidDateOrThrow(NaN));
});

it('correct turn ValidDate to Date ', function() {
it('correct turn ValidDate to Date ', function () {
const date = newValidDateOrThrow(2017, Month.Jul, 1) as Date;
date.setDate(2);
assert.deepEqual(date, new Date(2017, Month.Jul, 2));
Expand Down
Loading

0 comments on commit 516d7e4

Please sign in to comment.