From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../global_objects/string/codepointat/index.html | 127 +++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/string/codepointat/index.html (limited to 'files/es/web/javascript/reference/global_objects/string/codepointat') diff --git a/files/es/web/javascript/reference/global_objects/string/codepointat/index.html b/files/es/web/javascript/reference/global_objects/string/codepointat/index.html new file mode 100644 index 0000000000..ae3fef3ec8 --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/string/codepointat/index.html @@ -0,0 +1,127 @@ +--- +title: String.prototype.codePointAt() +slug: Web/JavaScript/Referencia/Objetos_globales/String/codePointAt +translation_of: Web/JavaScript/Reference/Global_Objects/String/codePointAt +--- +
{{JSRef}}
+ +
 
+ +
El método codePointAt() devuelve un entero no negativo que equivale al valor Unicode code point del carácter.
+ +

Sintaxis

+ +
str.codePointAt(indice)
+ +

Parámetros

+ +
+
indice
+
Índice del carácter en la cadena del que se quiere obtener el valor del Unicode code point.
+
+ +

Valor de retorno

+ +

Un número que equivale al valor code point del carácter especificado en el índice de la cadena; devuelve {{jsxref("undefined")}} si no se encuentra carácter en la posición especifica.

+ +

Description

+ +

If there is no element at the specified position, {{jsxref("undefined")}} is returned. If no UTF-16 surrogate pair begins at pos, the code unit at pos is returned.

+ +

Examples

+ +

Using codePointAt()

+ +
'ABC'.codePointAt(1);          // 66
+'\uD800\uDC00'.codePointAt(0); // 65536
+
+'XYZ'.codePointAt(42); // undefined
+
+ +

Polyfill

+ +

The following extends Strings to include the codePointAt() function as specified in ECMAScript 2015 for browsers not supporting it natively.

+ +
/*! http://mths.be/codepointat v0.1.0 by @mathias */
+if (!String.prototype.codePointAt) {
+  (function() {
+    'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
+    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 it’s 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
+          // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
+          return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
+        }
+      }
+      return first;
+    };
+    if (Object.defineProperty) {
+      Object.defineProperty(String.prototype, 'codePointAt', {
+        'value': codePointAt,
+        'configurable': true,
+        'writable': true
+      });
+    } else {
+      String.prototype.codePointAt = codePointAt;
+    }
+  }());
+}
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-string.prototype.codepointat', 'String.prototype.codePointAt')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-string.prototype.codepointat', 'String.prototype.codePointAt')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.String.codePointAt")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf