Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array findlast and findlastindex #35

Merged
merged 4 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13,223 changes: 4,825 additions & 8,398 deletions package-lock.json

Large diffs are not rendered by default.

101 changes: 51 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
{
"name": "@github/browser-support",
"version": "1.2.2",
"description": "Polyfills and Capable Browser detection",
"homepage": "https://github.github.io/browser-support",
"bugs": {
"url": "https://github.com/github/browser-support/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/github/browser-support.git"
},
"license": "MIT",
"author": "GitHub Inc.",
"contributors": [
"Keith Cirkel (https://keithcirkel.co.uk/)",
"Kristján Oddsson <[email protected]>"
],
"type": "module",
"main": "lib/index.js",
"module": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"build": "tsc --build",
"lint": "eslint . --ignore-path .gitignore",
"pretest": "npm run build",
"test": "npm run lint && karma start test/karma.config.cjs",
"prepack": "npm run build",
"postpublish": "npm publish --ignore-scripts --@github:registry='https://npm.pkg.github.com'"
},
"prettier": "@github/prettier-config",
"devDependencies": {
"@github/prettier-config": "^0.0.4",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"chai": "^4.3.0",
"chromium": "^3.0.3",
"eslint": "^8.8.0",
"eslint-plugin-github": "^4.1.1",
"karma": "^6.1.1",
"karma-chai": "^0.1.0",
"karma-chai-spies": "^0.1.4",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"mocha": "^9.2.0",
"tslib": "^2.1.0",
"typescript": "^4.7.4"
}
"name": "@github/browser-support",
"version": "1.2.2",
"description": "Polyfills and Capable Browser detection",
"homepage": "https://github.github.io/browser-support",
"bugs": {
"url": "https://github.com/github/browser-support/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/github/browser-support.git"
},
"license": "MIT",
"author": "GitHub Inc.",
"contributors": [
"Keith Cirkel (https://keithcirkel.co.uk/)",
"Kristj\u00e1n Oddsson <[email protected]>"
],
"type": "module",
"main": "lib/index.js",
"module": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"build": "tsc --build",
"lint": "eslint . --ignore-path .gitignore",
"pretest": "npm run build",
"test": "npm run lint && karma start test/karma.config.cjs",
"prepack": "npm run build",
"postpublish": "npm publish --ignore-scripts --@github:registry='https://npm.pkg.github.com'"
},
"prettier": "@github/prettier-config",
"devDependencies": {
"@github/prettier-config": "^0.0.4",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"chai": "^4.3.0",
"chromium": "^3.0.3",
"eslint": "^8.8.0",
"eslint-plugin-github": "^4.1.1",
"karma": "^6.1.1",
"karma-chai": "^0.1.0",
"karma-chai-spies": "^0.1.4",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"mocha": "^9.2.0",
"tslib": "^2.1.0",
"typescript": "^5.1.3"
}
}
8 changes: 1 addition & 7 deletions src/abortsignal-abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ declare global {

/*#__PURE__*/
export function isSupported(): boolean {
return (
'abort' in AbortSignal &&
// @ts-expect-error `.abort`
typeof AbortSignal.abort === 'function'
)
return 'abort' in AbortSignal && typeof AbortSignal.abort === 'function'
}

/*#__PURE__*/
export function isPolyfilled(): boolean {
// @ts-expect-error `.abort`
return AbortSignal.abort === abortSignalAbort
}

export function apply(): void {
if (!isSupported()) {
// @ts-expect-error `.abort`
AbortSignal.abort = abortSignalAbort
}
}
8 changes: 1 addition & 7 deletions src/abortsignal-timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ declare global {

/*#__PURE__*/
export function isSupported(): boolean {
return (
'abort' in AbortSignal &&
// @ts-expect-error `.timeout`
typeof AbortSignal.timeout === 'function'
)
return 'abort' in AbortSignal && typeof AbortSignal.timeout === 'function'
}

/*#__PURE__*/
export function isPolyfilled(): boolean {
// @ts-expect-error `.timeout`
return AbortSignal.timeout === abortSignalTimeout
}

export function apply(): void {
if (!isSupported()) {
// @ts-expect-error `.timeout`
AbortSignal.timeout = abortSignalTimeout
}
}
26 changes: 26 additions & 0 deletions src/array-findlast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function arrayFindLast<T>(
this: T[],
pred: (this: T[], value: T, i: number, array: T[]) => boolean,
recv = this
): T | void {
for (let i = this.length; i > 0; i -= 1) {
if (pred.call(recv, this[i], i, this)) return this[i]
}
}

/*#__PURE__*/
export function isSupported(): boolean {
return 'findLast' in Array.prototype && typeof Array.prototype.findLast === 'function'
}

/*#__PURE__*/
export function isPolyfilled(): boolean {
return Array.prototype.findLast === arrayFindLast
}

export function apply(): void {
if (!isSupported()) {
const defn = {value: arrayFindLast, writable: true, configurable: true}
Object.defineProperty(Array.prototype, 'findLast', defn)
}
}
31 changes: 31 additions & 0 deletions src/array-findlastindex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function arrayFindLastIndex<T>(
this: T[],
pred: (this: T[], value: T, i: number, array: T[]) => boolean,
recv = this
): number {
for (let i = this.length; i > 0; i -= 1) {
if (pred.call(recv, this[i], i, this)) return i
}
return -1
}

/*#__PURE__*/
export function isSupported(): boolean {
return 'findLastIndex' in Array.prototype && typeof Array.prototype.findLastIndex === 'function'
}

/*#__PURE__*/
export function isPolyfilled(): boolean {
return Array.prototype.findLastIndex === arrayFindLastIndex
}

export function apply(): void {
if (!isSupported()) {
const defn = {
value: arrayFindLastIndex,
writable: true,
configurable: true
}
Object.defineProperty(Array.prototype, 'findLastIndex', defn)
}
}
4 changes: 2 additions & 2 deletions src/crypto-randomuuid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function randomUUID() {
export function randomUUID(): `${string}-${string}-${string}-${string}-${string}` {
const buf = new Uint32Array(4)
crypto.getRandomValues(buf)
let idx = -1
Expand All @@ -7,7 +7,7 @@ export function randomUUID() {
const r = (buf[idx >> 3] >> ((idx % 8) * 4)) & 15
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}) as `${string}-${string}-${string}-${string}-${string}`
}

declare global {
Expand Down
29 changes: 29 additions & 0 deletions test/array-findlast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {apply, arrayFindLast, isPolyfilled, isSupported} from '../lib/array-findlast.js'

describe('arrayFindLast', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})

it('returns value that passes truthy', () => {
expect(arrayFindLast.call([1, 2, 3], v => v === 3)).to.equal(3)
const arr = [1, 2, 3]
const recv = {}
expect(
arrayFindLast.call(
arr,
function (v, i, _arr) {
// eslint-disable-next-line @typescript-eslint/no-invalid-this
expect(this).to.equal(recv)
expect(_arr).to.equal(arr)
expect(v).to.equal(arr[i])
},
recv
)
)
})
})
29 changes: 29 additions & 0 deletions test/array-findlastindex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {apply, arrayFindLastIndex, isPolyfilled, isSupported} from '../lib/array-findlastindex.js'

describe('arrayFindLastIndex', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})

it('returns value that passes truthy', () => {
expect(arrayFindLastIndex.call([1, 2, 3], v => v === 3)).to.equal(2)
const arr = [1, 2, 3]
const recv = {}
expect(
arrayFindLastIndex.call(
arr,
function (v, i, _arr) {
// eslint-disable-next-line @typescript-eslint/no-invalid-this
expect(this).to.equal(recv)
expect(_arr).to.equal(arr)
expect(v).to.equal(arr[i])
},
recv
)
)
})
})
32 changes: 16 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"include": ["src"],
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"lib": ["es2021", "dom"],
"module": "ESNext",
"moduleResolution": "node",
"noEmit": false,
"outDir": "./lib",
"sourceMap": true,
"strict": true,
"target": "ES2020"
}
"include": ["src"],
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"lib": ["es2023", "dom"],
"module": "ESNext",
"moduleResolution": "node",
"noEmit": false,
"outDir": "./lib",
"sourceMap": true,
"strict": true,
"target": "ES2020"
}
}