Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Jan 25, 2017
0 parents commit 30f31cf
Show file tree
Hide file tree
Showing 9 changed files with 231 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.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# assert-valid-glob-opts

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

Assert a given object is a valid [node-glob](https://github.com/isaacs/node-glob) option

```javascript
assertValidGlobOpts({
sync: true,
ignore: /node_modules/
});
```

```
TypeError: 2 errors found in the node-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 `ignore` option to be an array or string, but got /node_modules/.
at assertValidGlobOpts (/Users/shinnn/example/index.js:29:9)
at Object.<anonymous> (/Users/shinnn/example/app.js:2:1)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
```

## Installation

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

```
npm install assert-valid-glob-opts
```

## API

```javascript
const assertValidGlobOpts = require('assert-valid-glob-opts');
```

### assertValidGlobOpts(*obj*)

*obj*: `Object` ([`glob` options](https://github.com/isaacs/node-glob#options))

It validates a given object with [validate-glob-opts](https://github.com/shinnn/validate-glob-opts) and throws an error if the value is not valid.

```javascript
const assertValidGlobOpts = require('assert-valid-glob-opts');

const ok = {
mark: true,
matchBase: false
};

assertValidGlobOpts(ok); // doesn't throw

const notOk = {
mark: 'true',
matchbase: false
};

assertValidGlobOpts(notOk); // throws an error
```

## License

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

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

var validateGlobOpts = require('validate-glob-opts');

function isTypeError(err) {
return err.name === 'TypeError';
}

function createMessageLine(msg, err, index) {
return msg + '\n ' + (index + 1) + '. ' + err.message;
}

module.exports = function assertValidGlobOpts(obj) {
var results = validateGlobOpts(obj);
var count = results.length;

if (count === 0) {
return;
}

if (count === 1) {
throw results[0];
}

throw new (results.every(isTypeError) ? TypeError : Error)(results.reduce(
createMessageLine,
String(count) + ' errors found in the node-glob options:'
));
};
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "assert-valid-glob-opts",
"version": "0.1.0",
"description": "Assert a given object is a valid glob option",
"repository": "shinnn/assert-valid-glob-opts",
"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",
"validate",
"invalidate",
"check",
"type",
"error",
"throw",
"assert",
"assertion",
"strict"
],
"dependencies": {
"validate-glob-opts": "^0.2.0"
},
"devDependencies": {
"@shinnn/eslint-config-node-legacy": "^3.0.0",
"eslint": "^3.14.0",
"istanbul": "^0.4.5",
"tape": "^4.6.3"
},
"eslintConfig": {
"extends": "@shinnn/node-legacy"
}
}
37 changes: 37 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const main = require('.');
const test = require('tape');

test('assertValidGlobOpts()', t => {
t.doesNotThrow(
() => main(),
'should throw no errors when it takes no arguments.'
);

t.doesNotThrow(
() => main({nodir: true}),
'should throw no errors when it takes a valid glob option.'
);

t.throws(
() => main({cwd: 1}),
/^TypeError: node-glob expected `cwd` option to be a directory path \(string\), but got 1\./,
'should throw an error when it takes an invalid glob option.'
);

t.throws(
() => main({cache: {'1': 2}, realPath: true}),
/^Error: 2 errors found in the node-glob options:\n.* {2}1\. .*\n {2}2\. .*/,
'should throw an error with multiple lines when the object inludes multiple invalid values.'
);

t.throws(
() => main({follow: new Map([[true, false]]), noext: Infinity}),
/^TypeError/,
'should throw a type error when every error in the result is type error.'
);

t.end();
});

0 comments on commit 30f31cf

Please sign in to comment.