Skip to content

Commit ba46320

Browse files
committed
refactor: do not try to reuse previously added timer with same values and simply push new timers as they are defined
1 parent 18dc46f commit ba46320

File tree

3 files changed

+1
-58
lines changed

3 files changed

+1
-58
lines changed

src/classes/recipe.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
ingredientAliasRegex,
1919
} from "../regex";
2020
import {
21-
findOrPush,
2221
flushPendingItems,
2322
flushPendingNote,
2423
findAndUpsertIngredient,
@@ -265,15 +264,7 @@ export class Recipe {
265264
duration,
266265
unit,
267266
};
268-
const idxInList = findOrPush(
269-
this.timers,
270-
(t) =>
271-
t.name === timerObj.name &&
272-
t.duration === timerObj.duration &&
273-
t.unit === timerObj.unit,
274-
() => timerObj,
275-
);
276-
items.push({ type: "timer", value: idxInList });
267+
items.push({ type: "timer", value: this.timers.push(timerObj) - 1 });
277268
}
278269

279270
cursor = idx + match[0].length;

src/parser_helpers.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,6 @@ import {
1717
Quantity,
1818
} from "./units";
1919

20-
/**
21-
* Finds an item in a list or adds it if not present, then returns its index.
22-
* @param list - The list to search in.
23-
* @param finder - A predicate to find the item.
24-
* @param creator - A function to create the item if not found.
25-
* @returns The index of the item in the list.
26-
*/
27-
export function findOrPush<T>(
28-
list: T[],
29-
finder: (elem: T) => boolean,
30-
creator: () => T,
31-
): number {
32-
let index = list.findIndex(finder);
33-
if (index === -1) {
34-
index = list.push(creator()) - 1;
35-
}
36-
return index;
37-
}
38-
3920
/**
4021
* Pushes a pending note to the section content if it's not empty.
4122
* @param section - The current section object.

test/parser_helpers.test.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, it, expect, vi } from "vitest";
22
import { Section as SectionObject } from "../src/classes/section";
33
import type { Step, MetadataExtract, Cookware, Ingredient } from "../src/types";
44
import {
5-
findOrPush,
65
flushPendingNote,
76
flushPendingItems,
87
parseSimpleMetaVar,
@@ -234,34 +233,6 @@ images: [https://static01.nyt.com/images/2021/12/28/dining/yf-baked-feta/yf-bake
234233
});
235234
});
236235

237-
describe("findOrPush", () => {
238-
it("should add an item if it does not exist and return its index", () => {
239-
const list = [{ id: 1, name: "one" }];
240-
const creator = vi.fn(() => ({ id: 2, name: "two" }));
241-
242-
const index = findOrPush(list, (item) => item.name === "two", creator);
243-
244-
expect(index).toBe(1);
245-
expect(list).toHaveLength(2);
246-
expect(list[1]).toEqual({ id: 2, name: "two" });
247-
expect(creator).toHaveBeenCalledTimes(1);
248-
});
249-
250-
it("should find an existing item and return its index without adding", () => {
251-
const list = [
252-
{ id: 1, name: "one" },
253-
{ id: 2, name: "two" },
254-
];
255-
const creator = vi.fn(() => ({ id: 3, name: "three" }));
256-
257-
const index = findOrPush(list, (item) => item.name === "two", creator);
258-
259-
expect(index).toBe(1);
260-
expect(list).toHaveLength(2);
261-
expect(creator).not.toHaveBeenCalled();
262-
});
263-
});
264-
265236
describe("flushPendingNote", () => {
266237
it("should add a note to the section if the note is not empty", () => {
267238
const section = new SectionObject("Test Section");

0 commit comments

Comments
 (0)