Skip to content

DanielMSchmidt/babel-generate-guard-clauses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Babel-Generate-Guard-Clause travis status Coverage Status

A helper to generate different guard clauses.

Exports

generateTypeCheck(typeAssertion, options)({ name: argumentName })

generateTypeCheck("number")({ name: argumentName })

const typeCheckAst = genertateTypeCheck("string")({ name: "ponies" });

Generated Code:

if (typeof ponies !== "string") {
  throw new Error(
    "ponies should be a string, but got " + ponies + "(" + typeof ponies + ")"
  );
}

generateTypeCheck("number", { selector: "selector" })({ name: argumentName })

const typeCheckAst = generateTypeCheck("number", { selector: "x" })({
  name: "point"
});

Generated Code:

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" })

const typeCheckAst = generateIsOneOfCheck(["option1", "option2"])({
  name: "argName"
});

Generated Code:

if (!["option1", "option2"].some(x => x === argName)) {
  return new Error(
    "argName should be one of ['option1', 'option2'], but got " + argName
  );
}

About

A helper to generate different guard clauses

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •