-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the number Cake reject NaN (#64)
- Loading branch information
1 parent
85eca42
commit f421682
Showing
10 changed files
with
111 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
Cake, | ||
CakeDispatchCheckContext, | ||
CakeDispatchStringifyContext, | ||
CakeError, | ||
CakeErrorDispatchFormatContext, | ||
StringTree, | ||
WrongTypeCakeError, | ||
} from "./index-internal"; | ||
|
||
/** | ||
* See {@link number}. | ||
* | ||
* @public | ||
*/ | ||
class NumberCake extends Cake<number> { | ||
dispatchCheck( | ||
value: unknown, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
context: CakeDispatchCheckContext | ||
): CakeError | null { | ||
if (typeof value !== "number") { | ||
return new WrongTypeCakeError(this, value); | ||
} | ||
if (Number.isNaN(value)) { | ||
return new IsNaNCakeError(); | ||
} | ||
return null; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
dispatchStringify(context: CakeDispatchStringifyContext): string { | ||
return "number"; | ||
} | ||
|
||
withName(name: string | null): NumberCake { | ||
return new NumberCake({ ...this, name }); | ||
} | ||
} | ||
|
||
class IsNaNCakeError extends CakeError { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
dispatchFormat(context: CakeErrorDispatchFormatContext): StringTree { | ||
return "Value is NaN."; | ||
} | ||
} | ||
|
||
/** | ||
* A {@link Cake} representing the `number` type. | ||
* | ||
* @example | ||
* ```ts | ||
* number.is(5); // true | ||
* number.is("5"); // false | ||
* ``` | ||
* | ||
* @example Although `typeof NaN === "number"`, this Cake does not accept `NaN`: | ||
* ```ts | ||
* number.is(NaN); // false | ||
* number.as(NaN); // TypeError: Value is NaN. | ||
* ``` | ||
* | ||
* @public | ||
*/ | ||
const number = new NumberCake({}); | ||
|
||
export { NumberCake, number }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { number } from "../../src"; | ||
import { expectTypeError } from "../test-helpers"; | ||
|
||
describe("documentation examples", () => { | ||
test("number", () => { | ||
expect(number.is(5)).toStrictEqual(true); | ||
expect(number.is("5")).toStrictEqual(false); | ||
}); | ||
|
||
test("number NaN", () => { | ||
expect(number.is(NaN)).toStrictEqual(false); | ||
expectTypeError(() => number.as(NaN), "Value is NaN."); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters