Skip to content

Commit

Permalink
Merge pull request #35 from keithamus/add-array-findlast-and-findlast…
Browse files Browse the repository at this point in the history
…index

Add array findlast and findlastindex
  • Loading branch information
dgreif authored Jun 24, 2023
2 parents ea995cc + 1018b54 commit be57273
Show file tree
Hide file tree
Showing 11 changed files with 5,016 additions and 8,481 deletions.
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
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import * as promiseAllSettled from './promise-allsettled.js'
import * as promiseAny from './promise-any.js'
import * as requestIdleCallback from './requestidlecallback.js'
import * as stringReplaceAll from './string-replaceall.js'
import * as arrayFindLast from './array-findlast.js'
import * as arrayFindLastIndex from './array-findlastindex.js'

export const baseSupport =
typeof Blob === 'function' &&
Expand Down Expand Up @@ -70,7 +72,9 @@ export const polyfills = {
promiseAllSettled,
promiseAny,
requestIdleCallback,
stringReplaceAll
stringReplaceAll,
arrayFindLast,
arrayFindLastIndex
}

export function isSupported() {
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"
}
}

0 comments on commit be57273

Please sign in to comment.