Skip to content

Commit

Permalink
fix(convertMany): allow converting units containing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider committed Mar 5, 2021
1 parent fb81e39 commit 6581876
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/convert-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const enum MatchGroups {
Unit
}

const splitExpression = /(-?(?:\d+)?\.?\d+)([^\d\s]+)/g;
const splitExpression = /(-?(?:\d+)?\.?\d+)([^\s]+)/g;

/**
* Convert several values in a string into a single unit.
Expand Down
15 changes: 9 additions & 6 deletions test/convert-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import {convertMany} from '../src/';

describe('convertMany', () => {
it('combines several units', () => {
// expect(convertMany('1min 30s').to('second')).toBe(90);
// expect(convertMany('51h 13min 56s').to('hours')).toBe(51 + 13 / 60 + 56 / 60 / 60);
expect(convertMany('1min 30s').to('second')).toBe(90);
expect(convertMany('51h 13min 56s').to('hours')).toBe(51 + 13 / 60 + 56 / 60 / 60);
expect(convertMany('51h 13min 56s').to('h')).toBe(51 + 13 / 60 + 56 / 60 / 60);
});
expect(convertMany('1m3').to('m3')).toBe(1);

it("doesn't convert when not necessary", () => {
expect(convertMany('1min').to('second')).toBe(60);
expect(convertMany('.1m').to('m')).toBe(0.1);
expect(convertMany('-.1m').to('m')).toBe(-0.1);
expect(convertMany('-1m').to('m')).toBe(-1);
expect(convertMany('0.1m').to('m')).toBe(0.1);
expect(convertMany('-0.1m').to('m')).toBe(-0.1);
});

it('throws when appropriate', () => {
Expand All @@ -27,7 +30,7 @@ describe('convertMany', () => {
global.__DEV__ = true;

convertMany('1min 2hours').to('seconds');
convertMany('1min2km').to('seconds');
convertMany('1min 2km').to('seconds');
}).toThrow("Couldn't convert km to seconds");
});
});

0 comments on commit 6581876

Please sign in to comment.