Sha256: e7c6c272427e6ce85aad832a1fa40f16dd13feb5c51dd16f3771f0bda2e88ce0

Contents?: true

Size: 1.05 KB

Versions: 47

Compression:

Stored size: 1.05 KB

Contents

// Based on: https://github.com/mathiasbynens/String.prototype.at
// Thanks @mathiasbynens !

"use strict";

var toInteger  = require("../../number/to-integer")
  , validValue = require("../../object/valid-value");

module.exports = function (pos) {
	var str = String(validValue(this)), size = str.length, cuFirst, cuSecond, nextPos, len;
	pos = toInteger(pos);

	// Account for out-of-bounds indices
	// The odd lower bound is because the ToInteger operation is
	// going to round `n` to `0` for `-1 < n <= 0`.
	if (pos <= -1 || pos >= size) return "";

	// Second half of `ToInteger`
	// eslint-disable-next-line no-bitwise
	pos |= 0;
	// Get the first code unit and code unit value
	cuFirst = str.charCodeAt(pos);
	nextPos = pos + 1;
	len = 1;
	if (
		// Check if it’s the start of a surrogate pair
		cuFirst >= 0xd800 &&
		cuFirst <= 0xdbff && // High surrogate
		size > nextPos // There is a next code unit
	) {
		cuSecond = str.charCodeAt(nextPos);
		if (cuSecond >= 0xdc00 && cuSecond <= 0xdfff) len = 2; // Low surrogate
	}
	return str.slice(pos, pos + len);
};

Version data entries

47 entries across 47 versions & 3 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/es5-ext/string/#/at.js
govuk_publishing_components-18.0.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.21.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.20.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.19.1 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.19.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.18.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.17.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.16.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.15.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.14.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.13.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.12.2 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.12.1 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.12.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.11.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.10.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.9.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.8.0 node_modules/es5-ext/string/#/at.js
govuk_publishing_components-17.7.0 node_modules/es5-ext/string/#/at.js