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

immutable-functions

v0.9.2

Published

A simple, easy, straight forward approach to update immutable data.

Downloads

1,491

Readme

Build Status

Immutable Functions

A simple, easy, straight forward approach to update immutable data.

Installation

$ yarn add immutable-functions

or

$ npm install -s immutable-functions

Usage

import _i from 'immutable-functions';
import { CREATE, UPDATE, REMOVE } from '../actions/collection';

export default (state = [], action) => {
  switch (action.type) {
    case CREATE:
      return _i.create(state, action.obj);
    case UPDATE:
      return _i.update(state, action.obj.id, action.obj);
    case REMOVE:
      return _i.destroy(state, action.obj.id);
    default:
      return state;
  }
};
import React, { PureComponent } from 'react';
import { View, Text } from 'react-native';
import { connect } from 'react-redux';
import _i from 'immutable-functions';

class YourComponent extends PureComponent {
  render() {
    // Get the item with id === 5
    const item = _i.find(this.props.data, 5)

    // item
    // {
    //   id: 5,
    //   name: Jerry,
    //   email: [email protected]
    // }

    return (
      <View>
        <Text>Name: {item.name}</Text>
        <Text>Email: {item.email}</Text>
      </View>
    );
  }
}

// Here are the properties of the store this component uses
const mapStoreToProps = store => ({
  data: store.yourReducer
});

// This is how we connect the store to our component
export default connect(mapStoreToProps)(YourComponent);

Functions

_i.create(state, data)

Creates an object if it does not already exist (by id) in the array.

  • arguments
    • state (array) The current array of objects.
    • data (object) The object you are adding to the array.
  • returns
    • (array) A new array with the new object added to the end
_i.update(state, id, data)

Updates an object if it exists (by id) in the array, if it does not exist, it will create it.

  • arguments
    • state (array) The current array of objects.
    • id (integer) The id of the object you are updating.
    • data (object) The delta of the object you are updating.
  • returns
    • (array) A new array with the object updated
_i.destroy(state, id)

Destroys an object if it exists (by id) in the array.

  • arguments
    • state (array) The current array of objects.
    • id (integer) The id of the object you are destroying.
  • returns
    • (array) A new array with the object removed
_i.merge(state, array[, deep])

Merges an object or array of objects (unique by id) with the current state.

  • arguments
    • state (array/object) The current object or array of objects.
    • array (array/object) new object or array of objects being merged.
    • deep (boolean) will do a deep merge of an object
  • returns
    • (array/object) A new object or array with all the objects unique by id
_i.find(state, id)

Find an object if it exists (by id) in the array. Useful in components when accessing the data.

  • arguments
    • state (array) The current array of objects.
    • id (integer) The id of the object you are finding.
  • returns
    • (array) A single (first) object with the given id
_i.findByProp(state, prop, id)

Find an object if it exists by a property provided in the array. Useful in components when accessing the data.

  • arguments
    • state (array) The current array of objects.
    • prop (string) The identifier property on the objects in the array.
    • id (integer) The id of the object you are finding.
  • returns
    • (array) A single (first) object with the given identifier property
  • example
    • _i.findByProp(state, 'LoanId', '1234456');
_i.updateByProp(state, prop, id, data)

Updates an object if it exists by an identifier property provided in the array, if it does not exist, it will create it.

  • arguments
    • state (array) The current array of objects.
    • prop (string) The identifier property on the objects in the array.
    • id (integer) The id of the object you are updating.
    • data (object) The delta of the object you are updating.
  • returns
    • (array) A new array with the object updated
  • example
    • _i.updateByProp(state, 'LoanId', action.obj.LoanId, action.obj);
_i.destroyByProp(state, prop, id)

Destroy an object if it exists by an identifier property provided in the array.

  • arguments
    • state (array) The current array of objects.
    • prop (string) The identifier property on the objects in the array.
    • id (integer) The id of the object you are updating.
  • returns
    • (array) A new array with the object removed
  • example
    • _i.destroyByProp(state, 'LoanId', action.obj.LoanId);
_i.mergeByProp(state, array, prop[, deep])

Merges an object or array of objects (unique by prop) with the current state.

  • arguments
    • state (array/object) The current object or array of objects.
    • array (array/object) new object or array of objects being merged.
    • prop (string) The identifier property on the objects in the array.
    • deep (boolean) will do a deep merge of an object
  • returns
    • (array/object) A new object or array with all the objects unique by prop