Skip to content

Commit

Permalink
[Issue-196] Add numeral integer scale
Browse files Browse the repository at this point in the history
  • Loading branch information
nosir committed Jun 24, 2017
1 parent 05ccd32 commit eb8dc0a
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"form",
"input"
],
"version": "0.7.19",
"version": "0.7.20",
"author": {
"name": "Max Huang",
"email": "[email protected]",
Expand Down
8 changes: 8 additions & 0 deletions dist/cleave-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ return /******/ (function(modules) { // webpackBootstrap

pps.numeralFormatter = new Cleave.NumeralFormatter(
pps.numeralDecimalMark,
pps.numeralIntegerScale,
pps.numeralDecimalScale,
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
Expand Down Expand Up @@ -424,13 +425,15 @@ return /******/ (function(modules) { // webpackBootstrap
'use strict';

var NumeralFormatter = function (numeralDecimalMark,
numeralIntegerScale,
numeralDecimalScale,
numeralThousandsGroupStyle,
numeralPositiveOnly,
delimiter) {
var owner = this;

owner.numeralDecimalMark = numeralDecimalMark || '.';
owner.numeralIntegerScale = numeralIntegerScale >= 0 ? numeralIntegerScale : 10;
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
Expand Down Expand Up @@ -484,6 +487,10 @@ return /******/ (function(modules) { // webpackBootstrap
partDecimal = owner.numeralDecimalMark + parts[1].slice(0, owner.numeralDecimalScale);
}

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

switch (owner.numeralThousandsGroupStyle) {
case NumeralFormatter.groupStyle.lakh:
partInteger = partInteger.replace(/(\d)(?=(\d\d)+\d$)/g, '$1' + owner.delimiter);
Expand Down Expand Up @@ -946,6 +953,7 @@ return /******/ (function(modules) { // webpackBootstrap

// numeral
target.numeral = !!opts.numeral;
target.numeralIntegerScale = opts.numeralIntegerScale >= 0 ? opts.numeralIntegerScale : 10;
target.numeralDecimalScale = opts.numeralDecimalScale >= 0 ? opts.numeralDecimalScale : 2;
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
Expand Down
4 changes: 2 additions & 2 deletions dist/cleave-angular.min.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions dist/cleave-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ return /******/ (function(modules) { // webpackBootstrap
return;
}

pps.numeralFormatter = new NumeralFormatter(pps.numeralDecimalMark, pps.numeralDecimalScale, pps.numeralThousandsGroupStyle, pps.numeralPositiveOnly, pps.delimiter);
pps.numeralFormatter = new NumeralFormatter(pps.numeralDecimalMark, pps.numeralIntegerScale, pps.numeralDecimalScale, pps.numeralThousandsGroupStyle, pps.numeralPositiveOnly, pps.delimiter);
},

initDateFormatter: function initDateFormatter() {
Expand Down Expand Up @@ -1582,10 +1582,11 @@ return /******/ (function(modules) { // webpackBootstrap

'use strict';

var NumeralFormatter = function NumeralFormatter(numeralDecimalMark, numeralDecimalScale, numeralThousandsGroupStyle, numeralPositiveOnly, delimiter) {
var NumeralFormatter = function NumeralFormatter(numeralDecimalMark, numeralIntegerScale, numeralDecimalScale, numeralThousandsGroupStyle, numeralPositiveOnly, delimiter) {
var owner = this;

owner.numeralDecimalMark = numeralDecimalMark || '.';
owner.numeralIntegerScale = numeralIntegerScale >= 0 ? numeralIntegerScale : 10;
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
Expand Down Expand Up @@ -1642,6 +1643,10 @@ return /******/ (function(modules) { // webpackBootstrap
partDecimal = owner.numeralDecimalMark + parts[1].slice(0, owner.numeralDecimalScale);
}

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

switch (owner.numeralThousandsGroupStyle) {
case NumeralFormatter.groupStyle.lakh:
partInteger = partInteger.replace(/(\d)(?=(\d\d)+\d$)/g, '$1' + owner.delimiter);
Expand Down Expand Up @@ -2099,6 +2104,7 @@ return /******/ (function(modules) { // webpackBootstrap

// numeral
target.numeral = !!opts.numeral;
target.numeralIntegerScale = opts.numeralIntegerScale >= 0 ? opts.numeralIntegerScale : 10;
target.numeralDecimalScale = opts.numeralDecimalScale >= 0 ? opts.numeralDecimalScale : 2;
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
Expand Down
4 changes: 2 additions & 2 deletions dist/cleave-react.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions dist/cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ return /******/ (function(modules) { // webpackBootstrap

pps.numeralFormatter = new Cleave.NumeralFormatter(
pps.numeralDecimalMark,
pps.numeralIntegerScale,
pps.numeralDecimalScale,
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
Expand Down Expand Up @@ -424,13 +425,15 @@ return /******/ (function(modules) { // webpackBootstrap
'use strict';

var NumeralFormatter = function (numeralDecimalMark,
numeralIntegerScale,
numeralDecimalScale,
numeralThousandsGroupStyle,
numeralPositiveOnly,
delimiter) {
var owner = this;

owner.numeralDecimalMark = numeralDecimalMark || '.';
owner.numeralIntegerScale = numeralIntegerScale >= 0 ? numeralIntegerScale : 10;
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
Expand Down Expand Up @@ -484,6 +487,10 @@ return /******/ (function(modules) { // webpackBootstrap
partDecimal = owner.numeralDecimalMark + parts[1].slice(0, owner.numeralDecimalScale);
}

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

switch (owner.numeralThousandsGroupStyle) {
case NumeralFormatter.groupStyle.lakh:
partInteger = partInteger.replace(/(\d)(?=(\d\d)+\d$)/g, '$1' + owner.delimiter);
Expand Down Expand Up @@ -946,6 +953,7 @@ return /******/ (function(modules) { // webpackBootstrap

// numeral
target.numeral = !!opts.numeral;
target.numeralIntegerScale = opts.numeralIntegerScale >= 0 ? opts.numeralIntegerScale : 10;
target.numeralDecimalScale = opts.numeralDecimalScale >= 0 ? opts.numeralDecimalScale : 2;
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
Expand Down
4 changes: 2 additions & 2 deletions dist/cleave.min.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Numerals:
- [numeral](#numeral)
- [numeralThousandsGroupStyle](#numeralthousandsgroupstyle)
- [numeralIntegerScale](#numeralintegerscale)
- [numeralDecimalScale](#numeraldecimalscale)
- [numeralDecimalMark](#numeraldecimalmark)
- [numeralPositiveOnly](#numeralpositiveonly)
Expand Down Expand Up @@ -197,6 +198,23 @@ new Cleave('.my-input', {
// 123,4567.89
```

### `numeralIntegerScale`

An `Int` value indicates the numeral integer scale.

**Default value**: `10`

```js
new Cleave('.my-input', {
numeral: true,
numeralIntegerScale: 4
});
```

```js
// 1,234.56
```

### `numeralDecimalScale`

An `Int` value indicates the numeral decimal scale.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"form",
"input"
],
"version": "0.7.19",
"version": "0.7.20",
"author": {
"name": "Max Huang",
"url": "http://github.com/nosir",
Expand Down
1 change: 1 addition & 0 deletions src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Cleave.prototype = {

pps.numeralFormatter = new Cleave.NumeralFormatter(
pps.numeralDecimalMark,
pps.numeralIntegerScale,
pps.numeralDecimalScale,
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
Expand Down
1 change: 1 addition & 0 deletions src/Cleave.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var Cleave = CreateReactClass({

pps.numeralFormatter = new NumeralFormatter(
pps.numeralDecimalMark,
pps.numeralIntegerScale,
pps.numeralDecimalScale,
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
Expand Down
1 change: 1 addition & 0 deletions src/common/DefaultProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var DefaultProperties = {

// numeral
target.numeral = !!opts.numeral;
target.numeralIntegerScale = opts.numeralIntegerScale >= 0 ? opts.numeralIntegerScale : 10;
target.numeralDecimalScale = opts.numeralDecimalScale >= 0 ? opts.numeralDecimalScale : 2;
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
Expand Down
6 changes: 6 additions & 0 deletions src/shortcuts/NumeralFormatter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

var NumeralFormatter = function (numeralDecimalMark,
numeralIntegerScale,
numeralDecimalScale,
numeralThousandsGroupStyle,
numeralPositiveOnly,
delimiter) {
var owner = this;

owner.numeralDecimalMark = numeralDecimalMark || '.';
owner.numeralIntegerScale = numeralIntegerScale >= 0 ? numeralIntegerScale : 10;
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
Expand Down Expand Up @@ -61,6 +63,10 @@ NumeralFormatter.prototype = {
partDecimal = owner.numeralDecimalMark + parts[1].slice(0, owner.numeralDecimalScale);
}

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

switch (owner.numeralThousandsGroupStyle) {
case NumeralFormatter.groupStyle.lakh:
partInteger = partInteger.replace(/(\d)(?=(\d\d)+\d$)/g, '$1' + owner.delimiter);
Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/numeral.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@
]
]
},
{
"numeralIntegerScale": 4,
"numbers": [
[
"12",
"12"
],
[
"1234",
"1,234"
],
[
"1234567",
"1,234"
],
[
"-12345",
"-1,234"
]
]
},
{
"numeralDecimalScale": 0,
"numbers": [
Expand Down Expand Up @@ -195,6 +216,23 @@
]
]
},
{
"thousandsGroupStyle": "lakh",
"numeralIntegerScale": 6,
"numeralDecimalScale": 3,
"numeralDecimalMark": "'",
"delimiter": "|",
"numbers": [
[
"1234567'8901234",
"1|23|456'890"
],
[
"-1234567'8901234",
"-1|23|456'890"
]
]
},
{
"thousandsGroupStyle": "thousand",
"numeralPositiveOnly": true,
Expand Down
5 changes: 5 additions & 0 deletions test/unit/NumeralFormatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('NumeralFormatter', function () {
title.push('Thousands Group Style: ' + numeral.thousandsGroupStyle);
}

if (numeral.numeralIntegerScale) {
title.push('Integer Scale: ' + numeral.numeralIntegerScale);
}

if (numeral.numeralDecimalScale || numeral.numeralDecimalScale === 0) {
title.push('Decimal Scale: ' + numeral.numeralDecimalScale);
}
Expand All @@ -33,6 +37,7 @@ describe('NumeralFormatter', function () {
describe(title.join(', '), function () {
var numeralFormatter = new NumeralFormatter(
numeral.numeralDecimalMark,
numeral.numeralIntegerScale,
numeral.numeralDecimalScale,
numeral.thousandsGroupStyle,
numeral.numeralPositiveOnly,
Expand Down

0 comments on commit eb8dc0a

Please sign in to comment.