@simongrasinovski/str-utils
Version:
A lightweight utility library for effortless string manipulation. Simplify common tasks like case validation, conversion and formatting with easy-to-use functions that enhance your JavaScript applications
45 lines (43 loc) • 1.81 kB
JavaScript
;
var _require = require('./utils'),
ensureString = _require.ensureString,
shuffleArray = _require.shuffleArray;
var _require2 = require('./constants'),
EMPTY_SPACES_REGEX = _require2.EMPTY_SPACES_REGEX;
/**
* Returns all unique characters from a string.
*
* @function
* @param {string} input - The string to analyze.
* @param {object} [options={}] - Configuration object to control additional options.
* @param {boolean} [options.caseSensitive=true] - Whether to treat characters case-sensitively.
* @param {boolean} [options.removeSpaces=false] - Whether to remove spaces before processing.
* @param {boolean} [options.shuffleCharacters=false] - Whether to shuffle the characters in the output string.
* @returns {string} String with unique characters.
*
* @throws {TypeError} Throws an error if the input is not a string.
*
* @example
* const result = uniqueCharacters('hello world');
* console.log(result); // 'helo wrd'
*/
module.exports = ensureString(function (input) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$caseSensitiv = options.caseSensitive,
caseSensitive = _options$caseSensitiv === void 0 ? true : _options$caseSensitiv,
_options$removeSpaces = options.removeSpaces,
removeSpaces = _options$removeSpaces === void 0 ? false : _options$removeSpaces,
_options$shuffleChara = options.shuffleCharacters,
shuffleCharacters = _options$shuffleChara === void 0 ? false : _options$shuffleChara;
if (!caseSensitive) {
input = input.toLowerCase();
}
if (removeSpaces) {
input = input.replace(EMPTY_SPACES_REGEX, '');
}
var uniqueArr = Array.from(new Set(input));
if (shuffleCharacters) {
shuffleArray(uniqueArr);
}
return uniqueArr.join('');
});