import Immutable from "immutable" ; const inherits = require ( "util" ) . inherits ; import STYLES from "../constants/styles" ; import isBlock from "../utils/isBlock" ; import isEntity from "../utils/isEntity" ; import isStyle from "../utils/isStyle" ; const TokenRecord = Immutable . Record ( { type : String ( ) , data : Immutable . Map ( ) , text : null , raw : String ( ) , tokens : Immutable . List ( ) , } ) ; function Token ( def ) { if ( ! ( this instanceof Token ) ) { return new Token ( def ) ; } TokenRecord . call ( this , { type : def . type , data : Immutable . Map ( def . data ) , text : def . text , raw : def . raw , tokens : Immutable . List ( def . tokens ) , } ) ; } inherits ( Token , TokenRecord ) ; Token . prototype . getType = function ( ) { return this . get ( "type" ) ; } ; Token . prototype . getData = function ( ) { return this . get ( "data" ) ; } ; Token . prototype . getText = function ( ) { return this . get ( "text" ) ; } ; Token . prototype . getRaw = function ( ) { return this . get ( "raw" ) ; } ; Token . prototype . getTokens = function ( ) { return this . get ( "tokens" ) ; } ; return { boolean } Token . prototype . isBlock = function ( ) { return isBlock ( this ) ; } ; return { boolean } Token . prototype . isInline = function ( ) { return ! this . isBlock ( ) ; } ; return { boolean } Token . prototype . isStyle = function ( ) { return isStyle ( this ) ; } ; return { boolean } Token . prototype . isEntity = function ( ) { return isEntity ( this ) ; } ; param { Token } token return { Token } Token . prototype . mergeWith = function ( token ) { return this . merge ( { type : token . getType ( ) , text : this . getText ( ) + token . getText ( ) , raw : this . getRaw ( ) + token . getRaw ( ) , data : this . getData ( ) . merge ( token . getData ( ) ) , tokens : this . getTokens ( ) . concat ( token . getTokens ( ) ) , } ) ; } ; param { Token } token return { Token } Token . prototype . pushToken = function ( token ) { return this . merge ( { tokens : this . getTokens ( ) . push ( token ) , } ) ; } ; param { Object | Map } return { Token } Token . prototype . setData = function ( data ) { return this . set ( "data" , Immutable . Map ( data ) ) ; } ; return { string } Token . prototype . getAsPlainText = function ( ) { const tokens = this . getTokens ( ) ; if ( tokens . size === 0 ) { return this . getText ( ) || "" ; } return tokens . reduce ( ( text , tok ) => { return text + tok . getAsPlainText ( ) ; } , "" ) ; } ; param { Object } tok return { Token } Token . create = function ( type , tok ) { tok = tok || { } ; let text = tok . text || "" ; const tokens = Immutable . List ( tok . tokens || [ ] ) ; const data = Immutable . Map ( tok . data || { } ) ; if ( tokens . size > 0 ) { text = undefined ; } return new Token ( { type : type , text : text , raw : tok . raw || "" , tokens : tokens , data : data , } ) ; } ; param { string } text return { Token } Token . createText = function ( text ) { return Token . create ( STYLES . TEXT , { text : text , raw : text , } ) ; } ; export default Token ;