Overview
SafeJS is a JavaScript library that provides a set of functions for validation and security purposes. It offers methods to validate email addresses, passwords, URLs, and phone numbers. Additionally, it includes utility functions for input sanitization, text encryption and decryption, generating random strings, capitalizing strings, and reversing strings.
Installation
npm i safejslib
Alternatively, To use Safe.js you can download the library from the GitHub repository.
Usage
To use Safe.js in your JavaScript project, include the library file in your HTML:
<script src="https://safe.js.org/script/safe.js"></script>
Note:Please replace 'https://safe.js.org/script/safe.js' with the actual file path to your Safe.js library. Make sure to provide the correct file path to ensure that the library is properly included in your HTML file.
Then, you can call the desired validation or utility functions:
const email = 'test@example.com';
const isValidEmail = Safe.validateEmail(email);
console.log(isValidEmail); // true
API Reference
Safe.js provides the following methods:
validateEmail(email: string): boolean
- Validates an email address.validatePassword(password: string): boolean
- Validates a password.validateURL(url: string): boolean
- Validates a URL.validatePhoneNumber(phoneNumber: string): boolean
- Validates a phone number.sanitizeInput(input: string): string
- Sanitizes user input by replacing HTML entities.encryptText(text: string, key: number): string
- Encrypts text using a key.decryptText(encryptedText: string, key: number): string
- Decrypts encrypted text using a key.generateRandomString(length: number): string
- Generates a random string of the specified length.capitalizeString(str: string): string
- Capitalizes the first letter of a string.reverseString(str: string): string
- Reverses a string.
Examples
Here are a few examples to demonstrate the usage of Safe.js:
Example 1: Validating an Email
const email = 'test@example.com';
const isValidEmail = Safe.validateEmail(email);
console.log(isValidEmail); // true
Example 2: Encrypting and Decrypting Text
const text = 'Hello, World!';
const key = 5;
const encryptedText = Safe.encryptText(text, key);
console.log(encryptedText); // 'Mjqqt, Btwqi!'
const decryptedText = Safe.decryptText(encryptedText, key);
console.log(decryptedText); // 'Hello, World!'
Example 3: Generating a Random String
const randomString = Safe.generateRandomString(10);
console.log(randomString); // '8S0YzMb2kV'
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvement, please submit a pull request on the GitHub repository.
License
Safe.js is released under the MIT License. See the LICENSE file for more details.