normalize-text
Version:
Provides a simple API to normalize texts, white-spaces, paragraphs & diacritics.
155 lines (146 loc) • 5.03 kB
JavaScript
import pipe from '@bitty/pipe';
/**
* Capitalize first character of received text.
* @example
* capitalizeFirstLetter('karl Marx');
* //=> "Karl Marx"
* @param {string} text - A `string` value.
* @returns {string}
*/
var capitalizeFirstLetter = function (text) {
return text.charAt(0).toLocaleUpperCase() + text.substring(1);
};
/**
* If `String.prototype.normalize` is supported it normalizes diacritics by
* replacing them with "clean" character from received text.
* @example
* normalizeDiacritics('Olá, você aí');
* //=> 'Ola, voce ai'
* @param {string} text - A `string` value.
* @returns {string}
*/
var normalizeDiacritics = function (text) {
return !!String.prototype.normalize
? text.normalize('NFKD').replace(/[\u0300-\u036F]/g, '')
: text;
};
/**
* Resolves a value to an array, if it isn't already an array.
* @template T
* @param {T | readonly T[]} value
* @returns {readonly T[]}
*/
var resolveToArray = function (value) {
return Array.isArray(value) ? value : [value];
};
/**
* Normalize all white-space characters and remove trailing ones received text.
* @example
* normalizeWhiteSpaces(' What exactly is it? ');
* //=> "What exactly is it?"
*
* normalizeWhiteSpaces('Hi, how is \r\n everything \t?');
* //=> 'Hi, how is everything ?'
*
* normalizeWhiteSpaces`It is ${temperature}\n degree\r outside. `
* //=> 'It is 25 degree outside.'
* @param {TemplateStringsArray | string} partsOrText
* @param {unknown[]} values
* @returns {string}
*/
function normalizeWhiteSpaces(partsOrText) {
var values = [];
for (var _i = 1; _i < arguments.length; _i++) {
values[_i - 1] = arguments[_i];
}
return resolveToArray(partsOrText)
.reduce(function (text, part, index) { return text + values[index - 1] + part; })
.replace(/\s+/g, ' ')
.trim();
}
/**
* @param {string} word - A `string` value.
* @param {string[]} exceptions - A list of exceptions in lower-case.
* @returns {(_: string, word: string) => string}
*/
var byNormalizedNameExceptFor = function (exceptions) {
/**
* @param {string} _
* @param {string} word
* @returns {string}
*/
return function (_, word) {
return exceptions.indexOf(word) > -1 ? word : capitalizeFirstLetter(word);
};
};
/**
* Normalize received name by normalizing it's white-spaces and capitalizing
* first letter of every word but exceptions (received in lower-case).
* @example
* normalizeName(' fernanDA MONTENEGRO');
* //=> "Fernanda Montenegro"
*
* normalizeName(' wilson da costa', ['da']);
* //=> "Wilson da Costa"
* @param {string} name - A `string` value.
* @param {string[]} [exceptions] - A list of exceptions in lower-case.
* @returns {string}
*/
var normalizeName = function (name, exceptions) {
if (exceptions === void 0) { exceptions = []; }
return normalizeWhiteSpaces(name)
.toLowerCase()
.replace(/([^ -]+)/g, byNormalizedNameExceptFor(exceptions));
};
/**
* Get the last character of received text.
* @param {string} text - A `string` value.
* @returns {string}
*/
var getLastChar = function (text) { return text.charAt(text.length - 1); };
/**
* Add a period at end of a text, if you don't already have one.
* @param {string} text - A `string` value.
*/
var addPeriodAtEnd = function (text) {
return getLastChar(text) === '.' ? text : text + '.';
};
/**
* Normalize a paragraph by normalizing its white-spaces, capitalizing first
* letter and putting a period at end.
* @example
* normalizeParagraph(' once upon a time');
* //=> "Once upon a time."
* @param {string} value
* @returns {string}
*/
var normalizeParagraph = /*#__PURE__*/ pipe(normalizeWhiteSpaces, addPeriodAtEnd, capitalizeFirstLetter);
/**
* Resolve one or multiple texts into a single one.
* @param {string | readonly string[]} values - A `string` or an array of `string` values.
* @returns {string}
*/
var resolveToText = function (values) {
return Array.isArray(values) ? values.join(' ') : values;
};
/**
* Transforms a text to lower case.
* @param {string} text - A `string` value.
* @returns {string}
*/
var transformToLowerCase = function (text) { return text.toLocaleLowerCase(); };
/**
* Resolve received texts (when receives an `Array`) by normalizing its
* white-spaces and its diacritics and transforming to lower-case.
* @example
* normalizeText(' so there\'s a Way to NORMALIZE ');
* //=> "so there\'s a way to normalize"
*
* normalizeText(['Olá\r\n', 'como está a senhorita?']);
* //=> "ola como esta a senhorita?"
* @param {string | readonly string[]} values - A `string` or an array of `string` values.
* @returns {string}
*/
var normalizeText = /*#__PURE__*/ pipe(resolveToText, normalizeDiacritics, normalizeWhiteSpaces, transformToLowerCase);
export { capitalizeFirstLetter, normalizeText as default, normalizeDiacritics, normalizeName, normalizeParagraph, normalizeText, normalizeWhiteSpaces };
//# sourceMappingURL=index.mjs.map