Skip to content

Commit

Permalink
Improve testing and documentation (#3)
Browse files Browse the repository at this point in the history
* Improve testing and documentation

* Improve coverage
  • Loading branch information
bameyrick authored Feb 8, 2023
1 parent 6a80da9 commit 56dbd6e
Show file tree
Hide file tree
Showing 37 changed files with 331 additions and 180 deletions.
67 changes: 61 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@ incorporated.
- 9:26pm
- 9pm

year month day

- 2016-09-24
- 2016-9-24
- 20160924

day monthname year

- Wednesday, 01 January 2020
Expand Down Expand Up @@ -292,21 +286,82 @@ month day year

- 03/14/2020
- 03-14-2020
- 03.14.2020
- 03 14 2020
- 3/14/2020
- 3-14-2020
- 3.14.2020
- 3 14 2020
- 03/14/20
- 03-14-20
- 03.14.20
- 03 14 20
- 3/14/20
- 3-14-20
- 3.14.20
- 3 14 20

day month year

- 14/03/2020
- 14.03.2020
- 14-03-2020
- 14 03 2020
- 14/3/2020
- 14.3.2020
- 14-3-2020
- 14 3 2020
- 14/03/20
- 14.03.20
- 14-03-20
- 14 03 20
- 14/3/20
- 14.3.20
- 14-3-20
- 14 3 20

year month day

- 2016/09/24
- 2016/09/24
- 2016/9/24
- 2016/9/24
- 16/09/24
- 16/09/24
- 16/9/24
- 16/9/24
- 2016-09-24
- 2016-09-24
- 2016-9-24
- 2016-9-24
- 16-09-24
- 16-09-24
- 16-9-24
- 16-9-24
- 2016 09 24
- 2016 09 24
- 2016 9 24
- 2016 9 24
- 16 09 24
- 16 09 24
- 16 9 24
- 16 9 24
- 2016.09.24
- 2016.09.24
- 2016.9.24
- 2016.9.24
- 16.09.24
- 16.09.24
- 16.9.24
- 16.9.24
- 20160924
- 20160924
- 2016924
- 2016924
- 160924
- 160924
- 16924
- 16924

relative time

Expand Down
40 changes: 20 additions & 20 deletions src/data/numbering-systems.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('unicode numbering systems', () => {
'٩': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "arabext"', () => {
Expand All @@ -38,7 +38,7 @@ describe('unicode numbering systems', () => {
'۹': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "bali"', () => {
Expand All @@ -58,7 +58,7 @@ describe('unicode numbering systems', () => {
'᭙': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "beng"', () => {
Expand All @@ -78,7 +78,7 @@ describe('unicode numbering systems', () => {
'৯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "deva"', () => {
Expand All @@ -98,7 +98,7 @@ describe('unicode numbering systems', () => {
'९': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "fullwide" and "hanidec"', () => {
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('unicode numbering systems', () => {
: 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
expect(hanidec).toEqual(expected);
});

Expand All @@ -160,7 +160,7 @@ describe('unicode numbering systems', () => {
'૯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "khmr"', () => {
Expand All @@ -180,7 +180,7 @@ describe('unicode numbering systems', () => {
'៩': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "knda"', () => {
Expand All @@ -200,7 +200,7 @@ describe('unicode numbering systems', () => {
'೯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "laoo"', () => {
Expand All @@ -220,7 +220,7 @@ describe('unicode numbering systems', () => {
'໙': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "latn"', () => {
Expand Down Expand Up @@ -260,7 +260,7 @@ describe('unicode numbering systems', () => {
: 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "limb"', () => {
Expand All @@ -280,7 +280,7 @@ describe('unicode numbering systems', () => {
'᥏': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "mlym"', () => {
Expand All @@ -300,7 +300,7 @@ describe('unicode numbering systems', () => {
'൯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "mong"', () => {
Expand All @@ -320,7 +320,7 @@ describe('unicode numbering systems', () => {
'᠙': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "mymr"', () => {
Expand All @@ -340,7 +340,7 @@ describe('unicode numbering systems', () => {
'၉': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "orya"', () => {
Expand All @@ -360,7 +360,7 @@ describe('unicode numbering systems', () => {
'୯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "tamldec"', () => {
Expand All @@ -380,7 +380,7 @@ describe('unicode numbering systems', () => {
'௯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "telu"', () => {
Expand All @@ -400,7 +400,7 @@ describe('unicode numbering systems', () => {
'౯': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "thai"', () => {
Expand All @@ -420,7 +420,7 @@ describe('unicode numbering systems', () => {
'๙': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});

it('should build "tibt"', () => {
Expand All @@ -440,6 +440,6 @@ describe('unicode numbering systems', () => {
'༩': 9,
},
};
expect(actual).toEqual(expected);
expect(actual).toEqual(expect.objectContaining(expected));
});
});
23 changes: 14 additions & 9 deletions src/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ import { Format } from './format';
describe('Format', () => {
it('should require units or handler', () => {
function missingUnitsAndHandler() {
return new Format({ template: '' });
return new Format({ template: '', name: 'test' });
}

expect(missingUnitsAndHandler).toThrowError();
});

it('should require template or matcher', () => {
function missingTemplateAndRegex() {
return new Format({ units: [] });
return new Format({ units: [], name: 'test' });
}

expect(missingTemplateAndRegex).toThrowError();
});

it('should build RegExp from template', () => {
const format = new Format({ handler: () => null, template: 'year:_YEAR_' });
const format = new Format({ handler: () => null, template: 'year:_YEAR_', name: 'test' });
const regex = format.getRegExp();

expect(regex).toEqual(/year:[1-9]\d{3}|\d{2}/i);
});

it('should getMatches()', () => {
const format = new Format({ handler: () => null, matcher: /foo:(\d)(\d)/ });
const format = new Format({ handler: () => null, matcher: /foo:(\d)(\d)/, name: 'test' });
const matches = [...format.getMatches('foo:42')!];

expect(matches).toEqual(['foo:42', '4', '2']);
Expand All @@ -35,49 +35,54 @@ describe('Format', () => {
const format = new Format({
units: ['year', 'month', 'day'],
matcher: /./,
name: 'test',
});
const actual = format.toDateTime([null, '2020', '10', '13']);

expect(actual).toEqual({ year: 2020, month: 10, day: 13 });
expect(actual).toEqual(expect.objectContaining({ year: 2020, month: 10, day: 13 }));
});

it('should convert monthname matches to Object', () => {
const format = new Format({
units: ['year', 'month', 'day'],
matcher: /./,
name: 'test',
});
const actual = format.toDateTime([null, '2020', 'oct', '13']);

expect(actual).toEqual({ year: 2020, month: 10, day: 13 });
expect(actual).toEqual(expect.objectContaining({ year: 2020, month: 10, day: 13 }));
});

it('should convert 2-digit years', () => {
const format = new Format({
units: ['year', 'month', 'day', 'minute'],
matcher: /./,
name: 'test',
});
const actual = format.toDateTime([null, '20', 'october', '13', '59']);

expect(actual).toEqual({ year: 2020, month: 10, day: 13, minute: 59 });
expect(actual).toEqual(expect.objectContaining({ year: 2020, month: 10, day: 13, minute: 59 }));
});

it('should attempt to parse', () => {
const format = new Format({
matcher: /(\d+)m (\d+)s/,
units: ['minute', 'second'],
name: 'test',
});
const actual = format.attempt('56m 22s');

expect(actual).toEqual({ minute: 56, second: 22 });
expect(actual).toMatchObject({ minute: 56, second: 22 });
});

it('should trim()', () => {
const format = new Format({
matcher: /^(\d+)s (\d+)ms$/,
units: ['second', 'millisecond'],
name: 'test',
});
const actual = format.attempt(' 56s 813ms\t');

expect(actual).toEqual({ second: 56, millisecond: 813 });
expect(actual).toEqual(expect.objectContaining({ second: 56, millisecond: 813 }));
});
});
Loading

0 comments on commit 56dbd6e

Please sign in to comment.