@mmb-digital/shared-libs-auto
v0.0.17
Published
MMB shared libs
Maintainers
Keywords
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-autoor
npm install @mmb-digital/shared-libs-autoUsage
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: string): string- Formats bank account number to standard format (prefix-account/bankCode).inputFormatBankAccount(value: string): string- Formats bank account number to standard format (prefix-account/bankCode) for input fields.
Bytes
formatBytes(bytes: number, decimals?: number): string- Converts number of bytes to human-readable format (KB, MB, GB, etc.).
Phone Number
formatCZSKPhoneNumber(value: string): string- Formats phone number for CZ/SK to standard format (+420 xxx xxx xxx).inputFormatCZSKPhoneNumber(value: string): string- Formats phone number input in real-time while typing.
ZIP Code
formatCzZipCode(value: string): string- Formats Czech ZIP code to format xxx xx.inputFormatCzZipCode(value: string): string- Formats ZIP code input in real-time while typing.
Date & Time
formatDate(date: Date | string, format?: string): string- Formats date to the desired format.formatDateTime(date: Date | string, format?: string): string- Formats date and time to the desired format.
ICO
formatICO(value: string): string- Formats ICO (company identification number) to standard format.inputFormatICO(value: string): string- Formats ICO input in real-time while typing.
Numbers
formatNumber(value: number, options?: FormatOptions): string- Formats number with options for separators, decimal places, and currency.formatPercent(value: number, decimals?: number): string- Formats number as percentage.inputFormatNumber(value: string): string- Formats number input in real-time while typing.inputFormatFloat(value: string): string- Formats decimal number input in real-time while typing.
PIN
formatPin(value: string): string- Formats PIN/birth number to standard format.inputFormatPin(value: string): string- Formats PIN input in real-time while typing.
Roman Numerals
formatRomanNumeral(value: number): 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(initialValue?: boolean): [boolean, () => void]- Returns a boolean state and a function to toggle it. Useful for managing modal visibility, menu states, etc.
Utils
Array Operations
chunkArray<T>(array: T[], size: number): T[][]- Splits an array into chunks of specified size.groupBy<T>(array: T[], key: keyof T): Record<string, T[]>- Groups array elements by a specified key.joinBy<T>(array: T[], separator: string, key: keyof T): string- Joins array elements by a key with a separator.xor<T>(array1: T[], array2: T[]): T[]- Returns symmetric difference between two arrays (elements that exist in only one array).xorByKey<T>(array1: T[], array2: T[], key: keyof T): T[]- Returns symmetric difference between two arrays based on a key property.
Birth Number (Czech)
isBirthNumber(value: string): boolean- Validates if a string is a valid Czech birth number (rodné číslo).getDateFromBirthNumber(value: string): Date | null- Extracts date of birth from a birth number.getGenderFromBirthNumber(value: string): Gender | null- Determines gender from a birth number ('male' | 'female').
File Operations
arrayBufferToBase64(buffer: ArrayBuffer): string- Converts ArrayBuffer to Base64 string.base64ToArrayBuffer(base64: string): ArrayBuffer- Converts Base64 string to ArrayBuffer.blobToBase64(blob: Blob): Promise<string>- Converts Blob to Base64 string.downloadFile(data: Blob | string, filename: string, mimeType?: string): void- Triggers file download in the browser.openFile(accept?: string, multiple?: boolean): Promise<FileList>- Opens file picker dialog and returns selected files.
Number Operations
getNumber(value: unknown): number- Safely converts value to number, returns default value (null) if conversion fails.getNumberFloat(value: unknown): number- Safely converts value to float number, returns default value (null) if conversion fails.getNumberString(value: unknown): string- Converts value to numeric string, removes non-numeric characters.getSafeMax(...values: number[]): number- Returns maximum value, handles NaN and Infinity safely.getSafeMin(...values: number[]): number- Returns minimum value, handles NaN and Infinity safely.
Object Operations
cleanObject<T extends object>(obj: T): Partial<T>- Removes undefined and null values from an object.getObjectDiff<T extends object>(obj1: T, obj2: T): Partial<T>- Returns differences between two objects.getObjectValue<T>(obj: object, path: string): T | undefined- Gets nested object value by path string (e.g., 'user.address.city').setObjectValue<T>(obj: object, path: string, value: T): void- Sets nested object value by path string.hasObjectProperty<T extends object>(obj: T, key: PropertyKey): boolean- Type-safe check if object has a property.mapObjectKeys<T extends object>(obj: T, mapper: (key: string) => string): object- Maps object keys using a transformation function.mergeDeep<T>(...objects: Partial<T>[]): T- Deep merges multiple objects.
Phone Number
removePrefixFromPhoneNumber(value: string): string- Removes country prefix from phone number (+420, +421).
Range Operations
createRange(start: number, end: number): Range- Creates a range object with start and end values.isInRange(value: number, range: Range): boolean- Checks if a value is within a range.unionRange(...ranges: Range[]): Range[]- Merges overlapping ranges into minimal set of ranges.
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(value: unknown): string- Safely removes whitespaces from unknown value type.
URL & Path
composeUrlRequestString(baseUrl: string, params: object): string- Composes URL with query parameters.makeAbsolutePath(path: string, baseUrl?: string): string- Converts relative path to absolute URL.
Browser Operations
scrollToTop(smooth?: boolean): void- Scrolls page to top, optionally with smooth animation.getDateObject(date: Date | string): Date- Safely converts date string or Date object to Date instance.
Validators
Type Validators
isArrayBuffer(value: unknown): boolean- Checks if a value is an ArrayBuffer.isBlob(value: unknown): boolean- Checks if a value is a Blob.isBoolean(value: unknown): boolean- Checks if a value is a boolean.isFunction(value: unknown): boolean- Checks if a value is a function.isNull(value: unknown): boolean- Checks if a value is null.isNullOrUndefined(value: unknown): boolean- Checks if a value is null or undefined.isNumber(value: unknown): boolean- Checks if a value is a number.isNumberOrString(value: unknown): boolean- Checks if a value is a number or string.isObject(value: unknown): boolean- Checks if a value is an object (excluding null and arrays).isString(value: unknown): boolean- Checks if a value is a string.isUndefined(value: unknown): boolean- Checks if a value is undefined.
Value Validators
isEmpty(value: unknown): boolean- Checks if a value is empty (null, undefined, empty string, empty array, or empty object).isEmptyString(value: unknown): boolean- Checks if a value is an empty string or contains only whitespace.isEqual(value1: unknown, value2: unknown): boolean- Deep equality comparison between two values.isValidDate(value: unknown): boolean- Checks if a value is a valid Date object or date string.
String Format Validators
isEmail(value: string): boolean- Validates if a string is a valid email address.isNaturalNumericString(value: string): boolean- Checks if a string contains only numeric characters (0-9).isNaturalNumericStringWithWhitespaces(value: string): boolean- Checks if a string contains only numeric characters and whitespaces.isNumericString(value: string): boolean- Checks if a string can be converted to a valid number.
Czech/Slovak Specific Validators
isBankAccount(value: string): boolean- Validates Czech/Slovak bank account number format.isDIC(value: string): boolean- Validates Czech/Slovak VAT identification number (DIČ).isICO(value: string): boolean- Validates Czech/Slovak company identification number (IČO).isModulo11(value: string): boolean- Validates if a number passes the modulo 11 check (used for Czech IDs).isZipCode(value: string): boolean- Validates Czech ZIP code format (XXXXX or XXX XX).
Phone Number Validators
isValidCzechPhoneNumber(value: string): boolean- Validates Czech phone number format (+420 or without prefix).isValidForeignPhoneNumber(value: string): boolean- Validates international phone number format with country prefix.
Document Validators
isValidDrivingLicense(value: string): boolean- Validates Czech driving license number format.isValidIdentityCard(value: string): boolean- Validates Czech identity card number format.isValidPassport(value: string): boolean- Validates Czech passport number format.isValidVehicleIdentification(value: string): boolean- Validates vehicle identification number (VIN) format.
License
UNLICENSED