Skip to content

Commit

Permalink
Merge pull request #6 from es-shims/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Oct 15, 2024
2 parents c3b6231 + 524d34b commit 85730bb
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 28 deletions.
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"root": true,

"extends": "@ljharb",

"rules": {
"id-length": "off",
"new-cap": ["error", {
"capIsNewExceptions": [
"StringCharCodeAt",
"RequireObjectCoercible",
"ToIntegerOrInfinity",
"ToString",
],
}],
"no-magic-numbers": "off",
},

"overrides": [
{
"files": ["tests/**/*"],
"rules": {
"max-lines-per-function": "off",
"max-statements-per-line": "off",
},
},
]
}
11 changes: 11 additions & 0 deletions .github/workflows/node-aught.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: 'Tests: node.js < 10'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '< 10'
type: minors
command: npm run tests-only
7 changes: 7 additions & 0 deletions .github/workflows/node-pretest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'Tests: pretest/posttest'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/pretest.yml@main
11 changes: 11 additions & 0 deletions .github/workflows/node-tens.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: 'Tests: node.js >= 10'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 10'
type: minors
command: npm run tests-only
2 changes: 1 addition & 1 deletion .github/workflows/publish-on-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v3
with:
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/rebase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Automatic Rebase

on: [pull_request_target]

jobs:
_:
uses: ljharb/actions/.github/workflows/rebase.yml@main
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/require-allow-edits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Require “Allow Edits”

on: [pull_request_target]

jobs:
_:
name: "Require “Allow Edits”"

runs-on: ubuntu-latest

steps:
- uses: ljharb/require-allow-edits@main
File renamed without changes.
2 changes: 2 additions & 0 deletions auto.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/*! https://mths.be/codepointat v1.0.0 by @mathias */

'use strict';

require('./shim')();
16 changes: 8 additions & 8 deletions implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

'use strict';

var callBound = require('es-abstract/helpers/callBound');
var RequireObjectCoercible = require('es-abstract/2019/RequireObjectCoercible');
var ToString = require('es-abstract/2019/ToString');
var ToInteger = require('es-abstract/2019/ToInteger');
var callBound = require('call-bind/callBound');
var RequireObjectCoercible = require('es-abstract/2024/RequireObjectCoercible');
var ToString = require('es-abstract/2024/ToString');
var ToIntegerOrInfinity = require('es-abstract/2024/ToIntegerOrInfinity');
var StringCharCodeAt = callBound('String.prototype.charCodeAt');

module.exports = function codePointAt(position) {
var O = RequireObjectCoercible(this);
var string = ToString(O);
var size = string.length;
var index = ToInteger(position);
var index = ToIntegerOrInfinity(position);
// Account for out-of-bounds indices:
if (index < 0 || index >= size) {
return undefined;
Expand All @@ -21,13 +21,13 @@ module.exports = function codePointAt(position) {
var first = StringCharCodeAt(string, 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
first >= 0xD800 && first <= 0xDBFF // high surrogate
&& size > index + 1 // there is a next code unit
) {
second = StringCharCodeAt(string, 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 - 0xD800) * 0x400) + second - 0xDC00 + 0x10000;
}
}
return first;
Expand Down
32 changes: 22 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"exports": {
".": "./index.js",
"./auto": "./auto.js",
"./shim": "./shim.js",
"./getPolyfill": "./getPolyfill.js",
"./polyfill": "./polyfill.js",
"./implementation": "./implementation.js",
"./shim": "./shim.js",
"./package.json": "./package.json"
},
"keywords": [
Expand All @@ -30,20 +30,32 @@
},
"bugs": "https://github.com/mathiasbynens/String.prototype.codePointAt/issues",
"scripts": {
"pretest": "es-shim-api --bound",
"lint": "eslint --ext=js,mjs .",
"postlint": "es-shim-api --bound",
"pretest": "npm run lint",
"test": "npm run tests-only",
"tests-only": "tape 'tests/*.js'",
"cover": "istanbul cover --report html --verbose --dir coverage tape 'tests/*.js'"
"posttest": "npx npm@'>=10.2' audit --production"
},
"dependencies": {
"es-abstract": "^1.17.5"
"call-bind": "^1.0.7",
"es-abstract": "^1.23.3"
},
"devDependencies": {
"@es-shims/api": "^2.1.2",
"define-properties": "^1.1.3",
"function-bind": "^1.1.1",
"functions-have-names": "^1.2.1",
"@es-shims/api": "^2.5.1",
"@ljharb/eslint-config": "^21.1.1",
"define-properties": "^1.2.1",
"eslint": "=8.8.0",
"function-bind": "^1.1.2",
"functions-have-names": "^1.2.3",
"has-strict-mode": "^1.0.1",
"istanbul": "^0.4.5",
"tape": "^5.0.0"
"tape": "^5.9.0"
},
"directories": {
"test": "tests"
},
"engines": {
"node": ">= 0.4"
}
}
2 changes: 1 addition & 1 deletion shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module.exports = function shimCodePointAt() {
}

return polyfill;
}
};
4 changes: 2 additions & 2 deletions tests/shimmed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ codePointAt.shim();

var test = require('tape');
var defineProperties = require('define-properties');
var bind = require('function-bind');
var callBind = require('call-bind');
var isEnumerable = Object.prototype.propertyIsEnumerable;
var functionsHaveNames = require('functions-have-names')();

Expand All @@ -24,7 +24,7 @@ test('shimmed', function (t) {
et.end();
});

runTests(bind.call(Function.call, String.prototype.codePointAt), t);
runTests(callBind(String.prototype.codePointAt), t);

t.end();
});
12 changes: 6 additions & 6 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

module.exports = function (codePointAt, t) {
var supportsStrictMode = require('has-strict-mode')();

module.exports = function (codePointAt, t) {
t.test('String that starts with a BMP symbol', function (st) {
st.equal(codePointAt('abc\uD834\uDF06def', -1), undefined);
st.equal(codePointAt('abc\uD834\uDF06def', -0), 0x61);
Expand Down Expand Up @@ -82,21 +84,19 @@ module.exports = function (codePointAt, t) {
st.end();
});

var supportsStrictMode = (function () { return typeof this === 'undefined'; }());

t.test('bad string/this value', { skip: !supportsStrictMode }, function (st) {
st['throws'](function () { return codePointAt(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { return codePointAt(null, 'a'); }, TypeError, 'null is not an object');
st.end();
});

t.test('cast this value', function (st) {
st.equal(codePointAt(42, 0), 0x34);
st.equal(codePointAt(42, 1), 0x32);
st.equal(codePointAt({ 'toString': function() { return 'abc'; } }, 2), 0x63);
st.equal(codePointAt({ toString: function () { return 'abc'; } }, 2), 0x63);

var tmp = 0;
st.equal(codePointAt({ 'toString': function() { ++tmp; return String(tmp); } }, 0), 0x31);
st.equal(codePointAt({ toString: function () { tmp += 1; return String(tmp); } }, 0), 0x31);
st.equal(tmp, 1);

st.end();
Expand Down

0 comments on commit 85730bb

Please sign in to comment.