Giter VIP home page Giter VIP logo

safe-tools's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.