Skip to content

Commit

Permalink
validate argument length
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Jan 21, 2018
1 parent 492ffc6 commit 1284393
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/*!
* glob-observable | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/glob-observable
*/
'use strict';

const {inspect} = require('util');
Expand All @@ -13,14 +9,27 @@ const Glob = require('glob').Glob;
const {makeAbs} = require('glob/common.js');
const Observable = require('zen-observable');

module.exports = function globObservable(pattern, options) {
module.exports = function globObservable(...args) {
return new Observable(observer => {
const argLen = args.length;

if (argLen !== 1 && argLen !== 2) {
throw new RangeError(`Expected 1 or 2 arguments (<string>[, <Object>]), but got ${
argLen === 0 ? 'no' : argLen
} arguments.`);
}

const [pattern] = args;

if (typeof pattern !== 'string') {
throw new TypeError(`Expected a glob pattern string, but got ${inspect(pattern)}.`);
}

assertValidGlobOpts(options);
options = options || {};
if (argLen === 2) {
assertValidGlobOpts(args[1]);
}

const options = args[1] || {};

const realpath = options.realpath;
const unique = options.nounique !== true;
Expand Down
26 changes: 25 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const dummyFileStat = new Stats(1000000, 33188, 1, 501, 20, 0, 4096, 500000, 1,
const dummyFilePath = slash(resolve('__actually_this_file_does_not_exist__'));

test('globObservable()', async t => {
t.plan(13);
t.plan(15);

await rmfr('tmp');
await Promise.all([
Expand Down Expand Up @@ -186,4 +186,28 @@ test('globObservable()', async t => {
},
complete: unexpectedComplete
});

globObservable()
.subscribe({
error({message}) {
t.equal(
message,
'Expected 1 or 2 arguments (<string>[, <Object>]), but got no arguments.',
'should fail when it takes no arguments.'
);
},
complete: unexpectedComplete
});

globObservable('', {}, '')
.subscribe({
error({message}) {
t.equal(
message,
'Expected 1 or 2 arguments (<string>[, <Object>]), but got 3 arguments.',
'should fail when it takes too many arguments.'
);
},
complete: unexpectedComplete
});
});

0 comments on commit 1284393

Please sign in to comment.