safe-prop

1.1.1 • Public • Published

logo

Build Status Coverage Status

A micro-library for safe handling of object properties using the Either ADT from data.either.

API

You can review the API below or take an interactive look in CodeSandbox

# safeProp(prop, obj)

// safeProp : String -> Object -> Either Error Value
import { safeProp } from 'safe-prop';

const obj = { value: 1, empty: null };

safeProp("fake", obj);  // Either.Left(`The property "fake" does not exist on the passed in object.`)
safeProp("empty", obj); // Either.Left(`The value for the property "empty" is either null or undefined on the passed in object.`)
safeProp("value", obj); // Either.Right(1)

Safely check if prop exists on object. Returns Either.Left if it doesn't exists or the value of the property is Nullable and Either.Right with the value of the property if it does.

# safePath([..props], obj)

// safeProp : Array String -> Object -> Either Error Value
import { safePath } from 'safe-prop';

const obj = { one: { two: { value: 1, empty: null } } };

safeProp(["fake"], obj);  // Either.Left(`The property "fake" does not exist on the passed in object.`)
safeProp(["one", "three", "value"], obj);  // Either.Left(`The property "three" does not exist on the passed in object.`)
safeProp(["one", "two", "empty"], obj); // Either.Left(`The value for the property "empty" is either null or undefined on the passed in object.`)
safeProp(["one", "two", "value"], obj); // Either.Right(1)

Safely check if nested prop exists on path for an object. Returns Either.Left if the path doesn't exists or the value of the property is Nullable and Either.Right with the value of the property if it does.

Package Sidebar

Install

npm i safe-prop

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

13.3 kB

Total Files

7

Last publish

Collaborators

  • wking_io