diff --git a/bower.json b/bower.json index 288381b5..64685038 100644 --- a/bower.json +++ b/bower.json @@ -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": "risonhuang@gmail.com", diff --git a/doc/options.md b/doc/options.md index 5cdd7bf8..c7de05fa 100644 --- a/doc/options.md +++ b/doc/options.md @@ -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` @@ -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. diff --git a/package-lock.json b/package-lock.json index 8b99a82a..651c086f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cleave.js", - "version": "1.5.3", + "version": "1.6.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c12a3a65..60bf975c 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "vanilla", "react" ], - "version": "1.6.0", + "version": "1.6.1", "files": [ "src", "dist", diff --git a/src/Cleave.js b/src/Cleave.js index 37f3412d..0cf29cbd 100644 --- a/src/Cleave.js +++ b/src/Cleave.js @@ -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; diff --git a/src/common/DefaultProperties.js b/src/common/DefaultProperties.js index 224cd0f0..30f24deb 100644 --- a/src/common/DefaultProperties.js +++ b/src/common/DefaultProperties.js @@ -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; diff --git a/src/shortcuts/NumeralFormatter.js b/src/shortcuts/NumeralFormatter.js index 626215e7..a391a744 100644 --- a/src/shortcuts/NumeralFormatter.js +++ b/src/shortcuts/NumeralFormatter.js @@ -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; @@ -29,7 +29,7 @@ NumeralFormatter.groupStyle = { thousand: 'thousand', lakh: 'lakh', wan: 'wan', - none: 'none' + none: 'none' }; NumeralFormatter.prototype = { @@ -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, '') @@ -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'); @@ -76,7 +82,7 @@ NumeralFormatter.prototype = { } else { partSignAndPrefix = partSign; } - + partInteger = value; if (value.indexOf(owner.numeralDecimalMark) >= 0) { diff --git a/test/browser/numeral.js b/test/browser/numeral.js index 0cb0a035..041435ef 100644 --- a/test/browser/numeral.js +++ b/test/browser/numeral.js @@ -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, diff --git a/test/fixtures/numeral.json b/test/fixtures/numeral.json index 8799d12d..5d26b7bd 100644 --- a/test/fixtures/numeral.json +++ b/test/fixtures/numeral.json @@ -393,6 +393,24 @@ ] ] }, + { + "thousandsGroupStyle": "thousand", + "numeralPositiveOnly": "strict", + "numbers": [ + [ + "1234567", + "1,234,567" + ], + [ + "-1234567", + "1,234,567" + ], + [ + "-0.0000", + "" + ] + ] + }, { "thousandsGroupStyle": "thousand", "stripLeadingZeroes": false,