diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..2379838 --- /dev/null +++ b/.eslintrc @@ -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", + }, + }, + ] +} diff --git a/.github/workflows/node-aught.yml b/.github/workflows/node-aught.yml new file mode 100644 index 0000000..4213896 --- /dev/null +++ b/.github/workflows/node-aught.yml @@ -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 diff --git a/.github/workflows/node-pretest.yml b/.github/workflows/node-pretest.yml new file mode 100644 index 0000000..765edf7 --- /dev/null +++ b/.github/workflows/node-pretest.yml @@ -0,0 +1,7 @@ +name: 'Tests: pretest/posttest' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/pretest.yml@main diff --git a/.github/workflows/node-tens.yml b/.github/workflows/node-tens.yml new file mode 100644 index 0000000..1f55be0 --- /dev/null +++ b/.github/workflows/node-tens.yml @@ -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 diff --git a/.github/workflows/publish-on-tag.yml b/.github/workflows/publish-on-tag.yml index ddb8304..f557404 100644 --- a/.github/workflows/publish-on-tag.yml +++ b/.github/workflows/publish-on-tag.yml @@ -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: diff --git a/.github/workflows/rebase.yml b/.github/workflows/rebase.yml new file mode 100644 index 0000000..b9e1712 --- /dev/null +++ b/.github/workflows/rebase.yml @@ -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 }} diff --git a/.github/workflows/require-allow-edits.yml b/.github/workflows/require-allow-edits.yml new file mode 100644 index 0000000..7b842f8 --- /dev/null +++ b/.github/workflows/require-allow-edits.yml @@ -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 diff --git a/LICENSE-MIT.txt b/LICENSE similarity index 100% rename from LICENSE-MIT.txt rename to LICENSE diff --git a/auto.js b/auto.js index bc90109..9b30243 100644 --- a/auto.js +++ b/auto.js @@ -1,3 +1,5 @@ /*! https://mths.be/codepointat v1.0.0 by @mathias */ +'use strict'; + require('./shim')(); diff --git a/implementation.js b/implementation.js index 34f96df..74d93cf 100644 --- a/implementation.js +++ b/implementation.js @@ -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; @@ -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; diff --git a/package.json b/package.json index 150489e..34343de 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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" } } diff --git a/shim.js b/shim.js index c279551..87b69db 100644 --- a/shim.js +++ b/shim.js @@ -14,4 +14,4 @@ module.exports = function shimCodePointAt() { } return polyfill; -} +}; diff --git a/tests/shimmed.js b/tests/shimmed.js index 426fbf7..fffec72 100644 --- a/tests/shimmed.js +++ b/tests/shimmed.js @@ -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')(); @@ -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(); }); diff --git a/tests/tests.js b/tests/tests.js index 13e1a29..80d1dea 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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); @@ -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();