Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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
);
}
```