Git Product home page Git Product logo

pth's Introduction

pth - What

pth ("path") is a simple helper for selectors in a React Redux environment. It simplifies the act of creating a function that pics out certain parts of your state depending on the arguments passed in.

Install

npm i pth

How

At its simplest, you can compare these two functions:

// raw js
const getNameById = (state, id) => {
    const user = state.users[id]
    return user && user.name
}

// pth
const getNameById = pth('$0.users.$1.name')

So pth returns a function that takes an arbitrary number of arguments and attempts to access the property you are looking for. If it fails, it returns undefined.

Of course you can also use it to create small utility functions for accessing properties on objects:

const getFirstName = pth('$0.firstName')
const getLastName = pth('$0.lastName')
// etc...

options argument

pth also takes an options object as second parameter, where you can change a few things:

// If name isn't found, return 'Mr Nobody'
const getNameById = pth('$0.users.$1.name', {or: 'Mr Nobody'})

// Change the identifier from $ to %
const getNameById = pth('%0.users.%1.name', {identifier: '%'})

// Change the splitter from . to ->
const getNameById = pth('%0->users->%1->name', {splitter: '->'})

pth.fromObject

There is a second version where the returned function only takes two arguments:

  1. the object where we expect to find the data we are looking for, and
  2. an object with the keys we need

The path string we pass to it also looks a little different (better if you ask me).

These two are equevalent:

// raw js
const getNameById = (state, {id}) => {
    const user = state.users[id]
    return user && user.name
}

// pth
const getNameById = pth.fromObject('users.$id.name')

This version takes the same options object as second parameter as the above.

Because too much magic?

If giving pth a string is too much magic for you, then know that it also takes an array.

const getNameById = pth(['$0', 'users', '$1', 'name'])

const getNameById = pth.fromObject(['users', '$id', 'name'])

Tiny caution

I don't recommend "building" the function every time you call it. That is, this is probably not a great idea:

const getName = user => pth('$0.name')(user)

The reason is performance. It's not that it is particularly slow; but it is splitting up a string and looping through each of the sections every time; so really it's better to just build the function ahead of time.

pth's People

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.