sanitize-tokens
v1.0.0
Published
Returns an array of tokens found in the provided strings.
Downloads
5
Maintainers
Readme
sanitize-tokens
Returns an array of tokens found in the provided strings.
Tokens are separated by whitespace. In HTML, tokens are used in class
and rel
attributes, for example.
Installation
Requires Node.js 7.0.0 or above.
npm i sanitize-tokens
API
The module exports a single function.
Parameters
- Bindable:
tokens
(string or array): A string of space-separated tokens, or an array (which may contain nested arrays) of such strings. - Optional: Object argument:
elseThrow
(boolean, string, or Error): The error to throw if an invalid token is encountered. AnError
object, if provided, will be thrown as-is. A string will be wrapped in aTypeError
and thrown. If set totrue
, aTypeError
will be thrown. Defaults tofalse
.unique
(boolean): Whether to remove duplicate tokens. Defaults tofalse
.
Return Value
An array of tokens.
Example
const sanitizeTokens = require('sanitize-tokens')
sanitizeTokens(['this is', [['a'], 'test']]) // ['this', 'is', 'a', 'test']