1.0.16 • Published 9 months ago

nr-sdk v1.0.16

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

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.

1.0.16

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.0

9 months ago