A collection of string utility functions.
npm install --save @hyfi06/str-utils
Here is an example of how to use the sanitizeStr
function:
const { sanitizeStr } = require("@hyfi06/str-utils");
const str = " HéllO my friend! ";
const config = { removeAccents: true, toUpperCase: true };
const result = sanitizeStr(str, config);
console.log(result); // Prints: 'HELLO MY FRIEND!'
areStringsSimilar(str1: string, str2: string): boolean
: Compares two strings for similarity. Removes leading, trailing, and duplicate spaces, removes accents, and converts the strings to uppercase before comparing them.multiReplace(str: string, replace: { [pattern: string]: string } = {}): string
: Replaces all occurrences of specified patterns in a given string.removeAccents(str: string): string
: Removes all accents from a given string.removeSpaces(str: string): string
: Removes leading, trailing, and duplicate spaces in a given string.sanitizeJoin(array: any[], separator: string = " "): string
: Joins an array into a string using a specified separator, ignoring any falsy values in the array. It also removes leading, trailing, and duplicate spaces from the resulting string.sanitizeStr(str: string, config: { removeAccents?: boolean, toUpperCase?: boolean } = {}): string
: Processes a given string based on a configuration object. By default, it removes leading, trailing, and duplicate spaces. If the configuration object specifies, it can also remove accents and convert the string to uppercase.
The areStringsSimilar
function compares two strings for similarity. It removes leading, trailing, and duplicate spaces, removes accents, and converts the strings to uppercase before comparing them.
str1
(string): The first string to compare.str2
(string): The second string to compare.
- (boolean): Returns
true
if the processed strings are equal,false
otherwise.
const str1 = " HéllO my friend! ";
const str2 = "hello my friend!";
const result = areStringsSimilar(str1, str2);
// result is true
The multiReplace
function replaces all occurrences of the keys in the replace object with the corresponding values in the given string.
str
(string): The string to perform replacements on.replace
(object): An object where the keys are the patterns to be replaced and the values are the replacements. Defaults to an empty object.
- (string): A new string where all occurrences of the keys in the replace object in str have been replaced with the corresponding values.
const str = "foo biz baz";
const replace = {
foo: "0",
biz: "1",
baz: "2",
};
const result = multiReplace(str, replace);
// result is '0 1 2'
The removeAccents
function removes all accents from a given string.
str
(string): The string to remove accents from.
- (string): The string with all accents removed.
const str = "Héctor";
const result = removeAccents(str);
// result is 'Hector'
The removeSpaces
removes leading, trailing, and duplicate spaces in a given string.
str
(string): The string to remove spaces from.
- (string): The string with all leading, trailing, and duplicate spaces removed.
const str = " hello my friend! ";
const result = removeSpaces(str);
// result is 'hello my friend!'
The sanitizeJoin
function joins an array into a string using a specified separator, ignoring any falsy values in the array. It also removes leading, trailing, and duplicate spaces from the resulting string.
array
(Array): The array to join.separator
(string): The separator to use. Defaults to a single space.
- (string): The joined string with all leading, trailing, and duplicate spaces removed.
const array = ["hello", "", "my", null, "friend!", " "];
const separator = " ";
const result = sanitizeJoin(array, separator);
// result is 'hello my friend!'
The sanitizeStr
function processes a given string based on a configuration object. By default, it removes leading, trailing, and duplicate spaces. If the configuration object specifies, it can also remove accents and convert the string to uppercase.
str
(string): The string to process.config
(object): An optional configuration object. Can have the following properties:removeAccents
(boolean): Iftrue
, removes accents from the string.toUpperCase
(boolean): Iftrue
, converts the string to uppercase.
- (string): The processed string.
const str = " HéllO my friend! ";
const config = {
removeAccents: true,
toUpperCase: true,
};
const result = sanitizeStr(str, config);
// result is 'HELLO MY FRIEND!'
Copyright (c) 2023 Héctor Olvera Vital
This project is licensed under the MIT License, which means you are free to use, modify, and distribute the code for both commercial and non-commercial purposes. However, the software is provided "as is," without any warranty or guarantee of its effectiveness or suitability for your specific needs. Please review the license file for more details.