(input)
| 300 | * @returns {String} The resulting Punycode string of ASCII-only symbols. |
| 301 | */ |
| 302 | const encode = function(input) { |
| 303 | const output = []; |
| 304 | |
| 305 | // Convert the input in UCS-2 to an array of Unicode code points. |
| 306 | input = ucs2decode(input); |
| 307 | |
| 308 | // Cache the length. |
| 309 | const inputLength = input.length; |
| 310 | |
| 311 | // Initialize the state. |
| 312 | let n = initialN; |
| 313 | let delta = 0; |
| 314 | let bias = initialBias; |
| 315 | |
| 316 | // Handle the basic code points. |
| 317 | for (const currentValue of input) { |
| 318 | if (currentValue < 0x80) { |
| 319 | output.push(stringFromCharCode(currentValue)); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | const basicLength = output.length; |
| 324 | let handledCPCount = basicLength; |
| 325 | |
| 326 | // `handledCPCount` is the number of code points that have been handled; |
| 327 | // `basicLength` is the number of basic code points. |
| 328 | |
| 329 | // Finish the basic string with a delimiter unless it's empty. |
| 330 | if (basicLength) { |
| 331 | output.push(delimiter); |
| 332 | } |
| 333 | |
| 334 | // Main encoding loop: |
| 335 | while (handledCPCount < inputLength) { |
| 336 | |
| 337 | // All non-basic code points < n have been handled already. Find the next |
| 338 | // larger one: |
| 339 | let m = maxInt; |
| 340 | for (const currentValue of input) { |
| 341 | if (currentValue >= n && currentValue < m) { |
| 342 | m = currentValue; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>, |
| 347 | // but guard against overflow. |
| 348 | const handledCPCountPlusOne = handledCPCount + 1; |
| 349 | if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { |
| 350 | error('overflow'); |
| 351 | } |
| 352 | |
| 353 | delta += (m - n) * handledCPCountPlusOne; |
| 354 | n = m; |
| 355 | |
| 356 | for (const currentValue of input) { |
| 357 | if (currentValue < n && ++delta > maxInt) { |
| 358 | error('overflow'); |
| 359 | } |
no test coverage detected
searching dependent graphs…