nr-sdk
v1.0.16
Published
Global functions that will help you create websites
Downloads
32
Readme
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.
git clone nr-sdk
Usage
After cloning the repo, you can import the functions like this:
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
.
setLanguage('id');
translate(key: string): string
Get the translation for a specific key based on the currently selected language.
translate('hello'); // Output: 'Halo' (if 'id' is set)
translateWithPrefix(prefix: string, key: string): string
Get the translation with a prefix.
translateWithPrefix('Welcome', 'hello'); // Output: 'Welcome Halo'
translateWithSuffix(suffix: string, key: string): string
Get the translation with a suffix.
translateWithSuffix('User', 'hello'); // Output: 'Hello User'
setEncryptionKey(encKey: string): void
Set a new encryption key for encryption/decryption.
setEncryptionKey('newEncryptionKey');
encryptText(value: string): string
Encrypt a string using the set encryption key.
const encrypted = encryptText('mySecret'); // Output: 'mySecretdummy-encrypted'
decryptText(value: string): string
Decrypt a previously encrypted string.
const decrypted = decryptText(encrypted); // Output: 'mySecret'
roundPriceToNearestMultiple(price: number, nearestMultiple: number): number
Round the price to the nearest multiple of the provided number.
const roundedPrice = roundPriceToNearestMultiple(1050, 100); // Output: 1100
convertFileToBase64(file: File): Promise<string>
Convert a file to a base64 string.
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).
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).
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.
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.
convertToISODate('08-10-2024', 'id'); // Output: '2024-10-08'
generateRandomString(length: number): string
Generate a random string of the specified length.
const randomStr = generateRandomString(10); // Output: 'Ab12Cd34Ef'
toFix2(value: number): string
Round a number to 2 decimal places and return it as a string.
toFix2(1); // Output: '1.00'
toFix3(value: number): string
Round a number to 3 decimal places and return it as a string.
toFix3(1); // Output: '1.000'
convertStringToNumber(value: string): number
Convert a string to a number.
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).
formatCurrency(1000); // Output: '$1,000.00'
License
This project is licensed under the MIT License.