truthy.js

0.6.1 • Public • Published

truthy.js Build Status Generic

Reasons this project exists:

  • Each new JavaScript project I create, I end up defining a handful of basic functions to help simplify/normalize rules (e.g. existence check of both null/undefined).
  • Strong preference for monads and applicative functors over traditional control structures (e.g. if/else). Wondering what the heck a monad or applicative functor is and why you might want to use one? Checkout this 30 minute video.

Matrix:

Function True when Boolean(a): true/false Option(a): Some(a)/None Either(a): Left(a)/Right(a) Validation(a, e): success(a)/failure(e)
arrayey existy and Array.isArray X X X X
booleany existy and type of boolean X X X X
existy not null or undefined X X X X
extensibley objecty and Object.isExtensible X X X X
finitey (numbery and isFinite) or (stringy and lengthy and isFinite) X X X X
frozeny objecty and Object.isFrozen X X X X
functiony existy and type of function X X X X
lengthy existy, length property, and length is > 0 (works on objects too) X X X X
nany isNaN X X X X
numbery existy and type of number X X X X
objecty existy and type of object X X X X
sealedy objecty and Object.isSealed X X X X
stringy existy and type of string X X X X
truthy existy and not boolean false X X X X
eitherize your function evaluates true X
optionize your function evaluates true X
validationize your function evaluates true X

This project leverages option, either, and validation from bilby.js by Brian McKenna.

Depending Upon

The project is available on the Node Packaged Modules registry. Add the dependency to your package.json file:

"dependencies": {
    "truthy.js": "0.6.x"
}

Usage (CoffeeScript)

Boolean:

# true 
truthy.boolean.existy(0)
truthy.bool.existy(0)
truthy.b.existy(0)
 
# false 
truthy.boolean.existy(null)
truthy.bool.existy(null)
truthy.b.existy(null)

Option:

# 9 
truthy.option.existy(0).map((a) -> a + 9).getOrElse(1)
truthy.opt.existy(0).map((a) -> a + 9).getOrElse(1)
truthy.o.existy(0).map((a) -> a + 9).getOrElse(1)
 
# 1 
truthy.option.existy(null).map((a) -> a + 9).getOrElse(1)
truthy.opt.existy(null).map((a) -> a + 9).getOrElse(1)
truthy.o.existy(null).map((a) -> a + 9).getOrElse(1)

Either:

# 10 
truthy.either.existy(0).fold(
    (-> -1),
    ((a) -> v + 10)
)
truthy.eth.existy(0).fold(
    (-> -1),
    ((a) -> v + 10)
)
truthy.e.existy(0).fold(
    (-> -1),
    ((a) -> v + 10)
)
 
# -1 
truthy.either.existy(null).fold(
    (-> -1),
    ((a) -> v + 10)
)
truthy.eth.existy(null).fold(
    (-> -1),
    ((a) -> v + 10)
)
truthy.e.existy(null).fold(
    (-> -1),
    ((a) -> v + 10)
)

Validation:

# Not an example of actual usage, checkout the link above. 
# { value: 0 } 
truthy.validation.existy(0['oh noes'])
truthy.vld.existy(0['oh noes'])
truthy.v.existy(0['oh noes'])
 
# Not an example of actual usage, checkout the link above. 
# { errors: ['oh noes'] } 
truthy.validation.existy(null['oh noes'])
truthy.vld.existy(null['oh noes'])
truthy.v.existy(null['oh noes'])

Make your own:

= truthy.eitherize((a) -> v is 99)
 
# 1 
f(0).fold(
    ((a) -> a + 1),
    ((a) -> a - 1)
)
 
# 98 
f(99).fold(
    ((a) -> a + 1),
    ((a) -> a - 1)
)

Operator overloading (via bilby.js):

# 'hello world' 
bilby.Do()(
    truthy.option.stringy >> truthy.optionize((a) -> v is 'hello world')
)('hello world').getOrElse('goodbye world')
 
# 'goodbye world' 
bilby.Do()(
    truthy.option.stringy >> truthy.optionize((a) -> v is 'hello world')
)('hi').getOrElse('goodbye world')

License

The MIT License (MIT)

Copyright (c) 2013 Rocky Madden (http://rockymadden.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i truthy.js

Weekly Downloads

0

Version

0.6.1

License

none

Last publish

Collaborators

  • rockymadden