Skip to content

Commit

Permalink
feat: allow checking validity on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-gilliotte committed Mar 28, 2021
1 parent 5f98089 commit f1ae493
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/timeslot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ declare enum TimeSlotPeriodicity {
}

declare class TimeSlot {
static fromValue(value: string, check: boolean = false): TimeSlot;
static fromDate(utcDate: string | Date, periodicity: TimeSlotPeriodicity): TimeSlot;

constructor(value: string);
constructor(value: string, check: boolean);

get value(): string;
get periodicity(): TimeSlotPeriodicity;
Expand Down
45 changes: 33 additions & 12 deletions src/timeslot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const instances = HashLru(1e3);
* This can be a given day, epidemiological week, month, quarter, ...
*/
class TimeSlot {
static fromValue(value) {
static fromValue(value, check = false) {
let ts = instances.get(value);

if (!ts) {
ts = new TimeSlot(value);
ts = new TimeSlot(value, check);
instances.set(value, ts);
}

Expand Down Expand Up @@ -194,7 +194,7 @@ class TimeSlot {
*
* @param {string} value A valid TimeSlot value (those can be found calling the `value` getter).
*/
constructor(value) {
constructor(value, check = false) {
this._value = value;
this._firstDate = null;
this._lastDate = null;
Expand All @@ -206,16 +206,37 @@ class TimeSlot {
// Poor man's parser.
// The previous versions based on regexps was on the top of the profiler on monitool.
const len = value.length;
if (len === 3) this._periodicity = 'all';
else if (len === 4) this._periodicity = 'year';
else if (len === 7) {
if (len === 3) {
this._periodicity = 'all';
} else if (len === 4) {
this._periodicity = 'year';
} else if (len === 7) {
const charAt6 = value[5];
if (charAt6 === 'Q') this._periodicity = 'quarter';
else if (charAt6 === 'S') this._periodicity = 'semester';
else this._periodicity = 'month';
} else if (len == 10) this._periodicity = 'day';
else if (len == 12) this._periodicity = 'week_' + value.substr(9);
else if (len == 14) this._periodicity = 'month_week_' + value.substr(11);
if (charAt6 === 'Q') {
this._periodicity = 'quarter';
} else if (charAt6 === 'S') {
this._periodicity = 'semester';
} else {
this._periodicity = 'month';
}
} else if (len == 10) {
this._periodicity = 'day';
} else if (len == 12) {
this._periodicity = 'week_' + value.substr(9);
} else if (len == 14) {
this._periodicity = 'month_week_' + value.substr(11);
}

if (check) {
try {
const newValue = TimeSlot.fromDate(this.firstDate, this.periodicity);
if (newValue !== value) {
throw new Error();
}
} catch (e) {
throw new Error('Invalid value');
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/timeslot.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare type TimeSlotPeriodicity =
| 'all';

declare export class TimeSlot {
static fromValue(value: string, check: boolean = false): TimeSlot;
static fromDate(utcDate: string | Date, periodicity: TimeSlotPeriodicity): TimeSlot;

constructor(value: string): TimeSlot;
Expand Down
7 changes: 7 additions & 0 deletions test/timeslot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ const assert = require('assert');
const TimeSlot = require('../src/timeslot');

describe('TimeSlot', () => {
describe('.fromValue', () => {
it('should not allow invalid values', () => {
assert.throws(() => TimeSlot.fromValue('Malaria', true));
assert.throws(() => new TimeSlot('Malaria', true));
});
});

describe('.periodicity', () => {
it('should work with year format', () => {
let ts = TimeSlot.fromValue('2010');
Expand Down

0 comments on commit f1ae493

Please sign in to comment.