Skip to content

Commit

Permalink
Closes #8 - Option to prefer decimal separator
Browse files Browse the repository at this point in the history
New option to prefer decimal separator instead of thousands separators
Adds sec updates in nyc & mocha
  • Loading branch information
pocketrocket committed May 25, 2019
1 parent 3107544 commit 0f7a853
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"eslint": "^5.14.1",
"eslint-config-airbnb-es5": "^1.2.0",
"eslint-plugin-react": "^7.12.4",
"mocha": "^6.0.1",
"nyc": "^13.3.0"
"mocha": "^6.1.4",
"nyc": "^14.1.1"
},
"author": "pocketrocket <pocketrocket@codecaboose.com>",
"contributors": [
Expand Down
20 changes: 13 additions & 7 deletions src/floatify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

var floatify = function floatify(str) {
var floatify = function floatify(str, preferDecimalSeparator) {
// by default we prefer thousands separators
var preferThousandsSeparators = preferDecimalSeparator !== true;

var toFloatFormat = function toFloatFormat(str, ts, ds) {
var string = str;
var decimalSeparator = ds || '';
Expand All @@ -13,7 +16,7 @@ var floatify = function floatify(str) {
return parseFloat(string);
};

var parseParts = function parseParts(str, ele, count) {
var parseParts = function parseParts(str, ele, count, preferThousandsSeparators) {
var string = str;
var element = ele;
var parts = string.split(element);
Expand All @@ -37,7 +40,10 @@ var floatify = function floatify(str) {
}

if (current.length === 3) {
return toFloatFormat(string, element, '');
if (preferThousandsSeparators) {
return toFloatFormat(string, element, '');
}
return toFloatFormat(string, '', element);
}

if (count === 1) {
Expand Down Expand Up @@ -67,7 +73,7 @@ var floatify = function floatify(str) {
return parseResult || Number.NaN;
};

var parse = function parse(str) {
var parse = function parse(str, preferThousandsSeparators) {
var string = str;
var spacePos;
var spaceSplit;
Expand Down Expand Up @@ -112,12 +118,12 @@ var floatify = function floatify(str) {
function parseSingleSeparators() {
if (dotPos !== -1) {
// only dot(s) in format
return parseParts(string, '.', dotCount);
return parseParts(string, '.', dotCount, preferThousandsSeparators);
}

if (commaPos !== -1) {
// only comma(s) in format
return parseParts(string, ',', commaCount);
return parseParts(string, ',', commaCount, preferThousandsSeparators);
}

return toFloatFormat(string);
Expand Down Expand Up @@ -175,7 +181,7 @@ var floatify = function floatify(str) {
return parseSingleSeparators();
};

return parse(str);
return parse(str, preferThousandsSeparators);
};

if (typeof exports !== 'undefined') {
Expand Down
32 changes: 25 additions & 7 deletions test/floatify.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ const tests = [
{ input: '123,123', expectation: 123123 }
]
},
{
title: 'should optionally prefer decimal separators',
assertions: [
{
input: '123.123',
options: {preferDecimalSeparator: true},
expectation: 123.123
},
{
input: '123,123',
options: {preferDecimalSeparator: true},
expectation: 123.123
},
]
},
{
title: 'should work with mixed commas and dots as thousands separators',
assertions: [
Expand All @@ -275,7 +290,7 @@ const tests = [
{ input: '123.123,0', expectation: 123123 },
{ input: '1.234.567,0', expectation: 1234567 },
{ input: '1.234.567.890,0', expectation: 1234567890 },

{ input: '1,123.0', expectation: 1123 },
{ input: '12,123.0', expectation: 12123 },
{ input: '123,123.0', expectation: 123123 },
Expand All @@ -294,7 +309,7 @@ const tests = [
{ input: '123.1234.123', expectation: Number.NaN },
{ input: '1234.123.123', expectation: Number.NaN },
{ input: '1.2.3', expectation: Number.NaN },

{ input: ',', expectation: Number.NaN },
{ input: ',,', expectation: Number.NaN },
{ input: ',,,', expectation: Number.NaN },
Expand All @@ -303,7 +318,7 @@ const tests = [
{ input: '123,1234,123', expectation: Number.NaN },
{ input: '1234,123,123', expectation: Number.NaN },
{ input: '1,2,3', expectation: Number.NaN },

{ input: ' ', expectation: Number.NaN },
{ input: ' ', expectation: Number.NaN },
{ input: ' ', expectation: Number.NaN },
Expand All @@ -316,15 +331,15 @@ const tests = [
{ input: '1 2 3', expectation: Number.NaN },
{ input: '123 123,123 123', expectation: Number.NaN },
{ input: '123 123.123 123', expectation: Number.NaN },

{ input: '123.123,123.123,123', expectation: Number.NaN },
{ input: '123.123,123.123.123', expectation: Number.NaN },
{ input: '123,123.123,123.123', expectation: Number.NaN },
{ input: '123,123.123,123,123', expectation: Number.NaN },

{ input: '..,-', expectation: Number.NaN },
{ input: 'seven', expectation: Number.NaN },

{ input: '123 123,123.12', expectation: Number.NaN },
{ input: '123,123,123.123.2', expectation: Number.NaN },
{ input: '123 123 123.123.2', expectation: Number.NaN },
Expand Down Expand Up @@ -358,7 +373,10 @@ tests.forEach((test) => {
return;
}

assert.equal(floatify(assertion.input), assertion.expectation);
assert.equal(
floatify(assertion.input, assertion.options && assertion.options.preferDecimalSeparator),
assertion.expectation
);
});
});
});
Expand Down

0 comments on commit 0f7a853

Please sign in to comment.