Monster.Types. TokenList

A TokenList allows you to manage tokens (individual character strings such as css classes in an attribute string).

The TokenList offers various functions to manipulate values. For example, you can add, remove or replace a class in a CSS list.

This class implements the iteration protocol.

Constructor

new TokenList(init)

Parameters:
NameTypeDescription
initarray | string | iteratable
Since
  • 1.2.0
License
  • AGPLv3
Example
import {TokenList} from '@schukai/monster/source/types/tokenlist.mjs';

typeof new TokenList("myclass row")[Symbol.iterator];
// ↦ "function"

Methods

add(value) → {TokenList}

Add tokens

.

Add tokens

Parameters:
NameTypeDescription
valuearray | string | iteratable
Throws:

unsupported value

Type
TypeError
Returns:
Type: 
TokenList
Example
import {TokenList} from '@schukai/monster/source/types/tokenlist.mjs';

new TokenList().add("abc xyz").toString(); // ↦ "abc xyz"
new TokenList().add(["abc", "xyz"]).toString(); // ↦ "abc xyz"
new TokenList().add(undefined); // ↦ add nothing

clear() → {TokenList}

remove all tokens

.

remove all tokens

Returns:
Type: 
TokenList

contains(value) → {boolean}

Returns true if it contains token, otherwise false

.

Returns true if it contains token, otherwise false

Parameters:
NameTypeDescription
valuearray | string | iteratable
Returns:
Type: 
boolean
Example
import {TokenList} from '@schukai/monster/source/types/tokenlist.mjs';

new TokenList("start middle end").contains('start'); // ↦ true
new TokenList("start middle end").contains('end'); // ↦ true
new TokenList("start middle end").contains('xyz'); // ↦ false
new TokenList("start middle end").contains(['end','start','middle']); // ↦ true
new TokenList("start middle end").contains(['end','start','xyz']); // ↦ false

entries() → {array}

returns an array with all tokens

.

returns an array with all tokens

Returns:
Type: 
array

forEach(callback) → {TokenList}

executes the provided function with each value of the set

.

executes the provided function with each value of the set

Parameters:
NameTypeDescription
callbackfunction
Returns:
Type: 
TokenList

getIterator() → {Symbol.iterator}

Iterator protocol

.

Iterator protocol

Returns:
Type: 
Symbol.iterator

remove(value) → {TokenList}

Removes token

.

Removes token

Parameters:
NameTypeDescription
valuearray | string | iteratable
Throws:

unsupported value

Type
TypeError
Returns:
Type: 
TokenList
Example
import {TokenList} from '@schukai/monster/source/types/tokenlist.mjs';

new TokenList("abc xyz").remove("xyz").toString(); // ↦ "abc"
new TokenList("abc xyz").remove(["xyz"]).toString(); // ↦ "abc"
new TokenList("abc xyz").remove(undefined); // ↦ remove nothing

replace(token, newToken) → {TokenList}

this method replaces a token with a new token.

this method replaces a token with a new token.

if the passed token exists, it is replaced with newToken and TokenList is returned. if the token does not exist, newToken is not set and TokenList is returned.

Parameters:
NameTypeDescription
tokenstring
newTokenstring
Returns:
Type: 
TokenList

Symbol.iterator() → {Object}

Iterator

.

Iterator

Returns:
Type: 
Object

toggle(value) → {boolean}

Removes token from string.

Removes token from string. If token doesn't exist it's added.

Parameters:
NameTypeDescription
valuearray | string | iteratable
Throws:

unsupported value

Type
TypeError
Returns:
Type: 
boolean
Example
import {TokenList} from '@schukai/monster/source/types/tokenlist.mjs';

new TokenList("abc def ghi").toggle("def xyz").toString(); // ↦ "abc ghi xyz"
new TokenList("abc def ghi").toggle(["abc", "xyz"]).toString(); // ↦ "def ghi xyz"
new TokenList().toggle(undefined); // ↦ nothing

toString() → {string}

returns the individual tokens separated by a blank character

.

returns the individual tokens separated by a blank character

Returns:
Type: 
string