Skip to content

Commit

Permalink
Merge pull request #6 from diogofcunha/get-field
Browse files Browse the repository at this point in the history
feat: 🎸 added getFieldById method
  • Loading branch information
diogofcunha authored Feb 27, 2024
2 parents b7bdde3 + 5a5baf4 commit 9837b80
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ store.updateFieldsValue([
// 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
Expand Down Expand Up @@ -121,8 +133,10 @@ Represents the input configuration for the 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

Expand Down
68 changes: 68 additions & 0 deletions src/__tests__/getFieldById.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { createFormulaStore } from "..";

describe("getFieldById", () => {
test("should fail if field doesn`t exist", () => {
const store = createFormulaStore({ onChange: jest.fn() });

expect(() => store.getFieldById("abc")).toThrowErrorMatchingInlineSnapshot(
`"Field "abc" not found."`
);
});

test("should get added and edited fields", () => {
const store = createFormulaStore({ onChange: jest.fn() });

// 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
});

expect(store.getFieldById("a")).toEqual({
id: "a",
incomingNeighbors: [],
value: 1
});

expect(store.getFieldById("b")).toEqual({
id: "b",
incomingNeighbors: [],
value: 3
});

expect(store.getFieldById("c")).toEqual({
id: "c",
calculate: expect.any(Function),
incomingNeighbors: ["a", "b"],
value: 4
});

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

expect(store.getFieldById("c")).toEqual({
id: "c",
calculate: expect.any(Function),
incomingNeighbors: ["b", "a"],
value: 8
});
});
});
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import {
FormulaFieldNotFoundError
} from "./errors";
import {
FormulaField,
AddedField,
FormulaStore,
FormulaStoreInput,
FormulaUpdate
} from "./types";
import { Graph, Node, SearchAlgorithmNodeBehavior } from "fast-graph";

type AddedField = Node<unknown> & Pick<FormulaField<unknown>, "calculate">;

export function createFormulaStore({
onChange
}: FormulaStoreInput): FormulaStore {
Expand Down Expand Up @@ -170,6 +168,15 @@ export function createFormulaStore({

return {
removeField,
getFieldById: fieldId => {
const existingField = addedFields.get(fieldId);

if (!existingField) {
throw new FormulaFieldNotFoundError(fieldId);
}

return existingField;
},
editField: ({ id, value, dependencies, calculate }) => {
checkFields(id, dependencies);

Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { Node } from "fast-graph";

/**
* Represents a field that is either a direct formula
* or a field that will be used inside another formula.
Expand Down Expand Up @@ -77,4 +79,12 @@ export interface FormulaStore {
updateFieldsValue: (
fields: Array<Pick<FormulaField<any>, "value" | "id">>
) => void;
/**
* Gets a formula field from the store by id.
* @template T - The type of the field value.
*/
getFieldById: (fieldId: string) => AddedField;
}

export type AddedField = Node<unknown> &
Pick<FormulaField<unknown>, "calculate">;

0 comments on commit 9837b80

Please sign in to comment.