Skip to content

Commit

Permalink
feature: strict positive number
Browse files Browse the repository at this point in the history
Signed-off-by: Changyu Geng <[email protected]>
  • Loading branch information
KingMario committed Jun 21, 2021
1 parent e3fa6f3 commit 4edffcd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description":
"JavaScript library for formatting input text content when you are typing",
"keywords": ["cleave", "javascript", "html", "form", "input"],
"version": "1.6.0",
"version": "1.6.1",
"author": {
"name": "Max Huang",
"email": "[email protected]",
Expand Down
17 changes: 16 additions & 1 deletion doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ new Cleave('.my-input', {

### `numeralPositiveOnly`

A `Boolean` value indicates if it only allows positive numeral value
A `Boolean` or `String` value indicates if it only allows positive numeral value

For `String` type, only the word `strict` in lowercase is available to indicate even `zero (0)` is not acceptable.

**Default value**: `false`

Expand All @@ -351,9 +353,22 @@ new Cleave('.my-input', {
```

```js
// -1234.56 entered
// 1234.56
```

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

```js
// `zero` (-0.0000) entered
// (blank)
```

### `signBeforePrefix`

A `Boolean` value indicates if the sign of the numeral should appear before the prefix.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"vanilla",
"react"
],
"version": "1.6.0",
"version": "1.6.1",
"files": [
"src",
"dist",
Expand Down
6 changes: 6 additions & 0 deletions src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* @param {String | HTMLElement} element
* @param {Object} opts
*/

// polyfill Number.EPSILON
if (Number.EPSILON === undefined) {
Number.EPSILON = Math.pow(2, -52);
}

var Cleave = function (element, opts) {
var owner = this;
var hasMultipleElements = false;
Expand Down
4 changes: 2 additions & 2 deletions src/common/DefaultProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ var DefaultProperties = {
target.numeralDecimalScale = opts.numeralDecimalScale >= 0 ? opts.numeralDecimalScale : 2;
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
target.numeralPositiveOnly = opts.numeralPositiveOnly;
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
target.signBeforePrefix = !!opts.signBeforePrefix;
target.tailPrefix = !!opts.tailPrefix;

// others
target.swapHiddenInput = !!opts.swapHiddenInput;

target.numericOnly = target.creditCard || target.date || !!opts.numericOnly;

target.uppercase = !!opts.uppercase;
Expand Down
14 changes: 10 additions & 4 deletions src/shortcuts/NumeralFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var NumeralFormatter = function (numeralDecimalMark,
owner.numeralIntegerScale = numeralIntegerScale > 0 ? numeralIntegerScale : 0;
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
owner.numeralPositiveOnly = numeralPositiveOnly;
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
owner.prefix = (prefix || prefix === '') ? prefix : '';
owner.signBeforePrefix = !!signBeforePrefix;
Expand All @@ -29,7 +29,7 @@ NumeralFormatter.groupStyle = {
thousand: 'thousand',
lakh: 'lakh',
wan: 'wan',
none: 'none'
none: 'none'
};

NumeralFormatter.prototype = {
Expand All @@ -39,6 +39,7 @@ NumeralFormatter.prototype = {

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

// strip alphabet letters
value = value.replace(/[A-Za-z]/g, '')
Expand All @@ -56,11 +57,16 @@ NumeralFormatter.prototype = {
.replace(/\-/g, '')

// replace the minus sign (if present)
.replace('N', owner.numeralPositiveOnly ? '' : '-')
.replace('N', (owner.numeralPositiveOnly === true
|| owner.numeralPositiveOnly === strStrict) ? '' : '-')

// replace decimal mark
.replace('M', owner.numeralDecimalMark);

if (owner.numeralPositiveOnly === strStrict && parseFloat(value) < Number.EPSILON) {
value = '';
}

// strip any leading zeros
if (owner.stripLeadingZeroes) {
value = value.replace(/^(-)?0+(?=\d)/, '$1');
Expand All @@ -76,7 +82,7 @@ NumeralFormatter.prototype = {
} else {
partSignAndPrefix = partSign;
}

partInteger = value;

if (value.indexOf(owner.numeralDecimalMark) >= 0) {
Expand Down
10 changes: 10 additions & 0 deletions test/browser/numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ describe('Numeral input field', function () {
assert.equal(field.value, '1,234.56');
});

it('should use defined strict positive only option', function () {
var cleave = new Cleave(field, {
numeral: true,
numeralPositiveOnly: 'strict'
});

cleave.setRawValue('-0.000');
assert.equal(field.value, '');
});

it('it should not strip leading zeroes', function () {
var cleave = new Cleave(field, {
numeral: true,
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/numeral.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,24 @@
]
]
},
{
"thousandsGroupStyle": "thousand",
"numeralPositiveOnly": "strict",
"numbers": [
[
"1234567",
"1,234,567"
],
[
"-1234567",
"1,234,567"
],
[
"-0.0000",
""
]
]
},
{
"thousandsGroupStyle": "thousand",
"stripLeadingZeroes": false,
Expand Down

0 comments on commit 4edffcd

Please sign in to comment.