Text Cleaner
A small tool for easily cleaning text.
Installation
npm install text-cleaner --save
Example
const TextCleaner = ; ;// some text to clean
Usage
Constructor: TextCleaner(string)
const cleanString = ;
Returns an object, with the following methods:
#valueOf() and #toString()
Returns the current working value of the string being cleaned
// "STRING"// "STRING"
#length
length// 6
#remove(search string)
// "sing"
#replace(search string, replace string)
// "sling"
#trim()
// "string"
#toLowerCase()
// "string"
#toUpperCase()
// "STRING"
#truncate(length)
// "a long"
#condense()
Condenses all white space to a single space
// "s t r i n g"
#stripEmails()
// "Email me at: "
#stripHtml()
// "string"
#removeChars(options)
Remove all non-alpha characters, including numbers. Only letters, white space and characters specified in the exclude option will not be removed.
Options (object):
- replaceWith (default: "") Character to replace matched characters with. Allows for characters to be replaced by a space, preventing words from merging on character removal.
- exclude: (default: "") String of characters to exclude. These are added to a regular expression; e.g. "0-9" would exclude numbers from replacement
// "string!"
#removeApostrophes()
Remove apostrophes from the text, but leave other single quotes in the text.
// "a quote: 'he didnt'"
Allows words containing apostrophes to be treated separately to removeChars()
, such as when replacing characters with a space with removeChars({ replaceWith: ' ' })
, preserving the word.
/* undesired behaviour */// "don t text" /* desired behaviour */// "dont text"
#removeStopWords()
Remove common stop words from the text for textual/sentiment anlysis. Uses stopword.
// "test string words"