0.0.103 • Published 1 year ago

goatscript v0.0.103

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Goatscript 🐐

This is a functional library which is GOATED, and has Goats. Basically you just wrap functions with functions for type safety. Kind of like Sanctuary.js. Except this is much easier to use in my opinion. It's also small and probably not done properly! So don't use it probably! But i plan to use in my projects and keep developing it.

Introduction

This library is used to wrap functions with typechecking, but without needing a compiler like Typescript. It also loads the types of AJV so that it is easy to use basic types by their name directly, or specify new types in json schema, making it easy to integrate with things like APIs.

Install

Recommend to load like this. The alias is to make the functions easier to use frequently, as you might want to do that. You may not need validateWithSchema and validateWithFunction, as there is enough functionality in the signature function and the validators object.

import { signature as s, validators as v, validateWithSchema, validateWithFunction } from 'goatscript'

Fast Usage

import { signature as s, validators as v } from 'goatscript'

// We have some function that we want to make typesafe
const nameToEmail = (name) => name + '@example.com'

// To do this we send the signature function 3 arguments in succession(it is 'curried') .
const wrapped = s
  (v.string)    // input type
  (v.email)     // output type
  (nameToEmail) // function

// type checking is done at both ends, and the results are wrapped in an object.

// Correct Use of the function
res = wrapped('goat').value
console.log(res.value)
// goat@example.com

// Try enter a wrong type, like a number, when a string was defined as the input type
res = wrapped(123)
console.log(res.value)
// undefined
console.log(res.error.message)
// property type must be string
0.0.103

1 year ago

0.0.102

1 year ago

0.0.101

1 year ago

0.0.1

1 year ago