Skip to content

Commit

Permalink
add number methods
Browse files Browse the repository at this point in the history
  • Loading branch information
valcucodev committed Apr 20, 2018
1 parent 4723f5d commit 4869faf
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export declare class VUtilities {
static filterHashes(hashes?: any[], selectorHash?: any): any[];
static enumDate(obj?: any): number | null;
static newUTCDateTimeStamp(): number;
static parseIntOrZero(value: number | string): number;
static parseFloatOrZero(value: number | string): number;
static parseBigOrZero(value: number | string): mathjs.BigNumber;
static parseBigOrOne(value: number | string): mathjs.BigNumber;
}
export default VUtilities;
9 changes: 9 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,18 @@ var VUtilities = /** @class */ (function () {
/*
* Number methods
*/
VUtilities.parseIntOrZero = function (value) {
return VUtilities.isNumeric(value) ? parseInt(value) : 0;
};
VUtilities.parseFloatOrZero = function (value) {
return VUtilities.isNumeric(value) ? parseFloat(value) : 0.0;
};
VUtilities.parseBigOrZero = function (value) {
return math.bignumber(VUtilities.isNumeric(value) ? value : 0.0);
};
VUtilities.parseBigOrOne = function (value) {
return math.bignumber(VUtilities.isNumeric(value) ? value : 1.0);
};
return VUtilities;
}());
exports.VUtilities = VUtilities;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,22 @@ export class VUtilities {
/*
* Number methods
*/
public static parseIntOrZero(value: number | string) {
return VUtilities.isNumeric(value) ? parseInt(value as string) : 0;
}

public static parseFloatOrZero(value: number | string) {
return VUtilities.isNumeric(value) ? parseFloat(value as string) : 0.0
}

public static parseBigOrZero(value: number | string) {
return math.bignumber(VUtilities.isNumeric(value) ? value : 0.0);
}

public static parseBigOrOne(value: number | string) {
return math.bignumber(VUtilities.isNumeric(value) ? value : 1.0);
}

}

export default VUtilities;
66 changes: 66 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,50 @@ describe('VUtilities functions test', () => {
expect(result.toString().length).to.equal(13);
});

it('should return parseIntOrZero', () => {
var result = [
VUtilities.parseIntOrZero(null),
VUtilities.parseIntOrZero('0'),
VUtilities.parseIntOrZero('010'),
VUtilities.parseIntOrZero(100),
VUtilities.parseIntOrZero(100.001),
VUtilities.parseIntOrZero('00100.001'),
VUtilities.parseIntOrZero('00100.001'),
];
var expectation = [
0,
0,
10,
100,
100,
100,
100,
];
expect(result.join('')).to.equal(expectation.join(''));
});

it('should return parseFloatOrZero', () => {
var result = [
VUtilities.parseFloatOrZero(null),
VUtilities.parseFloatOrZero('0'),
VUtilities.parseFloatOrZero('010'),
VUtilities.parseFloatOrZero(100),
VUtilities.parseFloatOrZero(100.001),
VUtilities.parseFloatOrZero('00100.001'),
VUtilities.parseFloatOrZero('00100.001'),
];
var expectation = [
0.0,
0.0,
10.0,
100.0,
100.001,
100.001,
100.001,
];
expect(result.join('')).to.equal(expectation.join(''));
});

it('should return parseBigOrZero', () => {
var result = [
VUtilities.parseBigOrZero(null),
Expand All @@ -652,4 +696,26 @@ describe('VUtilities functions test', () => {
expect(result.join('')).to.equal(expectation.join(''));
});

it('should return parseBigOrOne', () => {
var result = [
VUtilities.parseBigOrOne(null),
VUtilities.parseBigOrOne('0'),
VUtilities.parseBigOrOne('010'),
VUtilities.parseBigOrOne(100),
VUtilities.parseBigOrOne(100.001),
VUtilities.parseBigOrOne('00100.001'),
VUtilities.parseBigOrOne('00100.001'),
];
var expectation = [
1.0,
0.0,
10.0,
100.0,
100.001,
100.001,
100.001,
];
expect(result.join('')).to.equal(expectation.join(''));
});

});

0 comments on commit 4869faf

Please sign in to comment.