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

field-change-effects-calculator

v2.0.0

Published

**field-change-effects-calculator** is a utility that will calculate all field changes for changes to a certain field.

Downloads

13

Readme

field-change-effects-calculator

field-change-effects-calculator is a utility that will calculate all field changes for changes to a certain field.

It does multi level changes at one shot. For instance if there is a rule saying if A changes B has to change and if B changes C has to change, all the calculations are done and returned by the utility function.

It also has infinite loop mitigation logic built into it so even if there were some misleading rules set, the algorithm will avoid such loops.

Example

An REPL has been created and hosted to show the usage of this utility, check it out here.

Usage

The npm module exposes a utility function that should be used to calculate the change effects.

import fieldChangesCalculator from 'field-change-effects-calculator'

or

const fieldChangesCalculator = require('field-change-effects-calculator')

Signature:

(rules: { fieldName: Array<FieldChangeEffectRuleType> }) => (state: Object) => (nameOfTheField: string, fieldPathInState: Array<string>, newValue: any): Array<FieldChangeObjectType>

The function when provided with rules, state object, field's name, path and new value, returns the changed state of the fields that have changed as an Array of objects of type FieldChangeObjectType.

type  FieldChangeObjectType = {
    name: string,
    path: Array<string>,
    props: {
    prop: any
  }
}

More about rules in the future sections.

Why?

This is very useful when we are maintaining large forms where ther are many inter field dependencies.

All your actions do is call the calculator function, pass rules and information it needs, get back the changed fields data and do what ever you want (in case of redux, dispatch it).

Also since the module does not relate itself to a certain state management tool like redux, etc, you can use it with any state management choice of yours.

Rules

Rules are the most important part of the calculator. They decide the change effects.

Rules object will have keys as field names and values are array field change configurations.

For instance if we have a rule saying B's editability has to change if A's value is changed to World from Hello. Here is how the rules object will look like:

var RULES = {
  A: [
    {
      name: 'B',
      path: ['fields', 'B'],
      props: (ANewValue, state) => {
        editable: ANewValue === 'World' ? 'true' : 'false',
      }
    }
  ]
}

If you want to write more complex rules like changing A changes B and C and changing B changes D, you can write it like this:

var RULES = {
  A: [
    {
      name: 'B',
      path: ['fields', 'B'],
      props: (ANewValue, state) => ({
        editable: ANewValue === 'World' ? 'true' : 'false',
      })
    },
    {
      name: 'C',
      path: ['fields', 'C'],
      props: (ANewValue, state) => ({
        value: ANewValue.toLowerCase()
      })
    }
  ],
  B: [
    {
      name: 'D',
      path: ['fields', 'D'],
      props: () => ({
        value: BNewValue.toLowerCase() + '*',
        editable: true,
      })
    }
  ]
}

Each rule has the following properties:

| Property Name | isRequired | Type | Description | | ------------- | ---------- | ------------------------- | ------------------------------------------------------------------------- | | name | true | string | Name of the field that will be used to uniquely identify the field. | | path | true | Array<string> | Path of the field where it resides in the state. | | props | true | (newValue, state) => {} | Function that will be used to calculate props/changed props of the field. |

Wanna Contribute?

Any help is appreciated.

Clone the project and create your own branch to work on. Once done create a Pull Request.

Make sure to run tests and add more tests if you add a new feature or to replicate the bug that you are trying to fix.

npm run test