Skip to content

Commit

Permalink
Change returned value of positiveNumber() and positiveInteger() a…
Browse files Browse the repository at this point in the history
…round -0 case
  • Loading branch information
nvie committed Sep 21, 2023
1 parent 8ac1a93 commit d0739fd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v2.0.5

- The returned value for `positiveInteger(-0)` is now `0`, not `-0`
- The returned value for `positiveNumber(-0)` is now `0`, not `-0`

## v2.0.4

- Fix a bug in the `url` decoder, which could incorrectly reject URLs with a `/` in the
Expand Down
4 changes: 4 additions & 0 deletions docs/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
'example': """
// 👍
positiveNumber.verify(123) === 123;
positiveNumber.verify(0) === 0;
positiveNumber.verify(-0) === 0;
// 👎
positiveNumber.verify(-42); // throws
Expand All @@ -222,6 +224,8 @@
'example': """
// 👍
positiveInteger.verify(123) === 123;
positiveInteger.verify(0) === 0;
positiveInteger.verify(-0) === 0;
// 👎
positiveInteger.verify(-3); // throws
Expand Down
14 changes: 7 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ integer.verify('not a integer'); // throws

---

<a href="#positiveNumber">#</a> **positiveNumber**: <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;number&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/lib/numbers.js#L34-L40 'Source')
<a href="#positiveNumber">#</a> **positiveNumber**: <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;number&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/lib/numbers.js#L34-L39 'Source')
{: #positiveNumber .signature}

Accepts only non-negative (zero or positive) finite numbers.
Expand All @@ -329,7 +329,7 @@ Accepts only non-negative (zero or positive) finite numbers.
// 👍
positiveNumber.verify(123) === 123;
positiveNumber.verify(0) === 0;
positiveNumber.verify(-0) === -0;
positiveNumber.verify(-0) === 0;

// 👎
positiveNumber.verify(-42); // throws
Expand All @@ -341,7 +341,7 @@ positiveNumber.verify('not a number'); // throws

---

<a href="#positiveInteger">#</a> **positiveInteger**: <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;number&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/lib/numbers.js#L42-L48 'Source')
<a href="#positiveInteger">#</a> **positiveInteger**: <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;number&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/lib/numbers.js#L41-L46 'Source')
{: #positiveInteger .signature}

Accepts only non-negative (zero or positive) finite whole numbers.
Expand All @@ -350,7 +350,7 @@ Accepts only non-negative (zero or positive) finite whole numbers.
// 👍
positiveInteger.verify(123) === 123;
positiveInteger.verify(0) === 0;
positiveInteger.verify(-0) === -0;
positiveInteger.verify(-0) === 0;

// 👎
positiveInteger.verify(-3); // throws
Expand Down Expand Up @@ -515,7 +515,7 @@ iso8601.verify(new Date()); // throws (does not accept dates)

Accepts only the given constant value.

> _![](./assets/tiny-flow-logo.png) **Note to Flow users!** Flow will incorrectly infer the type for constants by default! The inferred type for `constant(42)` is `Decoder<number>`. To work around this, always use this syntax in Flow: `constant((42: 42))`._
> _![](./assets/tiny-flow-logo.png) **Note to Flow users!** Flow will incorrectly infer the type for constants by default! The inferred type for `constant(42)` is `Decoder<number>`. To work around this, always use this syntax in Flow: `constant((42: 42))`._
> _![](./assets/tiny-ts-logo.png) **TypeScript** will correctly infer the type of `constant(42)` as `Decoder<42>`._
```typescript
Expand Down Expand Up @@ -1159,7 +1159,7 @@ For example, given an array of strings, like so:
oneOf(['foo', 'bar']);
```

> _![](./assets/tiny-flow-logo.png) **Note to Flow users!** Flow will (unfortunately) infer the type of this definition as `Decoder<string>`. To work around this, be sure to explicitly annotate the type. Either by doing `oneOf([('foo': 'foo'), ('bar': 'bar')])`, or as `oneOf<'foo' | 'bar'>(['foo', 'bar'])`._
> _![](./assets/tiny-flow-logo.png) **Note to Flow users!** Flow will (unfortunately) infer the type of this definition as `Decoder<string>`. To work around this, be sure to explicitly annotate the type. Either by doing `oneOf([('foo': 'foo'), ('bar': 'bar')])`, or as `oneOf<'foo' | 'bar'>(['foo', 'bar'])`._
> _![](./assets/tiny-ts-logo.png) **TypeScript** will correctly infer the return type as `Decoder<'foo' | 'bar'>`._
---
Expand Down Expand Up @@ -1351,5 +1351,5 @@ const treeDecoder: Decoder<Tree> = object({
});
```

<!--[[[end]]] (checksum: 143996692adad212d5d3887b8380b3ca) -->
<!--[[[end]]] (checksum: add40a56e2d8eeedecc0bf78e720db4b) -->
<!-- prettier-ignore-end -->
1 change: 1 addition & 0 deletions src/lib/__tests__/_fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const STRINGS: Array<string> = [
export const NUMBERS: Array<number> = [
-317.827682288236872383242082309328432093279,
-1,
-0,
0,
1,
2,
Expand Down
6 changes: 4 additions & 2 deletions src/lib/__tests__/numbers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('positiveNumber', () => {
it('valid', () => {
expect(okay.length).not.toBe(0);
for (const value of okay) {
expect(decoder.verify(value)).toBe(value);
expect(decoder.verify(value)).toBe(value === 0 ? 0 : value);
expect(decoder.verify(value)).not.toBe(-0);
}
});

Expand Down Expand Up @@ -94,7 +95,8 @@ describe('positiveInteger', () => {
it('valid', () => {
expect(okay.length).not.toBe(0);
for (const value of okay) {
expect(decoder.verify(value)).toBe(value);
expect(decoder.verify(value)).toBe(value === 0 ? 0 : value);
expect(decoder.verify(value)).not.toBe(-0);
}
});

Expand Down
14 changes: 6 additions & 8 deletions src/lib/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ export const integer: Decoder<number> = number.refine(
/**
* Accepts only non-negative (zero or positive) finite numbers.
*/
export const positiveNumber: Decoder<number> = number.refine(
(n) => n >= 0,
'Number must be positive',
);
export const positiveNumber: Decoder<number> = number
.refine((n) => n >= 0, 'Number must be positive')
.transform(Math.abs);

/**
* Accepts only non-negative (zero or positive) finite whole numbers.
*/
export const positiveInteger: Decoder<number> = integer.refine(
(n) => n >= 0,
'Number must be positive',
);
export const positiveInteger: Decoder<number> = integer
.refine((n) => n >= 0, 'Number must be positive')
.transform(Math.abs);

0 comments on commit d0739fd

Please sign in to comment.