Skip to content

Commit

Permalink
- Fixed #728
Browse files Browse the repository at this point in the history
- server duration parser and test refactor
  • Loading branch information
rg911 committed Nov 28, 2020
1 parent 1888cd0 commit 63b3a7a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
28 changes: 21 additions & 7 deletions src/core/utils/DtoMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,27 @@ export class DtoMapping {
*/
public static parseServerDuration(serverValue: string): Duration {
const preprocessedValue = serverValue.replace(`'`, '').trim();
const regex = `([0-9]+)([hdms]+)[:\\s]?$`;
const patten = `([0-9]+)([hdms]+)[:\\s]?`;
const regex = new RegExp(patten, 'g');

// if the input doesn't match the patten, don't bother to do loop
if (!preprocessedValue.match(patten)) {
throw new Error('Duration value format is not recognized.');
}

let duration = Duration.ofSeconds(0);
const matcher = preprocessedValue.match(regex);
if (matcher && matcher.length === 3) {
const num = parseInt(matcher[1]);
const type = matcher[2];
let match;
const types: string[] = [];
while ((match = regex.exec(preprocessedValue))) {
const num = parseInt(match[1]);
const type = match[2];

// Added check make sure no duplicated types
if (types.indexOf(type) >= 0 || !match || match.length !== 3) {
throw new Error('Duration value format is not recognized.');
}
types.push(type);

switch (type) {
case 'ms':
duration = duration.plusMillis(num);
Expand All @@ -120,9 +135,8 @@ export class DtoMapping {
default:
throw new Error('Duration value format is not recognized.');
}
return duration;
}
throw new Error(`Duration value format is not recognized.`);
return duration;
}

/**
Expand Down
29 changes: 13 additions & 16 deletions test/core/utils/DtoMapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,27 @@ describe('DtoMapping', () => {
});

it('parseServerDuration', () => {
const epochS = '12345s';
expect(DtoMapping.parseServerDuration(epochS).seconds()).to.be.equal(12345);
const epochM = '12345m';
expect(DtoMapping.parseServerDuration(epochM).toMinutes()).to.be.equal(12345);
const epochH = '12345h';
expect(DtoMapping.parseServerDuration(epochH).toHours()).to.be.equal(12345);
const epochMS = '12345ms';
expect(DtoMapping.parseServerDuration(epochMS).toMillis()).to.be.equal(12345);
const epochD = '12345d';
expect(DtoMapping.parseServerDuration(epochD).toDays()).to.be.equal(12345);
expect(DtoMapping.parseServerDuration('15s').toString()).to.be.equal('PT15S');
expect(DtoMapping.parseServerDuration('10m:15s').toString()).to.be.equal('PT10M15S');
expect(DtoMapping.parseServerDuration('10m').toString()).to.be.equal('PT10M');
expect(DtoMapping.parseServerDuration('5h3m1s').toString()).to.be.equal('PT5H3M1S');
expect(DtoMapping.parseServerDuration('10d:5m1s').toString()).to.be.equal('PT240H5M1S');
expect(DtoMapping.parseServerDuration('10d 5m1s').toString()).to.be.equal('PT240H5M1S');
expect(DtoMapping.parseServerDuration('10d:5m100ms').toString()).to.be.equal('PT240H5M0.1S');
expect(DtoMapping.parseServerDuration('10d:5m1ms').toString()).to.be.equal('PT240H5M0.001S');
expect(DtoMapping.parseServerDuration(`1'200ms`).toString()).to.be.equal('PT1.2S');
expect(DtoMapping.parseServerDuration('5s10x').toString()).to.be.equal('PT5S');
});

it('parseServerDuration - exception', () => {
expect(() => {
const epochS = '12345g';
DtoMapping.parseServerDuration(epochS).seconds();
DtoMapping.parseServerDuration('5sss');
}).to.throw();
expect(() => {
const epochS = 'adfs';
DtoMapping.parseServerDuration(epochS).seconds();
DtoMapping.parseServerDuration('10h:10h');
}).to.throw();
expect(() => {
const epochS = '123s45';
DtoMapping.parseServerDuration(epochS).seconds();
DtoMapping.parseServerDuration('abc');
}).to.throw();
});

Expand Down

0 comments on commit 63b3a7a

Please sign in to comment.