Skip to content

Commit

Permalink
Merge pull request #27 from zestia/add-natural-number-fix
Browse files Browse the repository at this point in the history
Fix whole number constraint option not matching some numbers with zeros
  • Loading branch information
DanaKirsh authored Jan 31, 2024
2 parents f49a221 + 914bacb commit 1bbfa1c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 5.2.1

- Fix whole number constraint option not matching some numbers with zeros

## 5.2.0

- Add whole number constraint option
Expand Down
2 changes: 1 addition & 1 deletion addon/-private/constraints/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function number(options = {}) {
}

if (options.whole) {
if (/^([0-9]|[1-9,]+)$/i.test(value)) {
if (/^([0-9,]+)$/i.test(value)) {
return;
}
} else if (/^-?[0-9,.]+$/i.test(value)) {
Expand Down
22 changes: 16 additions & 6 deletions tests/unit/constraints/number-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module('number', function (hooks) {
});

test('inputs, non-whole', function (assert) {
assert.expect(13);
assert.expect(15);

const validExamples = [
12345,
Expand All @@ -65,27 +65,37 @@ module('number', function (hooks) {
'123.45',
'01',
0,
'0'
'0',
10,
'10'
];
const invalidExamples = [null, '', 'abc'];

testInputs(assert, {}, validExamples, invalidExamples);
});

test('inputs, whole', function (assert) {
assert.expect(14);
assert.expect(15);

const options = { whole: true };
const validExamples = [0, '0', 12345, '12345', 123456789, '123,456,789'];
const validExamples = [
0,
'0',
12345,
'12345',
123456789,
'123,456,789',
10,
'10'
];
const invalidExamples = [
null,
'',
'abc',
-12345,
'-12345',
123.45,
'123.45',
'01'
'123.45'
];

testInputs(assert, options, validExamples, invalidExamples);
Expand Down

0 comments on commit 1bbfa1c

Please sign in to comment.