UNPKG

nr-sdk

Version:

Global functions that will help you create websites

176 lines (126 loc) 4.37 kB
# My Utility Library A collection of utility functions to handle various tasks like translations, encryption, date formatting, array reductions, file conversions, and currency formatting. ## Installation To use this library in your project, clone or download the repository, and import the necessary functions into your project. ```bash git clone nr-sdk ``` ## Usage After cloning the repo, you can import the functions like this: ```typescript import { setLanguage, translate, translateWithPrefix, translateWithSuffix, encryptText, decryptText, setEncryptionKey, roundPriceToNearestMultiple, convertFileToBase64, getCurrentDateString, convertToISODate, generateRandomString, getCurrentDateTimeString, reduceArrayByKey, toFix2, toFix3, convertStringToNumber, formatCurrency, } from './your-library-path'; ``` ## Functions ### `setLanguage(langCode: string): void` Set the current language used for translations. Supported languages: `en`, `id`, `zh`, `ms`. ```typescript setLanguage('id'); ``` ### `translate(key: string): string` Get the translation for a specific key based on the currently selected language. ```typescript translate('hello'); // Output: 'Halo' (if 'id' is set) ``` ### `translateWithPrefix(prefix: string, key: string): string` Get the translation with a prefix. ```typescript translateWithPrefix('Welcome', 'hello'); // Output: 'Welcome Halo' ``` ### `translateWithSuffix(suffix: string, key: string): string` Get the translation with a suffix. ```typescript translateWithSuffix('User', 'hello'); // Output: 'Hello User' ``` ### `setEncryptionKey(encKey: string): void` Set a new encryption key for encryption/decryption. ```typescript setEncryptionKey('newEncryptionKey'); ``` ### `encryptText(value: string): string` Encrypt a string using the set encryption key. ```typescript const encrypted = encryptText('mySecret'); // Output: 'mySecretdummy-encrypted' ``` ### `decryptText(value: string): string` Decrypt a previously encrypted string. ```typescript const decrypted = decryptText(encrypted); // Output: 'mySecret' ``` ### `roundPriceToNearestMultiple(price: number, nearestMultiple: number): number` Round the price to the nearest multiple of the provided number. ```typescript const roundedPrice = roundPriceToNearestMultiple(1050, 100); // Output: 1100 ``` ### `convertFileToBase64(file: File): Promise<string>` Convert a file to a base64 string. ```typescript convertFileToBase64(myFile).then(base64 => console.log(base64)); ``` ### `getCurrentDateString(lang?: string): string` Get the current date in `YYYY-MM-DD` format (or `DD-MM-YYYY` if `id` is selected). ```typescript getCurrentDateString(); // Output: '2024-10-08' (or '08-10-2024' in 'id') ``` ### `getCurrentDateTimeString(lang?: string): string` Get the current date and time in `YYYY-MM-DD HH:MM:SS` format (or `DD-MM-YYYY` if `id` is selected). ```typescript getCurrentDateTimeString(); // Output: '2024-10-08 14:30:45' ``` ### `reduceArrayByKey<T extends object>(array: T[], key: keyof T): number` Sum up all values for the specified key in an array of objects. ```typescript const data = [{ price: 1000 }, { price: 2000 }]; const total = reduceArrayByKey(data, 'price'); // Output: 3000 ``` ### `convertToISODate(dateString: string, lang?: string): string` Convert a date string from `DD-MM-YYYY` to `YYYY-MM-DD` format. ```typescript convertToISODate('08-10-2024', 'id'); // Output: '2024-10-08' ``` ### `generateRandomString(length: number): string` Generate a random string of the specified length. ```typescript const randomStr = generateRandomString(10); // Output: 'Ab12Cd34Ef' ``` ### `toFix2(value: number): string` Round a number to 2 decimal places and return it as a string. ```typescript toFix2(1); // Output: '1.00' ``` ### `toFix3(value: number): string` Round a number to 3 decimal places and return it as a string. ```typescript toFix3(1); // Output: '1.000' ``` ### `convertStringToNumber(value: string): number` Convert a string to a number. ```typescript convertStringToNumber('123.45'); // Output: 123.45 ``` ### `formatCurrency(value: number): string` Format a number into a currency string (`USD` format, with commas and 2 decimal places). ```typescript formatCurrency(1000); // Output: '$1,000.00' ``` --- ## License This project is licensed under the MIT License.