-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c370d93
commit 46b9632
Showing
12 changed files
with
78 additions
and
49 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,5 @@ | ||
--- | ||
"@actnowcoalition/actnow.js": minor | ||
--- | ||
|
||
Create validation package |
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 |
---|---|---|
@@ -1,13 +1,7 @@ | ||
# @actnowcoalition/number-format | ||
## Usage | ||
|
||
> Number formatting utility functions | ||
## Installing | ||
|
||
```sh | ||
yarn add @actnowcoalition/number-format | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) |
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 |
---|---|---|
@@ -1,13 +1,7 @@ | ||
# @actnowcoalition/time-utils | ||
## Usage | ||
|
||
> Utility functions to handle and format time | ||
## Installing | ||
|
||
```sh | ||
yarn add @actnowcoalition/time-utils | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) |
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 |
---|---|---|
@@ -1,13 +1,7 @@ | ||
# @actnowcoalition/ui-components | ||
## Usage | ||
|
||
> UI components for Act Now | ||
## Installing | ||
|
||
```sh | ||
yarn add @actnowcoalition/ui-components | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) |
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,7 @@ | ||
## Usage | ||
|
||
> Validation functions | ||
## License | ||
|
||
[MIT](./LICENSE) |
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,26 @@ | ||
import { throwValidationError, validate } from "./index"; | ||
|
||
describe("validate", () => { | ||
test("throws an error if condition is falsy", () => { | ||
const errorMsg = "Error message"; | ||
expect(() => validate(false)).toThrow(); | ||
expect(() => validate(0)).toThrow(); | ||
expect(() => validate(undefined)).toThrow(); | ||
expect(() => validate(null)).toThrow(); | ||
expect(() => validate("")).toThrow(); | ||
expect(() => validate(false, errorMsg)).toThrowError(errorMsg); | ||
}); | ||
|
||
test("pass if condition is truthy", () => { | ||
expect(() => validate(true)).not.toThrow(); | ||
expect(() => validate(1)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe("throwActNowJsError", () => { | ||
const errorMsg = "Error message"; | ||
test("throws an error with the given message", () => { | ||
expect(() => throwValidationError()).toThrow(); | ||
expect(() => throwValidationError(errorMsg)).toThrowError(errorMsg); | ||
}); | ||
}); |
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,30 @@ | ||
/** | ||
* Test if the `condition` is truthy. If `condition` is false, it throws | ||
* an error with an optional message. If the message parameter is missing, | ||
* it throws an error with a default message. | ||
*/ | ||
export function validate( | ||
condition: unknown, | ||
errorMessage?: string | ||
): asserts condition { | ||
if (!condition) { | ||
throw new ActNowJsError(errorMessage); | ||
} | ||
} | ||
|
||
/** | ||
* Throws an Error with the (optionally) provided error message. | ||
*/ | ||
export function throwValidationError(errorMessage?: string): never { | ||
throw new ActNowJsError(errorMessage); | ||
} | ||
|
||
/** | ||
* An ActNowJsError class to differentiate from default and internal errors. | ||
*/ | ||
export class ActNowJsError extends Error { | ||
constructor(public readonly errorMessage?: string) { | ||
super(errorMessage); | ||
this.name = "ActNowJsError"; | ||
} | ||
} |