Skip to content

Commit 4edffcd

Browse files
committed
feature: strict positive number
Signed-off-by: Changyu Geng <[email protected]>
1 parent e3fa6f3 commit 4edffcd

File tree

9 files changed

+65
-10
lines changed

9 files changed

+65
-10
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description":
44
"JavaScript library for formatting input text content when you are typing",
55
"keywords": ["cleave", "javascript", "html", "form", "input"],
6-
"version": "1.6.0",
6+
"version": "1.6.1",
77
"author": {
88
"name": "Max Huang",
99
"email": "[email protected]",

doc/options.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ new Cleave('.my-input', {
339339

340340
### `numeralPositiveOnly`
341341

342-
A `Boolean` value indicates if it only allows positive numeral value
342+
A `Boolean` or `String` value indicates if it only allows positive numeral value
343+
344+
For `String` type, only the word `strict` in lowercase is available to indicate even `zero (0)` is not acceptable.
343345

344346
**Default value**: `false`
345347

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

353355
```js
356+
// -1234.56 entered
354357
// 1234.56
355358
```
356359

360+
```js
361+
new Cleave('.my-input', {
362+
numeral: true,
363+
numeralPositiveOnly: 'strict'
364+
});
365+
```
366+
367+
```js
368+
// `zero` (-0.0000) entered
369+
// (blank)
370+
```
371+
357372
### `signBeforePrefix`
358373

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

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"vanilla",
1717
"react"
1818
],
19-
"version": "1.6.0",
19+
"version": "1.6.1",
2020
"files": [
2121
"src",
2222
"dist",

src/Cleave.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
* @param {String | HTMLElement} element
77
* @param {Object} opts
88
*/
9+
10+
// polyfill Number.EPSILON
11+
if (Number.EPSILON === undefined) {
12+
Number.EPSILON = Math.pow(2, -52);
13+
}
14+
915
var Cleave = function (element, opts) {
1016
var owner = this;
1117
var hasMultipleElements = false;

src/common/DefaultProperties.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ var DefaultProperties = {
4242
target.numeralDecimalScale = opts.numeralDecimalScale >= 0 ? opts.numeralDecimalScale : 2;
4343
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
4444
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
45-
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
45+
target.numeralPositiveOnly = opts.numeralPositiveOnly;
4646
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
4747
target.signBeforePrefix = !!opts.signBeforePrefix;
4848
target.tailPrefix = !!opts.tailPrefix;
4949

5050
// others
5151
target.swapHiddenInput = !!opts.swapHiddenInput;
52-
52+
5353
target.numericOnly = target.creditCard || target.date || !!opts.numericOnly;
5454

5555
target.uppercase = !!opts.uppercase;

src/shortcuts/NumeralFormatter.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var NumeralFormatter = function (numeralDecimalMark,
1616
owner.numeralIntegerScale = numeralIntegerScale > 0 ? numeralIntegerScale : 0;
1717
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
1818
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
19-
owner.numeralPositiveOnly = !!numeralPositiveOnly;
19+
owner.numeralPositiveOnly = numeralPositiveOnly;
2020
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
2121
owner.prefix = (prefix || prefix === '') ? prefix : '';
2222
owner.signBeforePrefix = !!signBeforePrefix;
@@ -29,7 +29,7 @@ NumeralFormatter.groupStyle = {
2929
thousand: 'thousand',
3030
lakh: 'lakh',
3131
wan: 'wan',
32-
none: 'none'
32+
none: 'none'
3333
};
3434

3535
NumeralFormatter.prototype = {
@@ -39,6 +39,7 @@ NumeralFormatter.prototype = {
3939

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

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

5859
// replace the minus sign (if present)
59-
.replace('N', owner.numeralPositiveOnly ? '' : '-')
60+
.replace('N', (owner.numeralPositiveOnly === true
61+
|| owner.numeralPositiveOnly === strStrict) ? '' : '-')
6062

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

66+
if (owner.numeralPositiveOnly === strStrict && parseFloat(value) < Number.EPSILON) {
67+
value = '';
68+
}
69+
6470
// strip any leading zeros
6571
if (owner.stripLeadingZeroes) {
6672
value = value.replace(/^(-)?0+(?=\d)/, '$1');
@@ -76,7 +82,7 @@ NumeralFormatter.prototype = {
7682
} else {
7783
partSignAndPrefix = partSign;
7884
}
79-
85+
8086
partInteger = value;
8187

8288
if (value.indexOf(owner.numeralDecimalMark) >= 0) {

test/browser/numeral.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ describe('Numeral input field', function () {
7171
assert.equal(field.value, '1,234.56');
7272
});
7373

74+
it('should use defined strict positive only option', function () {
75+
var cleave = new Cleave(field, {
76+
numeral: true,
77+
numeralPositiveOnly: 'strict'
78+
});
79+
80+
cleave.setRawValue('-0.000');
81+
assert.equal(field.value, '');
82+
});
83+
7484
it('it should not strip leading zeroes', function () {
7585
var cleave = new Cleave(field, {
7686
numeral: true,

test/fixtures/numeral.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,24 @@
393393
]
394394
]
395395
},
396+
{
397+
"thousandsGroupStyle": "thousand",
398+
"numeralPositiveOnly": "strict",
399+
"numbers": [
400+
[
401+
"1234567",
402+
"1,234,567"
403+
],
404+
[
405+
"-1234567",
406+
"1,234,567"
407+
],
408+
[
409+
"-0.0000",
410+
""
411+
]
412+
]
413+
},
396414
{
397415
"thousandsGroupStyle": "thousand",
398416
"stripLeadingZeroes": false,

0 commit comments

Comments
 (0)