npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bulwarkjs/safe-tools

v1.1.2

Published

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

Downloads

41

Readme

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.