Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielmschmidt/babel-generate-guard-clauses
A helper to generate different guard clauses
https://github.com/danielmschmidt/babel-generate-guard-clauses
babel code-generation guard-clauses
Last synced: 5 days ago
JSON representation
A helper to generate different guard clauses
- Host: GitHub
- URL: https://github.com/danielmschmidt/babel-generate-guard-clauses
- Owner: DanielMSchmidt
- Created: 2017-08-06T11:56:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-28T16:53:12.000Z (9 months ago)
- Last Synced: 2024-04-15T06:56:56.048Z (7 months ago)
- Topics: babel, code-generation, guard-clauses
- Language: JavaScript
- Size: 568 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
Awesome Lists containing this project
README
# Babel-Generate-Guard-Clause ![travis status](https://travis-ci.org/DanielMSchmidt/babel-generate-guard-clauses.svg?branch=master) [![Coverage Status](https://coveralls.io/repos/github/DanielMSchmidt/babel-generate-guard-clauses/badge.svg?branch=master)](https://coveralls.io/github/DanielMSchmidt/babel-generate-guard-clauses?branch=master)
A helper to generate different guard clauses.
## Exports
### `generateTypeCheck(typeAssertion, options)({ name: argumentName })`
#### `generateTypeCheck("number")({ name: argumentName })`
```js
const typeCheckAst = genertateTypeCheck("string")({ name: "ponies" });
```Generated Code:
```js
if (typeof ponies !== "string") {
throw new Error(
"ponies should be a string, but got " + ponies + "(" + typeof ponies + ")"
);
}
```#### `generateTypeCheck("number", { selector: "selector" })({ name: argumentName })`
```js
const typeCheckAst = generateTypeCheck("number", { selector: "x" })({
name: "point"
});
```Generated Code:
```js
if (typeof point.x !== "number") {
throw new Error(
"point.x should be a number, but got " +
point.x +
"(" +
typeof point.x +
")"
);
}
```### `generateIsOneOfCheck`
#### `generateIsOneOfCheck(optionArray)({ name: "argName" })`
```js
const typeCheckAst = generateIsOneOfCheck(["option1", "option2"])({
name: "argName"
});
```Generated Code:
```js
if (!["option1", "option2"].some(x => x === argName)) {
return new Error(
"argName should be one of ['option1', 'option2'], but got " + argName
);
}
```