/** Highest positive signed 32-bit float value */ const maxInt = 2147483647 ; /** Bootstring parameters */ const base = 36 ; const tMin = 1 ; const tMax = 26 ; const skew = 38 ; const damp = 700 ; const initialBias = 72 ; const initialN = 128 ; const delimiter = '-' ; /** Regular expressions */ const regexNonASCII = /[^\0-\x7E]/ ; const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g ; /** Error messages */ type errorNames = 'overflow' | 'invalid-input' | 'not-basic' ; const errors = { 'invalid-input' : 'Invalid input' , 'not-basic' : 'Illegal input >= 0x80 (not a basic code point)' , 'overflow' : 'Overflow: input needs wider integers to process' , } ; /** Convenience shortcuts */ const baseMinusTMin = base - tMin ; private param { String } type returns { Error } function error ( type ) { throw new RangeError ( errors [ type ] ) ; } see see memberOf name param { String } string returns { Array } function ucs2decode ( str ) { const output = [ ] ; let counter = 0 ; const length = str . length ; while ( counter < length ) { const value = str . charCodeAt ( counter ++ ) ; if ( value >= 0xd800 && value <= 0xdbff && counter < length ) { const extra = str . charCodeAt ( counter ++ ) ; if ( ( extra & 0xfc00 ) === 0xdc00 ) { output . push ( ( ( value & 0x3ff ) << 10 ) + ( extra & 0x3ff ) + 0x10000 ) ; } else { output . push ( value ) ; counter -- ; } } else { output . push ( value ) ; } } return output ; } see private param { Number } codePoint returns { Number } function basicToDigit ( codePoint ) { if ( codePoint - 0x30 < 0x0a ) { return codePoint - 0x16 ; } if ( codePoint - 0x41 < 0x1a ) { return codePoint - 0x41 ; } if ( codePoint - 0x61 < 0x1a ) { return codePoint - 0x61 ; } return base ; } see private param { Number } digit returns { Number } function digitToBasic ( digit , flag ) { return digit + 22 + 75 * ( digit < 26 ? 1 : 0 ) - ( ( flag !== 0 ? 1 : 0 ) << 5 ) ; } private function adapt ( delta , numPoints , firstTime ) { let k = 0 ; delta = firstTime ? Math . floor ( delta / damp ) : delta >> 1 ; delta += Math . floor ( delta / numPoints ) ; for ( ; delta > ( baseMinusTMin * tMax ) >> 1 ; k += base ) { delta = Math . floor ( delta / baseMinusTMin ) ; } return Math . floor ( k + ( ( baseMinusTMin + 1 ) * delta ) / ( delta + skew ) ) ; } memberOf param { String } input returns { String } export function decode ( input ) { const output = [ ] ; const inputLength = input . length ; let i = 0 ; let n = initialN ; let bias = initialBias ; let basic = input . lastIndexOf ( delimiter ) ; if ( basic < 0 ) { basic = 0 ; } for ( let j = 0 ; j < basic ; ++ j ) { if ( input . charCodeAt ( j ) >= 0x80 ) { error ( 'not-basic' ) ; } output . push ( input . charCodeAt ( j ) ) ; } for ( let index = basic > 0 ? basic + 1 : 0 ; index < inputLength ; ) { const oldi = i ; for ( let w = 1 , k = base ; ; k += base ) { if ( index >= inputLength ) { error ( 'invalid-input' ) ; } const digit = basicToDigit ( input . charCodeAt ( index ++ ) ) ; if ( digit >= base || digit > Math . floor ( ( maxInt - i ) / w ) ) { error ( 'overflow' ) ; } i += digit * w ; const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias ; if ( digit < t ) { break ; } const baseMinusT = base - t ; if ( w > Math . floor ( maxInt / baseMinusT ) ) { error ( 'overflow' ) ; } w *= baseMinusT ; } const out = output . length + 1 ; bias = adapt ( i - oldi , out , oldi === 0 ) ; if ( Math . floor ( i / out ) > maxInt - n ) { error ( 'overflow' ) ; } n += Math . floor ( i / out ) ; i %= out ; output . splice ( i ++ , 0 , n ) ; } return String . fromCodePoint . apply ( null , output ) ; } memberOf param { String } input returns { String } export function encode ( str ) { const output = [ ] ; const input = ucs2decode ( str ) ; const inputLength = input . length ; let n = initialN ; let delta = 0 ; let bias = initialBias ; for ( let i = 0 ; i < input . length ; i += 1 ) { const currentValue = input [ i ] ; if ( currentValue < 0x80 ) { output . push ( String . fromCharCode ( currentValue ) ) ; } } const basicLength = output . length ; let handledCPCount = basicLength ; if ( basicLength ) { output . push ( delimiter ) ; } while ( handledCPCount < inputLength ) { let m = maxInt ; for ( let i = 0 ; i < input . length ; i += 1 ) { const currentValue = input [ i ] ; if ( currentValue >= n && currentValue < m ) { m = currentValue ; } } const handledCPCountPlusOne = handledCPCount + 1 ; if ( m - n > Math . floor ( ( maxInt - delta ) / handledCPCountPlusOne ) ) { error ( 'overflow' ) ; } delta += ( m - n ) * handledCPCountPlusOne ; n = m ; for ( let i = 0 ; i < input . length ; i += 1 ) { const currentValue = input [ i ] ; if ( currentValue < n && ++ delta > maxInt ) { error ( 'overflow' ) ; } if ( currentValue === n ) { let q = delta ; for ( let k = base ; ; k += base ) { const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias ; if ( q < t ) { break ; } const qMinusT = q - t ; const baseMinusT = base - t ; output . push ( String . fromCharCode ( digitToBasic ( t + ( qMinusT % baseMinusT ) , 0 ) ) ) ; q = Math . floor ( qMinusT / baseMinusT ) ; } output . push ( String . fromCharCode ( digitToBasic ( q , 0 ) ) ) ; bias = adapt ( delta , handledCPCountPlusOne , handledCPCount === basicLength ) ; delta = 0 ; ++ handledCPCount ; } } ++ delta ; ++ n ; } return output . join ( '' ) ; } memberOf param { String } input returns { String } export function toUnicode ( input ) { const labels = input . replace ( regexSeparators , '\x2E' ) . split ( '.' ) ; const encoded = [ ] ; for ( let i = 0 ; i < labels . length ; i += 1 ) { encoded . push ( labels [ i ] . startsWith ( 'xn--' ) ? decode ( labels [ i ] . slice ( 4 ) . toLowerCase ( ) ) : labels [ i ] ) ; } return encoded . join ( '.' ) ; } memberOf param { String } input returns { String } export function toASCII ( input ) { const labels = input . replace ( regexSeparators , '\x2E' ) . split ( '.' ) ; const encoded = [ ] ; for ( let i = 0 ; i < labels . length ; i += 1 ) { encoded . push ( regexNonASCII . test ( labels [ i ] ) ? 'xn--' + encode ( labels [ i ] ) : labels [ i ] ) ; } return encoded . join ( '.' ) ; } memberOf type String export const version = '2.1.0' ;