Skip to content

Commit

Permalink
improve TypeError messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Apr 28, 2018
1 parent 625a1ba commit 68a49e8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
'use strict';

var appendType = require('append-type');

/*!
* indexed-filter | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/indexed-filter
*/

function indexedFilter(arr, fn, thisObj) {
if (!Array.isArray(arr)) {
throw new TypeError(String(arr) + ' is not an array. Expected an array to be filtered.');
throw new TypeError('Expected an array to be filtered, but got a non-array value ' + appendType(arr) + '.');
}

if (typeof fn !== 'function') {
throw new TypeError(String(fn) + ' is not a function. Expected a filter function that returns Boolean value.');
throw new TypeError('Expected a filter function, but got a non-function value ' + appendType(fn) + '.');
}

var results = [];
Expand Down
6 changes: 4 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
* indexed-filter | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/indexed-filter
*/
import appendType from 'append-type';

export default function indexedFilter(arr, fn, thisObj) {
if (!Array.isArray(arr)) {
throw new TypeError(String(arr) + ' is not an array. Expected an array to be filtered.');
throw new TypeError('Expected an array to be filtered, but got a non-array value ' + appendType(arr) + '.');
}

if (typeof fn !== 'function') {
throw new TypeError(String(fn) + ' is not a function. Expected a filter function that returns Boolean value.');
throw new TypeError('Expected a filter function, but got a non-function value ' + appendType(fn) + '.');
}

var results = [];
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"client-side",
"browser"
],
"dependencies": {
"append-type": "^1.0.1"
},
"devDependencies": {
"@shinnn/eslint-config": "^5.4.0",
"eslint": "^4.19.1",
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ test('indexedFilter()', t => {

t.throws(
() => indexedFilter(1, () => {}),
/TypeError.*1 is not an array\. Expected an array to be filtered\./,
'should have a function name.'
/TypeError.*Expected an array to be filtered, but got a non-array value 1 \(number\)\./,
'should invalidate a non-array first argument.'
);

t.throws(
() => indexedFilter([], 1),
/TypeError.*1 is not a function\. Expected a filter function that returns Boolean value\./,
'should hadsfdfdfve a function name.'
/TypeError.*Expected a filter function, but got a non-function value 1 \(number\)\./,
'should invalidate a non-function second argument.'
);

t.end();
Expand Down

0 comments on commit 68a49e8

Please sign in to comment.