ChemPal Documentation - v1.7.0
    Preparing search index...

    Module helpers/utils

    Formatters - Helpers

    formatTimestamp

    Format a Unix epoch timestamp (in milliseconds) as a short, locale-aware "month day, hour:minute" string for UI surfaces like the history and excluded-products lists in the settings drawer. Shared so both panels render the same shape without drifting over time.

    formatBytes

    Format a byte count as a short, human-readable size string using binary (1024-based) units. Used by the settings panel to show how much storage the caches and price history occupy.

    Functions - Helpers

    ucfirst

    Converts the first character of a string to uppercase.

    md5sum

    MD5 hash function that handles various input types. Converts input to string representation before hashing.

    objectToQueryString

    Builds a URL-style query string from a plain object. Keys are emitted in insertion order. Array values are comma-joined.

    base64EncodeUtf8

    Base64-encodes a UTF-8 string (raw bytes, not URI-encoded). ASCII-only input uses btoa directly.

    base36Timestamp

    Opaque base-36 timestamp string (milliseconds + random suffix). Used by some suppliers for cache-busting request IDs.

    generatePageSizes

    Generates an array of page sizes based on the total number of rows. The array starts with the base size and doubles each time until it reaches the total.

    serialize

    Serializes a string to a base64 encoded string. Useful for safely storing strings that may contain special characters. First URI encodes the string, then base64 encodes it.

    deserialize

    Deserializes a base64 encoded string back to its original form. Reverses the serialize() operation by first base64 decoding, then URI decoding the result.

    sleep

    Creates a promise that resolves after the specified delay. Useful for adding delays in async operations or rate limiting.

    delayAction

    Delays the execution of an action by the specified number of milliseconds. Combines sleep() with a callback function for cleaner async code.

    firstMap

    Takes a function and an array of values, applies the function to each value in sequence, and returns the first non-undefined/null result. Useful for trying multiple possible inputs until finding one that produces a valid result.

    mapDefined

    Maps an array of items using a function and filters out any null or undefined results.

    decodeHTMLEntities

    Decodes HTML entities in a string.

    htmlToAscii

    Converts HTML to ASCII. Closing block-level tags (paragraphs, headings, list items, table rows/cells, …) and <br> become line breaks; every other tag is stripped and the common HTML entities are decoded. Runs of blank lines left by empty or nested blocks are collapsed to a single line break.

    findPdfHref

    Finds the href of the first PDF-linking anchor in a block of HTML. Useful for pulling out linked documents such as Safety Data Sheets or spec sheets that suppliers attach as <a href="….pdf"> inside descriptions or info sections.

    tryParseJson

    Tries to parse a JSON string. If it fails, it returns the original string.

    parseJsonFromDirtyString

    Extracts and parses the first top-level JSON array or object from a string that may contain trailing non-JSON content (e.g. delimiters like "&&&").

    getUserLocation

    Gets the user's location (two-letter country code) from the browser's i18n API. This just splits the locale by the "-" character and returns the second part. This is a simple way to get the country code from the locale.

    getUserLanguage

    Gets the user's preferred UI language locale from the browser's i18n API. Used to seed the language user setting. Falls back to "en-US" outside an extension context (e.g. tests).

    getLanguageName

    Resolves a locale code to a human-readable language name using the countries-list language data. The native name is preferred (e.g. "Deutsch"), falling back to the English name and finally the raw code.

    stripHTML

    Strips HTML tags from a string.

    formatFromHtmlTurndown

    Converts HTML to Markdown using TurndownService.

    formatFromHtml

    Formats HTML into a readable plain text string, handling paragraphs, lists, and links.

    getPath

    Resolves a value out of a nested object/array using a key path.

    addActualValueToIssues

    Enriches a list of valibot issues (or any { path }-shaped records) with the actual value found at each issue's path in the source object. Useful when logging validation failures — stock issues only say "expected X at path Y", and knowing what was there makes debugging drastically faster.

    preloadImages

    Preloads a list of images and returns a promise that resolves to an array of results.

    Modules

    <internal>

    Parsers - Helpers

    toFiniteNumber

    Parse a raw string (typically an input's value) into a finite number, returning undefined for empty or malformed input. Uses Number() rather than parseFloat, which would silently accept trailing garbage and turn "12abc" into 12. The empty check is explicit because Number("") and Number(" ") are both 0, not NaN.