Skip to content

Commit

Permalink
Merge pull request #78 from shgysk8zer0/feature/error-isError
Browse files Browse the repository at this point in the history
Add `Error.isError` & fix `arr.uniqueBy`
  • Loading branch information
shgysk8zer0 authored Oct 9, 2024
2 parents 3cea746 + 4c78e1e commit 2f1246e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.4.4] - 2024-10-09

### Added
- Add `Error.isError` (imperfect, but works)

### Fixed
- Fixed and simplicy `Array.prototype.uniqueBy`

## [v0.4.3] - 2024-09-17

### Added
Expand Down
47 changes: 19 additions & 28 deletions array.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,41 +190,32 @@ if (! (Array.prototype.equals instanceof Function)) {
*/
if (! (Array.prototype.uniqueBy instanceof Function)) {
Array.prototype.uniqueBy = function uniqueBy(arg) {
if (typeof arg === 'undefined') {
return [...new Set(this)];
} else if (typeof arg === 'string') {
const found = [];

return this.filter(obj => {
const key = obj[arg];
if (found.includes(key)) {
return false;
} else {
found.push(key);
return true;
}
});
} else if (arg instanceof Function) {
const found = [];
switch (typeof arg) {
case 'undefined':
return [...new Set(this)];

return this.filter((...args) => {
try {
case 'string':
case 'number':
case 'symbol':
return this.uniqueBy(entry => entry[arg]);

case 'function': {
const found = new Set(); //Cannot use `WeakSet` in case of primative values

return this.filter((...args) => {
const key = arg.apply(this, args);

if (typeof key !== 'string') {
return false;
} else if (found.includes(key)) {
if (found.has(key)) {
return false;
} else {
found.push(key);
found.add(key);
return true;
}
} catch {
return false;
}
});
} else {
throw new TypeError('Not a valid argument for uniqueBy');
});
}

default:
throw new TypeError('Not a valid argument for uniqueBy');
}
};
}
Expand Down
10 changes: 10 additions & 0 deletions errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ if (! (globalThis.reportError instanceof Function)) {
globalThis.dispatchEvent(errorToEvent(error));
};
}


if (! (Error.isError instanceof Function)) {
// @see https://github.com/tc39/proposal-is-error/blob/main/polyfill.js
const toStr = Function.bind.call(Function.call, Object.prototype.toString);

Error.isError = function isError(arg) {
return arg instanceof Error || (typeof arg === 'object' && toStr(arg) === '[object Error]' && arg[Symbol.toStringTag] === undefined);
};
}
10 changes: 2 additions & 8 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { ignoreFile } from '@shgysk8zer0/eslint-config/ignoreFile.js';
import browser from '@shgysk8zer0/eslint-config/browser.js';
import { browser } from '@shgysk8zer0/eslint-config';

export default [
ignoreFile,
browser({
ignores: ['**/*.min.js', '**/*.cjs', 'assets/dedent.js','assets/url-pattern.js'],
}),
];
export default browser({ ignores: ['**/*.min.js', '**/*.cjs', 'assets/dedent.js','assets/url-pattern.js'] });
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shgysk8zer0/polyfills",
"version": "0.4.3",
"version": "0.4.4",
"private": false,
"type": "module",
"description": "A collection of JavaScript polyfills",
Expand Down

0 comments on commit 2f1246e

Please sign in to comment.