Skip to content

Commit

Permalink
feat(api): Added check.found method, closes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 17, 2015
1 parent fcc9151 commit 8e629e5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,7 @@ for advice and examples.
* [check.defend in module pattern](#checkdefend-in-module-pattern)
* [Safe callback execution](#safe-callback-execution)
* [check.then](#checkthen)
* [check.found](#checkfound)
* [check.regexp](#checkregexp)
* [check.promise](#checkpromise)
* [check.validDate](#checkvaliddate)
Expand Down Expand Up @@ -670,6 +671,15 @@ exception. For these cases, there is `check.then`

----

#### check.found

Great for quickly checking string or array search results

```js
check.found('foo'.indexOf('f')); // true
check.found('foo bar'.indexOf('bar')); // true
```

#### check.regexp

Returns true if the passed value is a regular expression.
Expand Down
10 changes: 10 additions & 0 deletions check-more-types.js
Expand Up @@ -78,6 +78,15 @@
return (Array.isArray(x) || isString(x)) && x.length === k;
}

/**
Checks if the given index is valid in an array or string or -1
@method found
*/
function found(index) {
return index >= 0;
}

function startsWith(prefix, x) {
return isString(prefix) &&
isString(x) &&
Expand Down Expand Up @@ -741,6 +750,7 @@
schema: schema,
raises: raises,
empty: empty,
found: found,
emptyString: emptyString,
unempty: unempty,
unit: unit,
Expand Down
2 changes: 1 addition & 1 deletion check-more-types.min.js

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

9 changes: 9 additions & 0 deletions docs/use.md
Expand Up @@ -564,6 +564,15 @@ exception. For these cases, there is `check.then`

----

### check.found

Great for quickly checking string or array search results

```js
check.found('foo'.indexOf('f')); // true
check.found('foo bar'.indexOf('bar')); // true
```

### check.regexp

Returns true if the passed value is a regular expression.
Expand Down
14 changes: 14 additions & 0 deletions test/unit-tests.js
Expand Up @@ -202,6 +202,20 @@ describe('check-more-types', function () {
});
});

describe('check/found', function () {
it('returns true for found indices', function () {
la(check.found('foo'.indexOf('f')));
la(check.found('foo'.indexOf('oo')));
la(check.found('foo bar'.indexOf('bar')));
});

it('fails for negative numbers', function () {
la(!check.found('foo'.indexOf('a')));
la(!check.found(-1), '-1');
la(!check.found(-2), '-2');
});
});

describe('check/regexp', function () {
it('passes regular expressions', function () {
la(check.regexp(/foo/));
Expand Down

0 comments on commit 8e629e5

Please sign in to comment.