String.fromCodePoint

์ •์  String.fromCodePoint() ๋ฉ”์†Œ๋“œ๋Š” ์ฝ”๋“œ ํฌ์ธํŠธ ์‹œํ€€์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

String.fromCodePoint(9731, 9733, 9842, 0x2F804) // "โ˜ƒโ˜…โ™ฒไฝ "

Syntax

String.fromCodePoint(num1, [, ...[ numN ]])

Parameters

num1, ..., numN

์ฝ”๋“œ ํฌ์ธํŠธ์˜ ์‹œํ€€์Šค

Return value

์ฝ”๋“œ ํฌ์ธํŠธ์˜ ์‹œํ€€์Šค๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑํ•œ ๋ฌธ์ž์—ด

Exceptions

์œ ํšจํ•˜์ง€ ์•Š์€ ์œ ๋‹ˆ์ฝ”๋“œ ์ฝ”๋“œํฌ์ธํŠธ๋ฅผ ๋„˜๊ธด๋‹ค๋ฉด RangeError ("RangeError: NaN is not a valid code point")

Description

์ด ๋ฉ”์†Œ๋“œ๋Š” String object๊ฐ€ ์•„๋‹Œ string์„ returnํ•œ๋‹ค. fromCodePoint() ๋Š” String์˜ ์ •์  ๋ฉ”์†Œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์Šค์Šค๋กœ ์ƒ์„ฑํ•œ String object ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•ญ์ƒ String.fromCodePoint()๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

Example

fromCodePoint() ์‚ฌ์šฉํ•˜๊ธฐ

String.fromCodePoint(42); // "*"
String.fromCodePoint(65, 90);   // "AZ"
String.fromCodePoint(0x404);    // "\u0404" -> "ะ„"
String.fromCodePoint(0x2F804);  // "\uD87E\uDC04" -> "ไฝ "
String.fromCodePoint(194564);   // "\uD87E\uDC04" -> "ไฝ "
String.fromCodePoint(0x1D306, 0x61, 0x1D307); // "๐Œ†a๐Œ‡"

์œ ํšจํ•˜์ง€ ์•Š์€ input

String.fromCodePoint('_'); //  RangeError: Invalid code point NaN
// at Function.fromCodePoint (<anonymous>)
// at <anonymous>:1:8

fromCharCode()์™€ ๋น„๊ต

String.fromCharCode()๋Š” ์ฝ”๋“œ ํฌ์ธํŠธ๋ฅผ ์ง€์ •ํ•˜์—ฌ 0x010000 โ€“ 0x10FFFF๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์‹  ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด UTF-16์˜ surrogate pair๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

String.fromCharCode(0xD83C, 0xDF03); // "๐ŸŒƒ"
String.fromCharCode(55356, 57091);   // "๐ŸŒƒ"

๋ฐ˜๋ฉด String.fromCodePoint()๋Š” UTF-32 ์ฝ”๋“œ ์œ ๋‹›์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ ํฌ์ธํŠธ๋ฅผ ์ง€์ •ํ•˜์—ฌ 4-byte ๋ณด์กฐ ๋ฌธ์ž์—ด๊ณผ ์ผ๋ฐ˜์ ์ธ 2๋ฐ”์ดํŠธ BMP ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

String.fromCodePoint(0x1F303); // "๐ŸŒƒ"

Polyfill

IE์—์„œ๋Š” ์ „ํ˜€ ์ง€์›์ด ์•ˆ๋œ๋‹ค.

if(!String.fromCodePoint) (function(stringFromCharCode) {
  var fromCodePoint = function(_) {
    var codeUnits = [], codeLen = 0, result = '';
    for (var index = 0, len = arguments.length; index !== len; ++index) {
      var codePoint = +arguments[index];
      // `NaN`, `-Infinity`, `+Infinity` ํฌํ•จ๋œ ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ์ฒ˜๋ฆฌ
      // NaN ์ผ€์ด์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด `!(...)` ํ•„์š”
      // (codePoint>>0) === codePoint ์†Œ์ˆ˜์ ๊ณผ negatives ์ฒ˜๋ฆฌ
      if (!(codePoint < 0x10FFFF && (codePoint>>>0) === codePoint)) 
        throw RangeError("Invalid code point: " + codePoint);
      if (codePoint <= 0xFFFF) { // BMP code point
          codeLen = codeUnits.push(codePoint);
      } else { //  Astral code point; surrogate๋ฅผ ๋ถ„ํ™œ
      // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
          codePoint = codeUnits.push(
            (codePoint >> 10) + 0xD800, // high Surrogate
          (codePoint % 0x400) + 0xDC00 // low Surrogate
        )
      }
      if (codeLen >= 0x3fff) {
        result += stringFromCharCode.apply(null, codeUnits);
        codeUnits.length = 0;
      }
    }
    return result + stringFromCahrCode.apply(null, codeUnits);
  };
  try { // DOM element `Object.defineProperty` IE 8 ์„œํฌํŠธ
    Object.definePropery(String, "fromCodePoint", {
      "value": fromCodePoint, "configurable": true, "writable": true
    });
  } catch (e) {
    String.fromCodePoint = fromCodePoint;
  } 
}(String.fromCharCode));

Last updated

Was this helpful?