nr-sdk
Version:
Global functions that will help you create websites
176 lines (126 loc) • 4.37 kB
Markdown
A collection of utility functions to handle various tasks like translations, encryption, date formatting, array reductions, file conversions, and currency formatting.
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
```
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';
```
Set the current language used for translations. Supported languages: `en`, `id`, `zh`, `ms`.
```typescript
setLanguage('id');
```
Get the translation for a specific key based on the currently selected language.
```typescript
translate('hello'); // Output: 'Halo' (if 'id' is set)
```
Get the translation with a prefix.
```typescript
translateWithPrefix('Welcome', 'hello'); // Output: 'Welcome Halo'
```
Get the translation with a suffix.
```typescript
translateWithSuffix('User', 'hello'); // Output: 'Hello User'
```
Set a new encryption key for encryption/decryption.
```typescript
setEncryptionKey('newEncryptionKey');
```
Encrypt a string using the set encryption key.
```typescript
const encrypted = encryptText('mySecret'); // Output: 'mySecretdummy-encrypted'
```
Decrypt a previously encrypted string.
```typescript
const decrypted = decryptText(encrypted); // Output: 'mySecret'
```
Round the price to the nearest multiple of the provided number.
```typescript
const roundedPrice = roundPriceToNearestMultiple(1050, 100); // Output: 1100
```
Convert a file to a base64 string.
```typescript
convertFileToBase64(myFile).then(base64 => console.log(base64));
```
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')
```
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'
```
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
```
Convert a date string from `DD-MM-YYYY` to `YYYY-MM-DD` format.
```typescript
convertToISODate('08-10-2024', 'id'); // Output: '2024-10-08'
```
Generate a random string of the specified length.
```typescript
const randomStr = generateRandomString(10); // Output: 'Ab12Cd34Ef'
```
Round a number to 2 decimal places and return it as a string.
```typescript
toFix2(1); // Output: '1.00'
```
Round a number to 3 decimal places and return it as a string.
```typescript
toFix3(1); // Output: '1.000'
```
Convert a string to a number.
```typescript
convertStringToNumber('123.45'); // Output: 123.45
```
Format a number into a currency string (`USD` format, with commas and 2 decimal places).
```typescript
formatCurrency(1000); // Output: '$1,000.00'
```
---
This project is licensed under the MIT License.