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

formula-store

v1.3.2

Published

A package to handle formula calculations and dependency management

Downloads

24

Readme

formula-store

CircleCI npm package

A versatile and efficient package for managing and calculating formula fields in a store.

Description

🧮 formula-store is a TypeScript library designed to provide a flexible solution for handling formula fields. It enables you to manage dependencies, calculate values, and receive updates seamlessly. This package is well-suited for scenarios where dynamic formula-based computations are crucial and performance and robust api design matters.

Install

yarn add formula-store
npm install formula-store

Usage

1. Create Formula Store

Initialize a FormulaStore to manage and update formula fields.

import { createFormulaStore } from "formula-store";

// Example Usage:
const store = createFormulaStore({
  onChange: updates => {
    // Handle updates when formula fields change.
  }
});

2. Define Formula Fields

Create instances of FormulaField representing the formula fields you want to manage in your store.

import { FormulaField, FormulaStore } from "formula-store";

// Example Usage:
const myFormulaField: FormulaField<number> = {
  id: "uniqueId",
  value: 42,
  dependencies: ["dependencyA", "dependencyB"],
  calculate: (dependencyA, dependencyB) => dependencyA + dependencyB
};

3. Use Formula Store

Utilize the store to manage formula fields and receive updates.

// Example Usage:
// Add fields for dependencies.
store.addField({
  dependencies: [],
  id: "a",
  value: 1
});

store.addField({
  dependencies: [],
  id: "b",
  value: 3
});

store.addField({
  dependencies: ["a", "b"],
  id: "c",
  value: 0,
  calculate: (a, b) => a + b
});

// Update the values of formula fields in the store.
store.updateFieldsValue([
  { id: "a", value: 2 }
  // Add more updates as needed.
]);

// Remove a formula field from the store.
const affectedFields = store.removeField("a");
console.log("Affected Fields: ", affectedFields);

// Edit a store field.
store.editField({
  dependencies: ["a", "b"],
  id: "c",
  value: 0,
  calculate: (a, b) => a + b
});

// Get previously added field.
const fieldA = store.getFieldById("a");
console.log("Field a: ", fieldA);

API Reference

FormulaField<T>

Represents a field that is either a direct formula or a field that will be used inside another formula.

  • id: Unique identifier for the formula field.
  • value: The current value of the formula field.
  • dependencies: An array of other formula field ids representing the dependencies of the formula field.
  • calculate: A function that calculates the new value based on the provided dependencies. (Optional)

FormulaUpdate

Represents an update for a formula field in the store.

  • id: Unique identifier for the formula field to be updated.
  • value: The new value for the formula field.

FormulaStoreInput

Represents the input configuration for the FormulaStore.

  • onChange: A callback function to be called when formula fields are updated.

FormulaStore

Represents a store for managing formula fields.

  • addField: Adds a new formula field to the store.
  • editField: Edits a formula previously added to the store.
  • removeField: Removes a formula field from the store based on its identifier.
  • updateFieldsValue: Updates the values of multiple formula fields in the store.
  • getFieldById: Gets a field from the store by id.

Contributing

Contributions are welcome! Please submit a pull request with any improvements or bug fixes. Make sure to add tests for any new features and bug fixes, and ensure that the existing tests pass.

License

This project is licensed under the MIT License.

Contact

If you need help or have questions, feel free to open an issue in the GitHub repository.