1.0.0 • Published 6 years ago

sanitize-tokens v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

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

  1. Bindable: tokens (string or array): A string of space-separated tokens, or an array (which may contain nested arrays) of such strings.
  2. Optional: Object argument:
    • elseThrow (boolean, string, or Error): The error to throw if an invalid token is encountered. An Error object, if provided, will be thrown as-is. A string will be wrapped in a TypeError and thrown. If set to true, a TypeError will be thrown. Defaults to false.
    • unique (boolean): Whether to remove duplicate tokens. Defaults to false.

Return Value

An array of tokens.

Example

const sanitizeTokens = require('sanitize-tokens')

sanitizeTokens(['this is', [['a'], 'test']]) // ['this', 'is', 'a', 'test']