normalize-words
v1.0.4
Published
String normalization with removal spaces and/or words.
Downloads
6
Maintainers
Readme
Features
- Automatic removal of spaces and tabs.
- Normalization of words with options:
UPPERCASE
,lowercase
,Uppercase the first letter of the string
,First letter of all capitalized words
. - Set the minimum string length to normalization.
- Set the maximum string size to normalization.
- Do not normalize words with specific length.
- Remove words from the string through a list (array).
- Remove characters from the string through a list (array).
- Enables user customized functions to complement normalization.
- Full Typescript compatibility.
API
Install with NPM or YARN:
$ npm i normalize-words
or
$ yarn add normalize-words
Function
Options {object}
Function Normalization
normalizeWords(): string
Returns the normalized string.
String normalization with removal spaces and/or words. This function requires an object with the properties for normalizing the string.
normalizeWords({
str: 'my cRazY String',
transformType: 'toFirst'
});
Options
str: string
Original String to normalize. This property is mandatory for normalization.
transformType: 'toUpper' | 'toLower' | 'toFirst' | 'toFirstAll'
This property is mandatory for type of normalization.
minLength: number
(Optional)
Minimum of characters required for normalization.
maxLength: number
(Optional)
Maximum character limit accepted for normalization.
ignoreByLength: number
(Optional)
Do not normalize words with a specific length.
removeWords: string[]
(Optional)
Removes specific words from the string based on the array list.
NOTE: Words list is not case sensitive.
removeCharacters: string[]
(Optional)
Remove specific characters from the string based on the array list.
NOTE: Words list is not case sensitive. Only one letter per index is allowed.
applyMethod: Function
(Optional)
Any function to perform after normalizing the string.
How to use
Examples:
- Basic Usage
- With "Word" Removal
- With "Character" Removal
- With Minimum and Maximum Character Length
- Do not normalize words with specific length
- Optional Function to string treatment
- Complete example of normalization
- Example of normalization with option reuse
Basic Usage:
{ transformType: 'toUpper' | 'toLower' | 'toFirst' | 'toFirstAll' }
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: ' my cRazY String ',
transformType: 'toUpper'
});
// Returns: "MY CRAZY STRING"
- Basic Typescript example:
import { normalizeWords } from 'normalize-words';
normalizeWords({
str: ' my cRazY String ',
transformType: 'toFirstAll'
});
// Returns: "My Crazy String"
With "Word" Removal:
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: ' my cRazY String ',
transformType: 'toUpper',
removeWords: ['My']
});
// Returns: "CRAZY STRING"
With "Character" Removal:
{ removeCharacters: string[] }
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: ' my cRazY String !!',
transformType: 'toUpper',
removeCharacters: ['m', 'Y', '!']
});
// Returns: "CRAZ STRING"
With "Minimum and Maximum Character Length" to normalize:
{ minLength: number , maxLength: number }
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: 'john pallozo',
transformType: 'toFirstAll',
minLength: 5, // String less than 5 characters, will return an error.
maxLength: 20 // String longer than 20 characters, will return an error.
});
// Returns: "John Pallozo"
Do not normalize words with specific length:
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: 'city of venice is located in italy',
transformType: 'toFirstAll',
ignoreByLength: 2
});
// Returns: "City of Venice is Located in Italy"
// Note: The words: "is" and "in" have not been normalized.
Optional Function to string treatment:
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: 'john pallozo',
transformType: 'toFirstAll',
applyMethod: (normalizedString) => {
return normalizedString + ' - Full Stack Developer.';
}
});
// Returns: "John Pallozo - Full Stack Developer."
// Note: The parameter "normalizedString" in the fuction is mandatory because
// it contains the "Normalized String" previously.
- Another example with Typescript:
import { normalizeWords } from 'normalize-words';
normalizeWords({
str: 'divide string',
transformType: 'toUpper',
applyMethod: (normalizedString: string): string[] => {
return normalizedString.split('');
}
});
// Returns: [ 'D', 'I', 'V', 'I', 'D', 'E', ' ', 'S', 'T', 'R', 'I', 'N', 'G']
Complete example of normalization:
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: '@joHN paLLozO! any word!!',
transformType: 'toFirstAll',
removeWords: ['any', 'word'],
removeCharacters: ['@', '!'],
minLength: 5,
maxLength: 30,
ignoreByLength: 2,
applyMethod: (normalizedString: string): string => {
return normalizedString + ' - Full Stack Developer.';
}
});
// Returns: "John Pallozo - Full Stack Developer."
Example of normalization with options reuse:
const { normalizeWords } = require('normalize-words');
const baseOptions = {
transformType: 'toFirst',
minLength: 5,
maxLength: 30,
ignoreByLength: 2
};
const options1 = {
...baseOptions,
str: 'my CrasY striNG',
};
const options2 = {
...baseOptions,
str: 'john f pallozo!',
transformType: 'toFirstAll',
removeCharacters: ['!'],
};
const mergedOptions = {
...options2,
applyMethod: (normalizedString) => {
return `I'm ${normalizedString}, Full Stack Developer.`;
}
};
// RESULTS:
normalizeWords( options1 );
// Returns: "My crazy string"
normalizeWords( options2 );
// Returns: "John F Pallozo"
normalizeWords( mergedOptions );
// Returns: "I'm John F Pallozo, Full Stack Developer."
Autor
| @anselmodev | | :---: |