Skip to content

Commit

Permalink
Fix whole number constraint option not matching some numbers with zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
DanaKirsh committed Jan 30, 2024
1 parent f49a221 commit fef2053
Show file tree
Hide file tree
Showing 3 changed files with 22 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
23 changes: 17 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,38 @@ 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'
// '01'
];

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

0 comments on commit fef2053

Please sign in to comment.