[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt [Back]  [Original]

String.prototype.codePointAt() - JavaScript | MDN

Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.

View in English Always switch to English

String.prototype.codePointAt()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since setembro de 2015.

O mtodo codePointAt() retorna um nmero inteiro no negativo que o valor do ponto de cdigo Unicode.

In this article

Experimente

const icons = "";

console.log(icons.codePointAt(1));
// Expected output: "9733"

Sintaxe

str.codePointAt(pos)

Parmetros

pos

A posio de um elemento em uma string a partir do qual retorna o valor do ponto de cdigo.

Valor retornado

Um nmero que representa o valor do ponto de cdigo do caractere na pos fornecida. Se no houver nenhum elemento na pos, undefined retornado.

Descrio

Se no houver nenhum elemento na posio especificada, retornado o valor de undefined. Se nenhum par substituto UTF-16 comear na pos, a unidade de cdigo na pos ser retornada.

Polyfill

O seguinte cdigo cria no objeto global String a funo codePointAt() conforme especificado em ECMAScript 2015 para navegadores sem suporte nativo:

js
/*! https://mths.be/codepointat v0.2.0 by @mathias */
if (!String.prototype.codePointAt) {
  (function () {
    "use strict"; // needed to support `apply`/`call` with `undefined`/`null`
    var defineProperty = (function () {
      // IE 8 only supports `Object.defineProperty` on DOM elements
      try {
        var object = {};
        var $defineProperty = Object.defineProperty;
        var result = $defineProperty(object, object, object) && $defineProperty;
      } catch (error) {}
      return result;
    })();
    var codePointAt = function (position) {
      if (this == null) {
        throw TypeError();
      }
      var string = String(this);
      var size = string.length;
      // `ToInteger`
      var index = position ? Number(position) : 0;
      if (index != index) {
        // better `isNaN`
        index = 0;
      }
      // Account for out-of-bounds indices:
      if (index < 0 || index >= size) {
        return undefined;
      }
      // Get the first code unit
      var first = string.charCodeAt(index);
      var second;
      if (
        // check if its the start of a surrogate pair
        first >= 0xd800 &&
        first <= 0xdbff && // high surrogate
        size > index + 1 // there is a next code unit
      ) {
        second = string.charCodeAt(index + 1);
        if (second >= 0xdc00 && second <= 0xdfff) {
          // low surrogate
          // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
          return (first - 0xd800) * 0x400 + second - 0xdc00 + 0x10000;
        }
      }
      return first;
    };
    if (defineProperty) {
      defineProperty(String.prototype, "codePointAt", {
        value: codePointAt,
        configurable: true,
        writable: true,
      });
    } else {
      String.prototype.codePointAt = codePointAt;
    }
  })();
}

Exemplos

Usando codePointAt()

js
"ABC".codePointAt(1); // retorna 66
"\uD800\uDC00".codePointAt(0); // retorna 65536

"XYZ".codePointAt(42); // retorna undefined

Criando um loop com codePointAt()

js
for (let codePoint of "\ud83d\udc0e\ud83d\udc71\u2764") {
  console.log(codePoint.codePointAt(0).toString(16));
}
// retorna '1f40e', '1f471', '2764'

Especificaes

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.codepointat

Compatibilidade com navegadores

Veja tambm


Web Proxy Viewer  |  New URL  |  Original Page