Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Feb 6, 2017
0 parents commit f22e05f
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dist: trusty
git:
depth: 1
branches:
except: /^v\d/
language: node_js
node_js: node
script: npm run-script pretest && npm run-script coverage
after_script:
- npm install istanbul-coveralls
- node node_modules/.bin/istanbul-coveralls
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 Shinnosuke Watanabe

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# glob-option-error

[![NPM version](https://img.shields.io/npm/v/glob-option-error.svg)](https://www.npmjs.com/package/glob-option-error)
[![Build Status](https://travis-ci.org/shinnn/glob-option-error.svg?branch=master)](https://travis-ci.org/shinnn/glob-option-error)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/glob-option-error.svg)](https://coveralls.io/r/shinnn/glob-option-error)

Create an error from the result of [validate-glob-opts](https://github.com/shinnn/validate-glob-opts)

```javascript
const GlobOptionError = require('glob-option-error');
const validateGlobOpts = require('validate-glob-opts');

new GlobOptionError(validateGlobOpts({
sync: true,
mark: '/',
caches: {}
}));
/* => Error: 3 errors found in the glob options:
1. `sync` option is deprecated and there’s no need to pass any values to that option, but true was provided.
2. node-glob expected `mark` option to be a Boolean value, but got '/'.
3. node-glob doesn't have `caches` option. Probably you meant `cache`.
at new GlobOptionError (/Users/me/exmaple/node_modules/glob-option-error/index.js:33:17)
at Object.<anonymous> (/Users/me/exmaple/app.js:2:13)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
...
*/
```

## Installation

[Use npm.](https://docs.npmjs.com/cli/install)

```
npm install glob-option-error
```

## API

```javascript
const GlobOptionError = require('glob-option-error');
```

### GlobOptionError(*array*)

*array*: `Array<error>` (return value of [validate-glob-opts](https://github.com/shinnn/validate-glob-opts#api))
Return: `Error`

The returned error has an [iterator](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols) that returns the individual errors one by one.

```javascript
const GlobOptionError = require('glob-option-error');
const validateGlobOpts = require('validate-glob-opts');

const results = validateGlobOpts({
root: Buffer.from('Hi'),
nodir: NaN,
ignore: ['path1', 1]
});
/*=> [
TypeError: node-glob expected `root` option to be a directory path (string), but got <Buffer 48 69>.,
TypeError: node-glob expected `nodir` option to be a Boolean value, but got NaN.,
TypeError: Expected every value in the `ignore` option to be a string, but the array includes a non-string value 1.
] */

const error = new GlobOptionError(results); //=> TypeError: 3 errors found in the glob options: ...

for (const {message} of error) {
console.log(message);
// node-glob expected `root` option to be a directory path (string), but got <Buffer 48 69>.
// node-glob expected `nodir` option to be a Boolean value, but got NaN.
// Expected every value in the `ignore` option to be a string, but the array includes a non-string value 1.
}
```

The argument must include at least one error. That means you should check the array length before passing it to `GlobOptionError`.

```javascript
new GlobOptionError([]);
// throws a RangeError: Expected an array with at least one error, but got [] (empty array).
```

## License

Copyright (c) 2017 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*!
* glob-option-error | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/glob-option-error
*/
'use strict';

const inspect = require('util').inspect;

const isTypeError = err => err.name === 'TypeError';
const createMessageLine = (msg, err, index) => `${msg}\n ${(index + 1)}. ${err.message}`;

module.exports = function GlobOptionError(arr) {
if (!Array.isArray(arr)) {
throw new TypeError(`Expected an array of errors, but got a non-array value ${
inspect(arr)
}.`);
}

const count = arr.length;

if (count === 0) {
throw new RangeError('Expected an array with at least one error, but got [] (empty array).');
}

if (count === 1) {
arr[0][Symbol.iterator] = function *() {
yield arr[0];
};

return arr[0];
}

const error = new (arr.every(isTypeError) ? TypeError : Error)(arr.reduce(
createMessageLine,
`${count} errors found in the glob options:`
));

error[Symbol.iterator] = function *() {
for (const result of arr) {
yield result;
}
};

return error;
};

39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "glob-option-error",
"version": "0.1.0",
"description": "Create an error from the result of validate-glob-opts",
"repository": "shinnn/glob-option-error",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --fix --format=codeframe index.js test.js",
"test": "node --throw-deprecation test.js",
"coverage": "istanbul cover --print=both test.js"
},
"license": "MIT",
"files": [
"index.js"
],
"keywords": [
"glob",
"option",
"options",
"validate",
"validation",
"invalidate",
"check",
"type",
"error",
"strict"
],
"devDependencies": {
"@shinnn/eslint-config-node": "^3.0.0",
"eslint": "^3.15.0",
"graceful-fs": "^4.1.11",
"istanbul": "^0.4.5",
"tape": "^4.6.3",
"validate-glob-opts": "^0.4.0"
},
"eslintConfig": {
"extends": "@shinnn/node"
}
}
64 changes: 64 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

const GlobOptionError = require('.');
const test = require('tape');
const validateGlobOpts = require('validate-glob-opts');

test('GlobOptionError', t => {
const error0 = new GlobOptionError(validateGlobOpts({realPath: true}));
t.strictEqual(
error0.toString(),
'Error: node-glob doesn\'t have `realPath` option. Probably you meant `realpath`.',
'should return an error if when it takes an array of errors.'
);

t.deepEqual(
[...error0].map(String),
['Error: node-glob doesn\'t have `realPath` option. Probably you meant `realpath`.'],
'should add [Symbol.iterator] property to the error.'
);

t.strictEqual(
new GlobOptionError(validateGlobOpts({
cache: Buffer.from('Hi'),
nomount: new Uint8Array()
})).toString(),
`TypeError: 2 errors found in the glob options:
1. node-glob expected \`nomount\` option to be a Boolean value, but got Uint8Array [ ].
2. node-glob expected \`cache\` option to be an object, but got <Buffer 48 69>.`,
'should merge multiple errors into single one.'
);

t.strictEqual(
[...new GlobOptionError(validateGlobOpts({
sync: null,
realpathCache: Infinity
}))].length,
2,
'should make the return value iterable.'
);

t.throws(
() => new GlobOptionError(validateGlobOpts({
matchBase: true,
realpath: true
})),
/^RangeError.*Expected an array with at least one error, but got \[] \(empty array\)\./,
'should throw an error when it takes an empty array.'
);

t.throws(
() => new GlobOptionError(Math.sign),
/^TypeError.*Expected an array of errors, but got a non-array value \[Function: sign]\./,
'should throw an error when it takes a non-array argument.'
);

t.throws(
() => new GlobOptionError(),
/^TypeError.*Expected an array of errors, but got a non-array value undefined\./,
'should throw an error when it takes no arguments.'
);

t.end();
});

0 comments on commit f22e05f

Please sign in to comment.