-
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.
Added assert package and added basic asserts
- Loading branch information
1 parent
800413b
commit a87f4b0
Showing
13 changed files
with
302 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# @stdext/assert | ||
|
||
The assert package, contains validators and assertions | ||
|
||
```ts | ||
import { assertIsString, isString } from "@stdext/assert"; | ||
|
||
if (isString(someVar)) { | ||
// Returns true if a value is a string | ||
// someVar will typewise be a string from now on | ||
} | ||
|
||
assertIsString(someVar); // Throws if the value is not a string | ||
// someVar will typewise be a string from now on | ||
``` |
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,10 @@ | ||
{ | ||
"version": "0.0.1", | ||
"name": "@stdext/assert", | ||
"exports": { | ||
".": "./mod.ts", | ||
"./is_number": "./is_number.ts", | ||
"./is_record": "./is_record.ts", | ||
"./is_string": "./is_string.ts" | ||
} | ||
} |
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,47 @@ | ||
import { assert, assertFalse, AssertionError, assertThrows } from "@std/assert"; | ||
import { assertIsNumber, isNumber } from "./is_number.ts"; | ||
|
||
const VALID = [ | ||
0, | ||
1, | ||
1.123, | ||
1_123_12_3, | ||
-1, | ||
-1_1234, | ||
-1.123, | ||
NaN, | ||
]; | ||
|
||
const INVALID = [ | ||
"", | ||
"asdf", | ||
undefined, | ||
null, | ||
{}, | ||
[], | ||
[""], | ||
new Number(), | ||
]; | ||
|
||
Deno.test("isNumber > can detect numbers", () => { | ||
for (const v of VALID) { | ||
assert(isNumber(v), `Value of '${v}' is not valid`); | ||
} | ||
for (const v of INVALID) { | ||
assertFalse(isNumber(v), `Value of '${v}' is not invalid`); | ||
} | ||
}); | ||
|
||
Deno.test("assertIsNumber > can detect numbers", () => { | ||
for (const v of VALID) { | ||
assertIsNumber(v); | ||
} | ||
for (const v of INVALID) { | ||
assertThrows( | ||
() => assertIsNumber(v), | ||
AssertionError, | ||
undefined, | ||
`Value of '${v}' did not throw`, | ||
); | ||
} | ||
}); |
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,17 @@ | ||
import { AssertionError } from "@std/assert"; | ||
|
||
/** | ||
* Checks if a value is a number | ||
*/ | ||
export function isNumber(value: unknown): value is number { | ||
return typeof value === "number"; | ||
} | ||
|
||
/** | ||
* Asserts that a value is a number | ||
*/ | ||
export function assertIsNumber(value: unknown): asserts value is number { | ||
if (!isNumber(value)) { | ||
throw new AssertionError(`Value is not a number, was '${value}'`); | ||
} | ||
} |
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,47 @@ | ||
import { assert, assertFalse, AssertionError, assertThrows } from "@std/assert"; | ||
import { assertIsNumeric, isNumeric } from "./is_numeric.ts"; | ||
|
||
const VALID = [ | ||
0, | ||
1, | ||
1.123, | ||
1_123_12_3, | ||
-1, | ||
-1_1234, | ||
-1.123, | ||
]; | ||
|
||
const INVALID = [ | ||
NaN, | ||
"", | ||
"asdf", | ||
undefined, | ||
null, | ||
{}, | ||
[], | ||
[""], | ||
new Number(), | ||
]; | ||
|
||
Deno.test("isNumeric > can detect numerics", () => { | ||
for (const v of VALID) { | ||
assert(isNumeric(v), `Value of '${v}' is not valid`); | ||
} | ||
for (const v of INVALID) { | ||
assertFalse(isNumeric(v), `Value of '${v}' is not invalid`); | ||
} | ||
}); | ||
|
||
Deno.test("assertIsNumeric > can detect numerics", () => { | ||
for (const v of VALID) { | ||
assertIsNumeric(v); | ||
} | ||
for (const v of INVALID) { | ||
assertThrows( | ||
() => assertIsNumeric(v), | ||
AssertionError, | ||
undefined, | ||
`Value of '${v}' did not throw`, | ||
); | ||
} | ||
}); |
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,18 @@ | ||
import { AssertionError } from "@std/assert"; | ||
import { isNumber } from "./is_number.ts"; | ||
|
||
/** | ||
* Checks if a value is a number and not NaN | ||
*/ | ||
export function isNumeric(value: unknown): value is number { | ||
return isNumber(value) && !Number.isNaN(value); | ||
} | ||
|
||
/** | ||
* Asserts that a value is a number and not NaN | ||
*/ | ||
export function assertIsNumeric(value: unknown): asserts value is number { | ||
if (!isNumeric(value)) { | ||
throw new AssertionError(`Value is not a numeric, was '${value}'`); | ||
} | ||
} |
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,42 @@ | ||
import { assert, assertFalse, AssertionError, assertThrows } from "@std/assert"; | ||
import { assertIsRecord, isRecord } from "./is_record.ts"; | ||
|
||
const VALID = [ | ||
{}, | ||
{ a: 1 }, | ||
{ 1: "a" }, | ||
]; | ||
|
||
const INVALID = [ | ||
{ [Symbol.dispose]: "" }, | ||
"", | ||
1, | ||
undefined, | ||
null, | ||
[], | ||
[""], | ||
new Map(), | ||
]; | ||
|
||
Deno.test("isRecord > can detect records", () => { | ||
for (const v of VALID) { | ||
assert(isRecord(v), `Value of '${v}' is not valid`); | ||
} | ||
for (const v of INVALID) { | ||
assertFalse(isRecord(v), `Value of '${v}' is not invalid`); | ||
} | ||
}); | ||
|
||
Deno.test("assertIsRecord > can detect records", () => { | ||
for (const v of VALID) { | ||
assertIsRecord(v); | ||
} | ||
for (const v of INVALID) { | ||
assertThrows( | ||
() => assertIsRecord(v), | ||
AssertionError, | ||
undefined, | ||
`Value of '${v}' did not throw`, | ||
); | ||
} | ||
}); |
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,36 @@ | ||
import { AssertionError } from "@std/assert"; | ||
import { objectToStringEquals } from "./utils.ts"; | ||
|
||
/** | ||
* Checks if a value is a Record<string, unknown> | ||
*/ | ||
export function isRecord(value: unknown): value is Record<string, unknown> { | ||
if (!objectToStringEquals("Object", value)) { | ||
return false; | ||
} | ||
|
||
if (typeof value !== "object") { | ||
return false; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return false; | ||
} | ||
|
||
if (Object.getOwnPropertySymbols(value).length > 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Asserts that a value is a record | ||
*/ | ||
export function assertIsRecord( | ||
value: unknown, | ||
): asserts value is Record<string, unknown> { | ||
if (!isRecord(value)) { | ||
throw new AssertionError(`Value is not a Record, was '${value}'`); | ||
} | ||
} |
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,40 @@ | ||
import { assert, assertFalse, AssertionError, assertThrows } from "@std/assert"; | ||
import { assertIsString, isString } from "./is_string.ts"; | ||
|
||
const VALID = [ | ||
"", | ||
"asdf", | ||
]; | ||
|
||
const INVALID = [ | ||
1, | ||
undefined, | ||
null, | ||
{}, | ||
[], | ||
[""], | ||
new String(), | ||
]; | ||
|
||
Deno.test("isString > can detect strings", () => { | ||
for (const v of VALID) { | ||
assert(isString(v), `Value of '${v}' is not valid`); | ||
} | ||
for (const v of INVALID) { | ||
assertFalse(isString(v), `Value of '${v}' is not invalid`); | ||
} | ||
}); | ||
|
||
Deno.test("assertIsString > can detect strings", () => { | ||
for (const v of VALID) { | ||
assertIsString(v); | ||
} | ||
for (const v of INVALID) { | ||
assertThrows( | ||
() => assertIsString(v), | ||
AssertionError, | ||
undefined, | ||
`Value of '${v}' did not throw`, | ||
); | ||
} | ||
}); |
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,17 @@ | ||
import { AssertionError } from "@std/assert"; | ||
|
||
/** | ||
* Checks if a value is a string | ||
*/ | ||
export function isString(value: unknown): value is string { | ||
return typeof value === "string"; | ||
} | ||
|
||
/** | ||
* Asserts that a value is a string | ||
*/ | ||
export function assertIsString(value: unknown): asserts value is string { | ||
if (!isString(value)) { | ||
throw new AssertionError(`Value is not a string, was '${value}'`); | ||
} | ||
} |
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,4 @@ | ||
export * from "./is_number.ts"; | ||
export * from "./is_numeric.ts"; | ||
export * from "./is_record.ts"; | ||
export * from "./is_string.ts"; |
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,7 @@ | ||
/** | ||
* Compares the Object.prototype.toString of the value against the given name | ||
*/ | ||
export function objectToStringEquals(name: string, value: unknown): boolean { | ||
const objectTag = `[object ${name}]`; | ||
return Object.prototype.toString.call(value) === objectTag; | ||
} |
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