A bounded part of a text
class Token
Name | Type | Description |
---|---|---|
type | String | type of text object |
index | String | index of text object within containing text |
text | String | string of this text |
constructor: (@type, @index, @text) -> # Store the text length as the length property of the text object @length = @text.length
Create a new text object that is identical to this one
clone: -> return new @constructor(@type, @index, @text)
Create a new Token that joins this and another Token
other
(String|Token): Other text object or stringjoin : (other) -> str = if typeof other is 'string' then other else other.text return new Token(@type, @index, @text + str)
Remove n
characters from the left end (=beginning) of the text
Name | Type | Description |
---|---|---|
n=1 optional = 1 | Number | Number of characters to remove |
removeLeft : (n) -> n or= 1 return new Token(@type, @index, @text.substr(n))
Remove n
characters from the right end (=end) of the text
Name | Type | Description |
---|---|---|
n=1 optional = 1 | Number | Number of characters to remove |
removeRight : (n) -> n or= 1 max = if n < @text.length then @text.length - n else @text.length return new Token(@type, @index, @text.substr(0,max))
Replace first occurrences of a regex
replace : (pat, replacement) -> @text = XRegExp.replace @text, new XRegExp(pat), replacement # TODO return @
Replace all occurrences of a regex
replaceAll : (pat, replacement) -> @text = XRegExp.replace @text, new XRegExp(pat), replacement, 'all' # TODO return @
String.substr-like substring allowing negative indexes
substr : (start, len) -> if start < 0 start = @text.length + start len or= @text.length - start return @text.substr(start, len)