-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from keithamus/add-array-findlast-and-findlast…
…index Add array findlast and findlastindex
- Loading branch information
Showing
11 changed files
with
5,016 additions
and
8,481 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |