Skip to content

Commit

Permalink
feat: added expandToNumeric method, it expands unit based numeric arg…
Browse files Browse the repository at this point in the history
…ument to full size

if given '2K' for instance, this method returns 2000
  • Loading branch information
teclone committed Apr 16, 2019
1 parent 381569f commit 9b58815
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const alphabets = 'abcdefghijklmnopqrstuvwxyz';

const digits = '0123456789';

const units = {
'k': 1000,
'm': 1000000,
'g': 1000000000,
't': 1000000000000
}

export declare interface Callback {
(...args): any;
[propName: string]: any;
Expand Down Expand Up @@ -495,4 +502,37 @@ export const encodeQueries = (query: {[name: string]: string | number | (string
return Object.keys(query).map(name => {
return encodeQuery(name, query[name], multiValueIdentifier);
}).join('&');
};

/**
* expands the given unit based size to full numeric value
* @param size - numeric or string unit-based size
*/
export const expandToNumeric = (size: number | string): number => {

size = size.toString();
if (/^\.\d+$/.test(size) || /^\d+(?:\.\d*)?$/.test(size)) {
return Number.parseFloat(size);
}
else if (/^(\.\d+)([a-z]+)$/i.test(size) || /^(\d+(?:\.\d*)?)([a-z]+)$/i.test(size)) {
let numeric = Number.parseFloat(RegExp.$1);
const unit = RegExp.$2.toLowerCase();

switch(unit) {
case 'k':
return numeric * 1000;
case 'm':
case 'mb':
return numeric * 1000000;
case 'g':
case 'gb':
return numeric * 1000000000;
case 't':
case 'tb':
return numeric * 1000000000000;
}
}
else {
return 0;
}
};
36 changes: 36 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,40 @@ describe('Utils', function() {
);
});
});

describe('.expandToNumeric(size: number | string): number', function() {
it(`should expand the given unit based size to full numeric value multiplying value by
1000 if unit is k case insensitive`, function() {
expect(Utils.expandToNumeric('2.5k')).toEqual(2500);
expect(Utils.expandToNumeric('.5k')).toEqual(500);
});

it(`should expand the given unit based size to full numeric value multiplying value by
1000000 if unit is m or mb case insensitive`, function() {
expect(Utils.expandToNumeric('2.5M')).toEqual(2500000);
expect(Utils.expandToNumeric('.5mb')).toEqual(500000);
});

it(`should expand the given unit based size to full numeric value multiplying value by
1000000000 if unit is g or gb case insensitive`, function() {
expect(Utils.expandToNumeric('2.5G')).toEqual(2500000000);
expect(Utils.expandToNumeric('.5gb')).toEqual(500000000);
});

it(`should expand the given unit based size to full numeric value multiplying value by
1000000000000 if unit is T or tb case insensitive`, function() {
expect(Utils.expandToNumeric('2.5T')).toEqual(2500000000000);
expect(Utils.expandToNumeric('.5TB')).toEqual(500000000000);
});

it(`should return number if argument is a number`, function() {
expect(Utils.expandToNumeric('2.5')).toEqual(2.5);
expect(Utils.expandToNumeric(25)).toEqual(25);
});

it(`should return 0 if argument is a numeric unit`, function() {
expect(Utils.expandToNumeric('a')).toEqual(0);
expect(Utils.expandToNumeric('a00')).toEqual(0);
});
});
});

0 comments on commit 9b58815

Please sign in to comment.