Skip to content

Utilities

Helper functions in a utils/ folder hold reusable logic that does not depend on Vue reactivity or component state. Most utils are pure transformations: they compute a result from their arguments only. A util that changes the DOM, storage or other global state is a side-effecting util and must say so in its name.

Choose the utils/ folder

Three folders carry this name; pick the narrowest one that serves every caller (see Layer responsibilities):

  • <module>/utils/: helpers of one feature module.
  • shared/utils/: helpers used by several frontend modules.
  • utils/ (top level): helpers an entrypoint publishes on window.EASY.utils for ERB views and legacy scripts.

Example: pure transformation

filterBookmarks returns the bookmarks whose title matches a search query. It reads only its arguments and returns a new array. bookmarks is a made-up module: the example shows the pattern, not existing code.

import type { Bookmark } from "@/src/bookmarks/types";

export const filterBookmarks = (bookmarks: readonly Bookmark[], query: string): Bookmark[] => {
  const normalizedQuery = query.trim().toLowerCase();
  if (!normalizedQuery) return [...bookmarks];

  return bookmarks.filter(bookmark => bookmark.title.toLowerCase().includes(normalizedQuery));
};

Usage example

The composable reads the reactive state and passes plain values in:

const visibleBookmarks = computed(() => filterBookmarks(bookmarks.value, query.value));

The same input always gives the same output, so the util can be tested with plain objects, without DOM or Vue setup.

Side-effecting utils

Keep DOM changes out of pure helpers. When a util must change the page, name the effect with a verb such as set, apply or toggle, and keep it small so callers can mock it:

export const setPageScrollLocked = (locked: boolean) => {
  document.body.classList.toggle(SCROLL_LOCKED_CLASS, locked);
};

Best practices

  • Place generic, reusable, non-Vue logic in the utils/ folder. (Vue-specific logic should be in composables/, see Composables.)
  • Prefer pure transformations: do not mutate arguments or read global state; return a new value instead.
  • Separate computation from side effects: compute the value in a pure util and apply it in a small, explicitly named side-effecting util or in the caller.
  • Organize files by purpose (e.g., utils/layout.ts, utils/string.ts, utils/dom.ts).
  • A short helper used by one function can stay local to its file; move it to utils/ when it is reused or worth testing on its own.
  • Prefer named exports for each utility function.
  • Keep functions small, focused, and easy to test.
  • Use descriptive names that reflect the function’s purpose (e.g., setPageScrollLocked instead of toggleScroll).
  • Type exported utils as public boundaries (see Type public boundaries).