string-at

A robust & optimized ES3-compatible polyfill for the `String.prototype.at` proposal for ECMAScript 6/7 - based on https://mths.be/at

The original `String.prototype.at` proposal has been replaced by a new one; please use v1 or later of `string.prototype.at` instead
Last updated 10 years ago by ljharb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install string-at 
SYNC missed versions from official npm registry.

string-at Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

browser support

A robust & optimized ES3-compatible polyfill for the String.prototype.at proposal for ECMAScript 6/7.

This code is almost entirely copied from @mathiasbynens's excellent polyfill at https://mths.be/at - I created this one so that it did not automatically modify String.prototype, and so that it would fit in more neatly with the es7-shim.

Use it as a standalone function, or call its shim method to install it as a polyfill.

Example

var at = require('string-at');
var assert = require('assert');

var surrogatePair = '\uD834\uDF06 abc'; // a surrogate pair
assert(surrogatePair.length === 6);
assert(at(surrogatePair, 0) === '\uD834\uDF06');
assert(at(surrogatePair, 1) === '\uDF06');

var i = 0;
var str = '';
while (str.length < surrogatePair.length) {
	str += at(surrogatePair, str.length);
	i += 1;
}
assert(str === surrogatePair);
assert(i === 4); // 4 code points
assert(str.length === 6); // 6 "characters"

at.shim();
assert(surrogatePair.at(0) === at(surrogatePair, 0));

Tests

Simply clone the repo, npm install, and run npm test

Current Tags

  • 1.1.0                                ...           latest (5 years ago)

3 Versions

  • 1.1.0 [deprecated]           ...           5 years ago
  • 1.0.1 [deprecated]           ...           9 years ago
  • 1.0.0 [deprecated]           ...           10 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (10)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE