normalize-text
Version:
Provides a simple API to normalize texts, white-spaces, paragraphs & diacritics.
195 lines (183 loc) • 7.09 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.normalizeText = {}));
})(this, (function (exports) { 'use strict';
/**
* 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));
};
/**
* Performs function composition in LTR (Left To Right) direction.
*
* @example
* const normalizeWhiteSpaces = text => name.replace(/\s+/g, ' ').trim();
*
* const getInitials = pipe(
* normalizeWhiteSpaces,
* name => name.split(' ').map(name => name.charAt(0)),
* initials => initials.join('').toLocaleUpperCase()
* );
*
* getInitials('Vitor Luiz Cavalcanti');
* //=> "VLC"
*
* @param {Function} fn - An arity N function. Its result is the argument of next one.
* @param {...Function[]} fns - Functions of arity 1. Each one's result is next's argument.
* @returns {Function}
*/
function pipe(fn) {
var fns = [].slice.call(arguments, 1);
return function () {
return fns.reduce(function (x, fn) { return fn(x); }, fn.apply(null, arguments));
};
}
/**
* 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);
exports.capitalizeFirstLetter = capitalizeFirstLetter;
exports.default = normalizeText;
exports.normalizeDiacritics = normalizeDiacritics;
exports.normalizeName = normalizeName;
exports.normalizeParagraph = normalizeParagraph;
exports.normalizeText = normalizeText;
exports.normalizeWhiteSpaces = normalizeWhiteSpaces;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=index.umd.js.map