@unchainedshop/utils

December 10, 2025 ยท View on GitHub

npm version License: EUPL-1.2

@unchainedshop/utils

Common utility functions and base classes for the Unchained Engine.

Installation

npm install @unchainedshop/utils

Usage

import { slugify, generateRandomHash, resolveBestSupported } from '@unchainedshop/utils';

// Generate URL-safe slugs
const slug = slugify('Hello World!'); // 'hello-world'

// Generate random hashes
const hash = generateRandomHash();

// Resolve best supported locale
const locale = resolveBestSupported('en-US,de-CH', 'CH', {
  countries: [{ isoCode: 'CH' }],
  languages: [{ isoCode: 'de' }, { isoCode: 'en' }],
});

API Overview

String Utilities

ExportDescription
slugifyConvert string to URL-safe slug
findUnusedSlugFind an unused slug by appending numbers
generateRandomHashGenerate a random hash string
sha256Compute SHA-256 hash of a string
sha1Compute SHA-1 hash of a string

Locale Helpers

ExportDescription
systemLocaleDefault system locale from environment
resolveBestSupportedResolve best supported locale from Accept-Language
resolveBestCurrencyResolve best currency from available options
determineFallbackLocaleDetermine fallback locale from countries/languages

Calculation Utilities

Available under the calculation namespace:

ExportDescription
roundToNextRound value up to next precision step
calculateAmountToSplitCalculate amount to split based on rate/fixed
applyRateApply rate or fixed amount to a value
getTaxAmountCalculate tax amount from total and rate
resolveAmountAndTaxResolve share amount and tax from ratio
applyDiscountToMultipleSharesApply discount across multiple shares

Object Utilities

ExportDescription
objectInvertInvert object keys and values
buildObfuscatedFieldsFilterBuild filter for obfuscating sensitive fields

Director/Adapter Base Classes

ExportDescription
BaseAdapterBase class for all adapters with logging support
BaseDirectorFactory for creating adapter directors
IBaseAdapterInterface for adapter implementations
IBaseDirectorInterface for director implementations

Swiss-specific Helpers

Available under the ch namespace:

ExportDescription
addressToStringFormat Swiss address as string
priceToStringFormat price for Swiss locale

Types

ExportDescription
SortDirectionEnum-like object for ASC/DESC sorting
SortOptionInterface for sort options
PriceInterface for price with amount and currency
PricingCalculationInterface for pricing calculations
DateFilterInputInterface for date range filtering
Tree<T>Type for tree data structures
NodeOrTree<T>Type for tree nodes

Environment Variables

VariableDefaultDescription
UNCHAINED_LANGdeDefault language code
UNCHAINED_COUNTRYCHDefault country code
UNCHAINED_CURRENCYCHFDefault currency code

License

EUPL-1.2