Skip to content

Commit

Permalink
Merge branch 'rinogo-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
nosir committed May 25, 2019
2 parents 964b8bf + 6dbcf6c commit 112c530
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
19 changes: 19 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [numeralDecimalScale](#numeraldecimalscale)
- [numeralDecimalMark](#numeraldecimalmark)
- [numeralPositiveOnly](#numeralpositiveonly)
- [signBeforePrefix](#signbeforeprefix)
- [stripLeadingZeroes](#stripleadingzeroes)
- General config:
- [blocks](#blocks)
Expand Down Expand Up @@ -351,6 +352,24 @@ new Cleave('.my-input', {
// 1234.56
```

### `signBeforePrefix`

A `Boolean` value indicates if the sign of the numeral should appear before the prefix.

**Default value**: `false`

```js
new Cleave('.my-input', {
numeral: true,
prefix: '$',
signBeforePrefix: true
});
```

```js
// -$1234.56
```

### `stripLeadingZeroes`

A `Boolean` value indicates if zeroes appearing at the beginning of the number should be stripped out. This also prevents a number like "100,000" to disappear if the leading "1" is deleted.
Expand Down
8 changes: 6 additions & 2 deletions src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Cleave.prototype = {
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
pps.stripLeadingZeroes,
pps.prefix,
pps.signBeforePrefix,
pps.delimiter
);
},
Expand Down Expand Up @@ -248,8 +250,10 @@ Cleave.prototype = {

// numeral formatter
if (pps.numeral) {
if (pps.prefix && (!pps.noImmediatePrefix || value.length)) {
pps.result = pps.prefix + pps.numeralFormatter.format(value);
// Do not show prefix when noImmediatePrefix is specified
// This mostly because we need to show user the native input placeholder
if (pps.prefix && pps.noImmediatePrefix && value.length === 0) {
pps.result = '';
} else {
pps.result = pps.numeralFormatter.format(value);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Cleave.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ var cleaveReactClass = CreateReactClass({
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
pps.stripLeadingZeroes,
pps.prefix,
pps.signBeforePrefix,
pps.delimiter
);
},
Expand Down Expand Up @@ -308,8 +310,10 @@ var cleaveReactClass = CreateReactClass({

// numeral formatter
if (pps.numeral) {
if (pps.prefix && (!pps.noImmediatePrefix || value.length)) {
pps.result = pps.prefix + pps.numeralFormatter.format(value);
// Do not show prefix when noImmediatePrefix is specified
// This mostly because we need to show user the native input placeholder
if (pps.prefix && pps.noImmediatePrefix && value.length === 0) {
pps.result = '';
} else {
pps.result = pps.numeralFormatter.format(value);
}
Expand Down
1 change: 1 addition & 0 deletions src/common/DefaultProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var DefaultProperties = {
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
target.signBeforePrefix = !!opts.signBeforePrefix;

// others
target.numericOnly = target.creditCard || target.date || !!opts.numericOnly;
Expand Down
25 changes: 22 additions & 3 deletions src/shortcuts/NumeralFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var NumeralFormatter = function (numeralDecimalMark,
numeralThousandsGroupStyle,
numeralPositiveOnly,
stripLeadingZeroes,
prefix,
signBeforePrefix,
delimiter) {
var owner = this;

Expand All @@ -15,6 +17,8 @@ var NumeralFormatter = function (numeralDecimalMark,
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
owner.prefix = (prefix || prefix === '') ? prefix : '';
owner.signBeforePrefix = !!signBeforePrefix;
owner.delimiter = (delimiter || delimiter === '') ? delimiter : ',';
owner.delimiterRE = delimiter ? new RegExp('\\' + delimiter, 'g') : '';
};
Expand All @@ -32,7 +36,7 @@ NumeralFormatter.prototype = {
},

format: function (value) {
var owner = this, parts, partInteger, partDecimal = '';
var owner = this, parts, partSign, partSignAndPrefix, partInteger, partDecimal = '';

// strip alphabet letters
value = value.replace(/[A-Za-z]/g, '')
Expand Down Expand Up @@ -60,6 +64,17 @@ NumeralFormatter.prototype = {
value = value.replace(/^(-)?0+(?=\d)/, '$1');
}

partSign = value.slice(0, 1) === '-' ? '-' : '';
if (typeof owner.prefix != 'undefined') {
if (owner.signBeforePrefix) {
partSignAndPrefix = partSign + owner.prefix;
} else {
partSignAndPrefix = owner.prefix + partSign;
}
} else {
partSignAndPrefix = partSign;
}

partInteger = value;

if (value.indexOf(owner.numeralDecimalMark) >= 0) {
Expand All @@ -68,8 +83,12 @@ NumeralFormatter.prototype = {
partDecimal = owner.numeralDecimalMark + parts[1].slice(0, owner.numeralDecimalScale);
}

if(partSign === '-') {
partInteger = partInteger.slice(1);
}

if (owner.numeralIntegerScale > 0) {
partInteger = partInteger.slice(0, owner.numeralIntegerScale + (value.slice(0, 1) === '-' ? 1 : 0));
partInteger = partInteger.slice(0, owner.numeralIntegerScale);
}

switch (owner.numeralThousandsGroupStyle) {
Expand All @@ -89,7 +108,7 @@ NumeralFormatter.prototype = {
break;
}

return partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '');
return partSignAndPrefix + partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '');
}
};

Expand Down
77 changes: 77 additions & 0 deletions test/fixtures/numeral.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,83 @@
]
]
},
{
"thousandsGroupStyle": "thousand",
"prefix": "$",
"numbers": [
[
"1234567",
"$1,234,567"
],
[
"-1234567",
"$-1,234,567"
],
[
"01234567",
"$1,234,567"
],
[
"-01234567",
"$-1,234,567"
],
[
"0.1234",
"$0.12"
],
[
"-0.1234",
"$-0.12"
],
[
"00.1234",
"$0.12"
],
[
"-00.1234",
"$-0.12"
]
]
},
{
"thousandsGroupStyle": "thousand",
"prefix": "$",
"signBeforePrefix": true,
"numbers": [
[
"1234567",
"$1,234,567"
],
[
"-1234567",
"-$1,234,567"
],
[
"01234567",
"$1,234,567"
],
[
"-01234567",
"-$1,234,567"
],
[
"0.1234",
"$0.12"
],
[
"-0.1234",
"-$0.12"
],
[
"00.1234",
"$0.12"
],
[
"-00.1234",
"-$0.12"
]
]
},
{
"thousandsGroupStyle": "lakh",
"numbers": [
Expand Down
10 changes: 10 additions & 0 deletions test/unit/NumeralFormatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe('NumeralFormatter', function () {
title.push('Strip leading zeroes:' + numeral.stripLeadingZeroes);
}

if (numeral.prefix) {
title.push('Prefix:' + numeral.prefix);
}

if (numeral.signBeforePrefix) {
title.push('Sign before prefix:' + numeral.signBeforePrefix);
}

describe(title.join(', '), function () {
var numeralFormatter = new NumeralFormatter(
numeral.numeralDecimalMark,
Expand All @@ -46,6 +54,8 @@ describe('NumeralFormatter', function () {
numeral.thousandsGroupStyle,
numeral.numeralPositiveOnly,
numeral.stripLeadingZeroes,
numeral.prefix,
numeral.signBeforePrefix,
numeral.delimiter
);

Expand Down

0 comments on commit 112c530

Please sign in to comment.