Sign In

truthy.js

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

truthy.js

Functions to simplify/normalize rules with the ability to leverage monads and applicative functors over traditional control structures.

latest
npmnpm
Version
0.6.1
Version published
Maintainers
1
Created
Source

#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:

FunctionTrue whenBoolean(a): true/falseOption(a): Some(a)/NoneEither(a): Left(a)/Right(a)Validation(a, e): success(a)/failure(e)
arrayeyexisty and Array.isArrayXXXX
booleanyexisty and type of booleanXXXX
existynot null or undefinedXXXX
extensibleyobjecty and Object.isExtensibleXXXX
finitey(numbery and isFinite) or (stringy and lengthy and isFinite)XXXX
frozenyobjecty and Object.isFrozenXXXX
functionyexisty and type of functionXXXX
lengthyexisty, length property, and length is > 0 (works on objects too)XXXX
nanyisNaNXXXX
numberyexisty and type of numberXXXX
objectyexisty and type of objectXXXX
sealedyobjecty and Object.isSealedXXXX
stringyexisty and type of stringXXXX
truthyexisty and not boolean falseXXXX
eitherizeyour function evaluates trueX
optionizeyour function evaluates trueX
validationizeyour function evaluates trueX

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:

f = 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.

Keywords

truth

FAQs

Package last updated on 12 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts