TokenList

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.

You can call the method via the monster namespace new Monster.Types.TokenList().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/monster.js';
new Monster.Types.TokenList("myclass row")
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {TokenList} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/types/tokenlist.js';
new TokenList("myclass row")
</script>

This class implements the iteration protocol.

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

Constructor

# new TokenList(init)

Parameters:
Name Type Description
init array | string | iteratable
Since:
  • 1.2.0

Methods

# add(value) → {TokenList}

add tokens

new TokenList().add("abc xyz").toString(); // ↦ "abc xyz"
new TokenList().add(["abc","xyz"]).toString(); // ↦ "abc xyz"
new TokenList().add(undefined); // ↦ add nothing
Parameters:
Name Type Description
value array | string | iteratable
Throws:

unsupported value

Type
TypeError
Returns:
Type
TokenList

# clear() → {TokenList}

remove all tokens

Returns:
Type
TokenList

# contains(value) → {boolean}

Returns true if it contains token, otherwise false

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
Parameters:
Name Type Description
value array | string | iteratable
Returns:
Type
boolean

# entries() → {array}

returns an array with all tokens

Returns:
Type
array

# forEach(callback) → {TokenList}

executes the provided function with each value of the set

Parameters:
Name Type Description
callback function
Returns:
Type
TokenList

# getIterator() → {Symbol.iterator}

Iterator protocol

Returns:
Type
Symbol.iterator

# remove(value) → {TokenList}

Removes token

new TokenList("abc xyz").remove("xyz").toString(); // ↦ "abc"
new TokenList("abc xyz").remove(["xyz"]).toString(); // ↦ "abc"
new TokenList("abc xyz").remove(undefined); // ↦ remove nothing
Parameters:
Name Type Description
value array | string | iteratable
Throws:

unsupported value

Type
TypeError
Returns:
Type
TokenList

# replace(token, newToken) → {TokenList}

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:
Name Type Description
token string
newToken string
Returns:
Type
TokenList

# Symbol.iterator() → {Object}

Iterator

Returns:
Type
Object

# toggle(value) → {boolean}

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

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
Parameters:
Name Type Description
value array | string | iteratable
Throws:

unsupported value

Type
TypeError
Returns:
Type
boolean

# toString() → {string}

returns the individual tokens separated by a blank character

Returns:
Type
string