-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (44 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
/**
* Create a new `Token` with the given `value` and `type`.
*
* ```js
* const token = new Token('*', 'Star');
* const token = new Token({type: 'star', value: '*'});
* console.log(token) //=> Token { type: 'star', value: '*' }
* ```
* @name Token
* @param {String|Object} `type` The token type to use when `value` is a string.
* @param {String} `value` Value to set
* @return {Object} Token instance
* @api public
*/
class Token {
constructor(type, value, match) {
if (Array.isArray(value)) {
match = value;
value = type.value || match[0];
}
if (isObject(type)) {
for (const key in type) {
this[key] = type[key];
}
} else {
this.type = type;
this.value = value;
}
if (match) {
this.match = match;
}
}
get isToken() {
return true;
}
static isToken(token) {
return isObject(token) && token.isToken === true;
}
}
function isObject(val) {
return val && typeof val === 'object' && !Array.isArray(val);
}
module.exports = Token;