@mmb-digital/shared-libs-auto

v0.0.22

Published

MMB shared libs

Readme

@mmb-digital/shared-libs-auto

MMB shared libraries for React and TypeScript projects. This package provides a collection of reusable utilities, formatters, hooks, and validators to streamline development across multiple projects.

Installation

yarn add @mmb-digital/shared-libs-auto

or

npm install @mmb-digital/shared-libs-auto

Usage

Import what you need from the package:

import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-auto'

TypeScript

  • Fully typed with TypeScript.
  • Type definitions are included in the package.

Features

Formaters

Bank Account

  • formatBankAccount(value: BankAccountInput, fallback?: string): string - Formats bank account number to standard format (prefix-account/bankCode).
  • inputFormatBankAccount(value: BankAccountInput): string | null - Formats bank account number to standard format (prefix-account/bankCode) for input fields.

Bytes

  • formatBytes(bytes: number | null | undefined, decimals?: number, fallback?: string): string - Converts number of bytes to human-readable format (KB, MB, GB, etc.).

Phone Number

  • formatCZSKPhoneNumber(value: string | undefined | null, fallback?: string): string - Formats phone number for CZ/SK to standard format (+420 xxx xxx xxx).
  • inputFormatCZSKPhoneNumber(value: string | undefined | null): string | null - Formats phone number input in real-time while typing.

ZIP Code

  • formatCzZipCode(value: number | string | null | undefined, fallback?: string): string - Formats Czech ZIP code to format xxx xx.
  • inputFormatCzZipCode(value: number | string | null | undefined): string | null - Formats ZIP code input in real-time while typing.

Date & Time

  • formatDate(input: string | null | undefined, fallback?: string): string - Formats a valid date string to dd. MM. yyyy, or returns fallback for empty/invalid input.
  • formatDateTime(input: string | null | undefined, fallback?: string): string - Formats a valid date string to dd. MM. yyyy HH:mm:ss, or returns fallback for empty/invalid input.

ICO

  • formatICO(value: string | number | null | undefined, fallback?: string): string - Formats ICO (company identification number) to standard format.
  • inputFormatICO(value: string | number | null | undefined): string | null - Formats ICO input in real-time while typing.

Numbers

  • formatNumber(value: number | string | null | undefined, decimals?: number, fallback?: string): string - Formats number with options for separators, decimal places, and currency.
  • formatPercent(value: number | string | null | undefined, isDecimalValue?: boolean, decimals?: number, fallback?: string): string - Formats number as percentage.
  • inputFormatNumber(value: number | string | null | undefined): string | null - Formats number input in real-time while typing.
  • inputFormatFloat(value: number | string | null | undefined): string | null - Formats decimal number input in real-time while typing.

PIN

  • formatPin(value: string | number | null | undefined, fallback?: string): string - Formats PIN/birth number to standard format.
  • inputFormatPin(value: string | number | null | undefined): string | null - Formats PIN input in real-time while typing.

Roman Numerals

  • formatRomanNumeral(num: number | null | undefined, fallback?: string): string - Converts Arabic number to Roman numerals.

Hooks

useDebounce

  • useDebounce<T>(value: T, delay?: number): T - Returns a debounced value that only updates after the specified delay has passed since the last change. Useful for optimizing search inputs and API calls.

usePreviousValue

  • usePreviousValue<T>(value: T): T | undefined - Returns the previous value of a state or prop. Useful for comparing current and previous values in effects.

useToggle

  • useToggle(defaultState?: boolean): readonly [boolean, () => void, { readonly open: () => void; readonly close: () => void; }, React.Dispatch<React.SetStateAction<boolean>>] - Returns boolean state, a toggle function, helper actions (open/close), and the underlying state setter.

Types

  • Prettify<T> - Prettify type

Utils

Array Operations

  • chunkArray<T>(input: unknown, size: number): T[] - Splits an array into chunks of specified size.
  • groupBy<T, K extends string | number | symbol>(arr: T[], getKey: (item: T) => K): Record<K, T[] | undefined> - Groups array elements by a specified key.
  • joinBy(separator: string, stringArray: Array<string | undefined>): string - Joins array elements by a key with a separator.
  • xor<T extends unknown[], R extends unknown[]>(a: T, b: R): Array<T[number] | R[number]> - Returns symmetric difference between two arrays (elements that exist in only one array).
  • xorByKey(a: Record<string, unknown>[] | null | undefined, b: Record<string, unknown>[] | null | undefined, key: string): Record<string, unknown>[] - Returns symmetric difference between two arrays based on a key property.

Birth Number (Czech)

  • isBirthNumber(value: string | undefined | null): boolean - Validates if a string is a valid Czech birth number (rodné číslo).
  • getDateFromBirthNumber(value: string | undefined | null): Date | null - Extracts date of birth from a birth number.
  • getGenderFromBirthNumber(value: string | undefined | null): Gender - Determines gender from a birth number ('male' | 'female').

File Operations

  • arrayBufferToBase64(buffer: ArrayBuffer | Uint8Array): string - Converts ArrayBuffer to Base64 string.
  • base64ToArrayBuffer(base64: string): Uint8Array<ArrayBuffer> - Converts Base64 string to ArrayBuffer.
  • blobToBase64(blob: Blob): Promise<string> - Converts Blob to Base64 string.
  • downloadFile(file: Blob | ArrayBuffer | string | undefined | null, filename: string, mimeType?: string): void - Triggers file download in the browser.
  • openFile(file: Blob | ArrayBuffer | string | undefined | null, mimeType?: string, newWindow?: boolean): void - Opens file in the browsers.

Number Operations

  • getNumber(value: unknown, defaultValue?: number): number | null - Safely converts value to number, returns default value (null) if conversion fails.
  • getNumberFloat(value: unknown, defaultValue?: number): number | null - Safely converts value to float number, returns default value (null) if conversion fails.
  • getNumberString(value: unknown): string | null - Converts value to numeric string, removes non-numeric characters.
  • getSafeMax(a: number | null | undefined, b: number | null | undefined): number | undefined - Returns max of two numbers with null/undefined fallback handling.
  • getSafeMin(a: number | null | undefined, b: number | null | undefined): number | undefined - Returns min of two numbers with null/undefined fallback handling.

Object Operations

  • cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): Partial<T> - Removes undefined and null values from an object.
  • getObjectDiff<T extends Record<string, unknown>, R extends Record<string, unknown>>(object: T | undefined | null, base: R | undefined | null): Partial<T> - Returns differences between two objects.
  • getObjectValue(obj: Record<string, unknown> | null | undefined, path: string | string[], defaultValue?: unknown): unknown - Gets nested object value by path string (e.g., 'user.address.city').
  • setObjectValue<T extends Record<string, unknown>>(obj: T, path: string, value: unknown): T - Sets nested object value by path string.
  • hasObjectProperty(object: Record<string, unknown> | undefined | null, key: string): boolean - Type-safe check if object has a property.
  • mapObjectKeys<T extends Record<string, unknown>, TResult>(obj: T, iteratee: (value: T[keyof T], key: keyof T) => TResult): { [K in keyof T]: TResult } - Maps object keys using a transformation function.
  • mergeDeep<T extends Record<string, unknown>>(target: T, source: unknown): T - Deep merges multiple objects.

Phone Number

  • removePrefixFromPhoneNumber(value: string, prefixes?: string[]): string - Removes country prefix from phone number (+420, +421).

Range Operations

  • createRange(min: number | null | undefined, max: number | null | undefined): Range | null - Creates a range object with start and end values.
  • isInRange(value: number | null | undefined, range: Range | null | undefined): boolean - Checks if a value is within a range.
  • unionRange(first: Range | null, second: Range | null): Range | null - Returns intersection for two ranges; if one range is null, returns the other; returns null only when both are null or when ranges do not overlap.

String Operations

  • removeSpecialChars(value: string): string - Removes special characters from string, keeps only alphanumeric.
  • removeWhitespaces(value: string): string - Removes all whitespace characters from string.
  • removeWhitespacesFromUnknown(input: unknown): unknown - Safely removes whitespaces from unknown value type.

URL & Path

  • composeUrlRequestString(parameters: Record<string, string | number | undefined | null>): string - Builds an encoded query string from parameters, skipping null, undefined, and empty-string values.
  • makeAbsolutePath(...relativePaths: string[]): string - Converts relative path to absolute URL.

Browser Operations

  • scrollToBottom(): void - Scrolls page to bottom, optionally with smooth animation.
  • scrollToTop(): void - Scrolls page to top, optionally with smooth animation.
  • getDateObject(input: string): Date | null - Validates a date string and returns a Date instance, or null for invalid input.

Validators

Type Validators

  • isArrayBuffer(value: unknown): value is ArrayBuffer - Checks if a value is an ArrayBuffer.
  • isBlob(value: unknown): value is Blob - Checks if a value is a Blob.
  • isBoolean(value: unknown): value is boolean - Checks if a value is a boolean.
  • isFile(value: unknown): boolean - Checks if a value is instance of File
  • isFunction(value: unknown): value is Function - Checks if a value is a function.
  • isNull(value: unknown): value is null - Checks if a value is null.
  • isNullOrUndefined(value: unknown): value is null | undefined - Checks if a value is null or undefined.
  • isNumber(value: unknown): value is number - Checks if a value is a number.
  • isNumberOrString(value: unknown): value is number | string - Checks if a value is a number or string.
  • isObject<T extends Record<string, unknown>>(value: unknown): value is T - Checks if a value is an object (excluding null and arrays).
  • isString(value: unknown): value is string - Checks if a value is a string.
  • isUndefined(value: unknown): value is undefined - Checks if a value is undefined.

Value Validators

  • isEmpty<T>(value: T | undefined | null): value is undefined | null - Checks if a value is empty (null, undefined, empty string, empty array, or empty object).
  • isEmptyString(value: unknown): value is '' - Checks whether a value is exactly an empty string ('').
  • isEqual(a: unknown, b: unknown): boolean - Deep equality comparison between two values.
  • isValidDate(input: string | null | undefined): boolean - Checks whether a date string is parseable into a valid Date.

String Format Validators

  • isEmail(value: string | undefined | null): boolean - Validates if a string is a valid email address.
  • isNaturalNumericString(value: unknown): boolean - Checks if a string contains only numeric characters (0-9).
  • isNaturalNumericStringWithWhitespaces(value: unknown): boolean - Checks if a string contains only numeric characters and whitespaces.
  • isNumericString(value: unknown): boolean - Checks if a string can be converted to a valid number.

Czech/Slovak Specific Validators

  • isBankAccount(account: string | null | undefined): boolean - Validates Czech/Slovak bank account number format.
  • isDIC(value: string | null | undefined): boolean - Validates Czech/Slovak VAT identification number (DIČ).
  • isICO(value: string | number | null | undefined): boolean - Validates Czech/Slovak company identification number (IČO).
  • isModulo11(number: number): boolean - Validates if a number passes the modulo 11 check (used for Czech IDs).
  • isZipCode(zipCode: string | undefined | null): boolean - Validates Czech ZIP code format (XXXXX or XXX XX).

Phone Number Validators

  • isValidCzechPhoneNumber(phoneNumber: string | null | undefined): boolean - Validates Czech phone number format (+420 or without prefix).
  • isValidForeignPhoneNumber(phoneNumber: string | null | undefined): boolean - Validates international phone number format with country prefix.

Document Validators

  • isValidDrivingLicense(value: string | null | undefined): boolean - Validates Czech driving license number format.
  • isValidIdentityCard(value: string | number | null | undefined): boolean - Validates Czech identity card number format.
  • isValidPassport(value: string | null | undefined): boolean - Validates Czech passport number format.
  • isValidVehicleIdentification(value: string | null | undefined): boolean - Validates vehicle identification number (VIN) format.

Yup Validators

  • isBirthNumberYupTest(message: string) - Creates Yup test config for Czech birth number validation.
  • isDateSameOrAfterYupWhen(message: string) - Creates Yup when condition for reference date being same or after a date.
  • isDateSameOrBeforeYupWhen(message: string) - Creates Yup when condition for reference date being same or before a date.
  • isDateYupTest(message: string) - Creates Yup test config for valid date validation.
  • isDICYupTest(message: string) - Creates Yup test config for DIČ validation.
  • isEmailYupTest(message: string) - Creates Yup test config for email validation.
  • isModulo11YupTest(message: string) - Creates Yup test config for modulo 11 validation.
  • isICOYupTest(message: string) - Creates Yup test config for IČO validation.
  • isNumberSameOrGreaterYupWhen(message: string) - Creates Yup when condition for number being same or greater than reference.
  • isNumberSameOrLowerYupWhen(message: string) - Creates Yup when condition for number being same or lower than reference.
  • maxDigitsLengthYupTest(maxDigits: number, message: string) - Creates Yup test config for maximum digits length.
  • minDigitsLengthYupTest(minDigits: number, message: string) - Creates Yup test config for minimum digits length.

License

UNLICENSED