Skip to content

Safe Tools is a list of functions to help you using folkate. It can be usefull or not

License

Notifications You must be signed in to change notification settings

bulwarkjs/safe-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Safe Tools

Safe Tools is a list of functions to help you with nullcheck based on folktale.

Table of Contents

Installation

safe-tools is available from npm.

$ npm install @bulwarkjs/safe-tools -S

Importing

import safeTools from '@bulwarkjs/safeTools'
// or
import safeProp from '@bulwarkjs/safeTools/safeProp'
// or
import { safePath } from '@bulwarkjs/safeTools'

Why safe-tools ?

TODO

Overview

TODO

API

fromAllNullables

Similar to fromNullable from folktale but receives an array of nullables

const a = 1
const b = 2
const c = 3

const value = safeTools.fromAllNullables([ a, b, c ])
  .map(([ a, b, c ]) => [ a+1, b+2, c+3 ])
  .getOrElse([ 0, 0, 0 ])

console.log(value) //  [2, 4, 6];

Or

const a = 1
const b = null
const c = 3

const value = safeTools.fromAllNullables([ a, b, c ])
  .map(([ a, b, c ]) => [ a+1, b+2, c+3 ])
  .getOrElse([ 0, 0, 0 ])

console.log(value) //  [0, 0, 0];

removeNullables

Remove all nulls values recursively

const nullables = [ 1, null, [ 3, null ], { foo: 4, bar: null } ]

const value = safeTools.removeNullables(nullables)

console.log(value) // [1, [ 3 ], { foo: 4 } ]

safePath

Similar to R.path from ramda but it returns an Maybe monad from folktale

const object = {
  foo: {
    bar: {
      baz: 1
    }
  }
}

const value = safeTools.safePath([ 'foo', 'bar', 'baz' ], object)
  .map(val => val+1)
  .getOrElse(0)

console.log(value) // 2

Or

const object = {
  foo: {
    bar: null
  }
}

const value = safeTools.safePath([ 'foo', 'bar', 'baz' ], object)
  .map(val => val+1)
  .getOrElse(0)

console.log(value) // 0

safeProp

Similar to safePath but with only one level of nesting

const object = {
  foo: 'bar'
}

const value = safeTools.safeProp('foo', object)
  .map(val => val.toUpperCase())
  .getOrElse('NOOP')

console.log(value) // BAR

Or

const object = null

const value = safeTools.safeProp('foo', object)
  .map(val => val.toUpperCase())
  .getOrElse('NOOP')

console.log(value) // NOOP

safeNumber

Similar to safePath but with numbers

const number = 1

const value = safeTools.safeNumber(number)
  .map(n => n * 10)
  .getOrElse(0)

console.log(value) // 10

Or

const number = 'I am a number, trust me'

const value = safeTools.safeNumber(number)
  .map(n => n * 10)
  .getOrElse(0)

console.log(value) // 0

tryCatch

Wrap up a function into a try/catch block and returns a Result monad from folktale

const parseJson = () => {
  const jsonStr = '{"foo":"bar"}'
  const value = JSON.parse(jsonStr)
  return value
}

const value = safeTools.tryCatch(parseJson)
  .getOrElse({})

console.log(value) // { foo: 'bar' }

Or

const parseJson = () => {
  const jsonStr = 'Invalid json'
  const value = JSON.parse(jsonStr)
  return value
}

const value = safeTools.tryCatch(parseJson)
  .getOrElse({})

console.log(value) // {}

log

Useful function to debbug

const str = 'Bulwark'
const value = safeTools.fromNullable(str)
  .map(str => str.toUpperCase())
  .map(log('Upppercase Str')) // log the value in console
  .map(str => `${str} - JS`)
  .map(log('Concat')) // log the value in console
  .getOrElse('')

isNilOrEmpty

Null or empty checker

safeTools.isNilOrEmpty('') // true

safeTools.isNilOrEmpty([]) // true

safeTools.isNilOrEmpty({}) // true

safeTools.isNilOrEmpty(null) // true

safeTools.isNilOrEmpty(undefined) // true

safeTools.isNilOrEmpty(0) // false

safeTools.isNilOrEmpty(true) // false

License

The code is available under the MIT License.

About

Safe Tools is a list of functions to help you using folkate. It can be usefull or not

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published