Stringify provides a set of methods for working with JSON strings and objects, case conversion, as well as encryption and decryption capabilities. It extends the Ncrypt class for encryption and decryption, and also allows you to convert JSON strings to JSON objects, and vice versa.
In this library, you'll find a variety of string conversion methods such as converting strings to camel case, snake case, kebab case, and sentence case. You can also encrypt and decrypt strings using the encryption key provided or via the .env file.
This documentation provides an overview of the Stringify class, including its methods, parameters, return types, and examples of how to use them. Whether you're working on a small or large project, Stringify aims to provide an easy-to-use set of tools that can help you with common string manipulation tasks. q
To use Stringify, you need to have Node.js installed on your computer. Once you have Node.js installed, you can install the package using the following command in your terminal:
npm install @limitless.claver/stringify
Add a 32 character
length encryption key to the created .env
file. You can ignore this step if you do not intend to use the encryption methods of this library.
//.env
ENCRYPTION_KEY=6bef904c684547d18f15a47e09ecdbb3
Encrypts a string using the encryption key.
const plaintext = 'my secret message';
const encrypted = Stringify.toEncryptedString(plaintext);
console.log(encrypted); // Encrypted string
value
: The string to encrypt.
- The encrypted string.
Decrypts an encrypted string to plain text using the encryption key.
const encrypted = 'Encrypted string';
const plaintext = Stringify.toDecryptedString(encrypted);
console.log(plaintext); // 'my secret message'
value
: The encrypted string.
- The decrypted string.
Decrypts an encrypted string to a JSON object using the encryption key.
const encrypted = 'Encrypted JSON string';
const jsonObject = Stringify.toDecryptedJSON(encrypted);
console.log(jsonObject); // Decrypted JSON object
value
: The encrypted JSON string.
- The decrypted JSON object.
The formatString
method replaces placeholders in a string template with values from an object.
template
(required) - The string template with placeholders to replace.values
(required) - The object containing the values to use for replacement.
const template = 'Hello, ${firstName} ${lastName}!';
const values = { firstName: 'John', lastName: 'Doe' };
const formattedString = Stringify.formatString(template, values);
console.log(formattedString);
// Output: "Hello, John Doe!"
The padLeft
method pads a string on the left with a specified character until it reaches the desired length.
str
(required) - The string to pad.length
(required) - The length to which the string should be padded.paddingChar
(optional) - The character to use for padding. The default value is a space character.
const str = '123';
const paddedString = Stringify.padLeft(str, 5, '0');
console.log(paddedString);
// Output: "00123"
The padRight
method pads a string on the right with a specified character until it reaches the desired length.
str
(required) - The string to pad.length
(required) - The length to which the string should be padded.paddingChar
(optional) - The character to use for padding. The default value is a space character.
const str = '123';
const paddedString = Stringify.padRight(str, 5, '0');
console.log(paddedString);
// Output: "12300"
The truncate
method truncates a string to a specified maximum length and appends a suffix to the end of the string.
str
(required) - The string to truncate.maxLength
(required) - The maximum length of the truncated string.suffix
(optional) - The suffix to append to the end of the string if it is truncated. The default value is "...".
const str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem';
const truncatedString = Stringify.truncate(str, 50);
console.log(truncatedString);
// Output: "Lorem ipsum dolor sit amet, consectetur adipisc..."
const str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem';
const truncatedString = Stringify.truncate(str, 50, "(continue)");
console.log(truncatedString);
// Output: "Lorem ipsum dolor sit amet, consectetur adipisc (continue)"
Converts the first letter of each word in a given string to uppercase and the remaining letters to lowercase.
Stringify.toTitleCase("example sentence"); // "Example Sentence"
str
: The string to convert to title case.
- A string that has been converted to title case.
Converts all letters in a given string to uppercase.
Stringify.toUpperCase("example sentence"); // "EXAMPLE SENTENCE"
str
: The string to convert to uppercase.
- A string that has been converted to uppercase.
Converts all letters in a given string to lowercase.
Stringify.toLowerCase("Example Sentence"); // "example sentence"
str
: The string to convert to lowercase.
- A string that has been converted to lowercase.
Replaces all occurrences of a substring with a new substring in a given string.
str
: The input string to be processed.oldSubstring
: The substring to be replaced.newSubstring
: The new substring to replace the old substring.
A new string with all occurrences of the old substring replaced with the new substring.
const input = "hello world";
const output = Stringify.replace(input, "world", "universe");
console.log(output); // "hello universe"
Replaces all occurrences of a substring with a new substring in a given string.
str
: The input string to be processed.oldSubstring
: The substring to be replaced.newSubstring
: The new substring to replace the old substring.
A new string with all occurrences of the old substring replaced with the new substring.
const input = "hello world";
const output = Stringify.replaceAll(input, "o", "x");
console.log(output); // "hellx wxrld"
Removes leading/trailing spaces and replaces multiple consecutive spaces with a single space.
value
: The string to remove whitespace from.
A new string with whitespace removed.
const input = " hello world ";
const output = Stringify.removeWhitespace(input);
console.log(output); // "hello world"
Reverses the characters of a string.
value
: The input string to reverse.
A new string with the characters reversed.
const input = "hello world";
const output = Stringify.reverse(input);
console.log(output); // "dlrow olleh"
Reverses the order of words in a string.
str
: The input string to be processed.
A new string with the order of words reversed.
const input = "hello world";
const output = Stringify.reverseWords(input);
console.log(output); // "world hello"
Shuffles the characters of a string.
str
: The input string to be shuffled.
A new string with the characters of the input string shuffled.
const input = "hello world";
const output = Stringify.shuffle(input);
console.log(output); // Output will be different every time due to randomness
Returns a boolean indicating whether the string contains a specified substring.
str
: The input string to search in.substring
: The substring to search for.
A boolean indicating whether the string contains the substring. true
if the substring is found, false
otherwise.
const input = "Hello, world!";
const hasComma = Stringify.contains(input, ",");
console.log(hasComma); // true
const hasExclamation = Stringify.contains(input, "!");
console.log(hasExclamation); // true
const hasNumbers = Stringify.contains(input, "123");
console.log(hasNumbers); // false
Returns the number of times a specified substring appears in the string.
str
: The input string to be searched.substring
: The substring to count occurrences of.
The number of times the substring appears in the string.
const input = "hello world";
const count = Stringify.countOccurrences(input, "l");
console.log(count); // 3
The slugify()
function takes a string as input and returns a URL-friendly slug.
str
: A string to convert to a slug.
The slugified string.
const str = "This is a string with spaces and special characters!";
const slug = slugify(str);
console.log(slug); // Output: "this-is-a-string-with-spaces-and-special-characters"
Encodes an object of key-value pairs as a URL query string.
Stringify.encodeQueryString({ foo: 'bar', baz: 'qux' }); // "foo=bar&baz=qux"
params
: An object containing key-value pairs to be encoded.
- A string containing the encoded key-value pairs as a query string.
Decodes a URL query string into an object of key-value pairs.
Stringify.decodeQueryString("foo=bar&baz=qux"); // { foo: 'bar', baz: 'qux' }
queryString
: A string containing the encoded key-value pairs as a query string.
- An object containing the decoded key-value pairs.
Parses a URL into an object containing its protocol, hostname, port, and pathname, as well as any query parameters.
Stringify.parseUrl("https://www.example.com/search?q=example");
// { protocol: 'https:', hostname: 'www.example.com', port: '', pathname: '/search', q: 'example' }
url
: The URL to be parsed.
- An object containing the parsed URL properties.
Converts XML string to valid JSON objects
Stringify.xmlToJson("<root><name>Claver</name></root>"); // "{name: 'Claver'}"
xml
: The XML string to convert to JSON
- A JSON object converted frm the XML string
Converts JSON objects to valid XML
Stringify.jsonToXml({"name": "Claver"}); // <name>Claver</name>
xml
: The JSON object to convert to XML
- A JSON object converted frm the XML string
toJson
: Converts a JSON string to a JSON ObjecttoString
: Converts a JSON object to a string usingJSON.stringify
Before you can use these methods, please add a .env
file with a 32 alpha numeric string
as an ENCRYPTION_KEY
.
toEncryptedString
: Encrypts and returns an encrypted hash of any string passed to it.toDecryptedString
: Decrypts the encrypted string and returns the original text.toDecryptedJSON
: Decrypts an encrypted stringified JSON object and returns the original JSON object.
toCamelCase
: Converts a string to camel case.toSnakeCase
: Converts a string to snake case.toKebabCase
: Converts a string to kebab case.toSentenceCase
: Converts a string to sentence case.toTitleCase
: Converts a string to title case.toUpperCase
: Converts a string to uppercase.toLowerCase
: Converts a string to lowercase.
encodeQueryString
: Encodes a query string.decodeQueryString
: Decodes a query string.parseUrl
: Parses a URL.
formatString
: Formats a string with placeholders and values.padLeft
: Pads a string with characters on the left.padRight
: Pads a string with characters on the right.truncate
: Truncates a string to a specified length.