- 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.
$ npm i normalize-words
or
$ yarn add normalize-words
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'
});
Original String to normalize.
This property is mandatory for normalization.
This property is mandatory for type of normalization.
(Optional)
Minimum of characters required for normalization.
(Optional)
Maximum character limit accepted for normalization.
(Optional)
Do not normalize words with a specific length.
(Optional)
Removes specific words from the string based on the array list.
NOTE: Words list is not case sensitive.
(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.
(Optional)
Any function to perform after normalizing the string.
- 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
{ 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"
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: ' my cRazY String ',
transformType: 'toUpper',
removeWords: ['My']
});
// Returns: "CRAZY STRING"
{ removeCharacters: string[] }
const { normalizeWords } = require('normalize-words');
normalizeWords({
str: ' my cRazY String !!',
transformType: 'toUpper',
removeCharacters: ['m', 'Y', '!']
});
// Returns: "CRAZ STRING"
{ 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"
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.
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']
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."
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."
@anselmodev |
---|