Skip to content

Commit

Permalink
added check.oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Mar 16, 2015
1 parent bf1ee11 commit e566ab6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# check-more-types v1.3.0
# check-more-types v1.4.0

> Additional type checks for [check-types.js](https://github.com/philbooth/check-types.js)
Expand Down Expand Up @@ -54,6 +54,7 @@ for advice and examples.
* [check.commitId](#checkcommitid)
* [check.shortCommitId](#checkshortcommitid)
* [check.index](#checkindex)
* [check.oneOf](#checkoneof)
* [check.same](#checksame)
* [check.sameLength](#checksamelength)
* [check.allSame](#checkallsame)
Expand Down Expand Up @@ -133,6 +134,15 @@ for advice and examples.

---

#### check.oneOf

var colors = ['red', 'green', 'blue'];
var color = 'green';
check.oneOf(colors, color)); // true
check.oneOf(colors, 'brown')); // false

---

#### check.same

var foo = {},
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "check-more-types",
"main": "check-more-types.js",
"version": "1.3.0",
"version": "1.4.0",
"homepage": "https://github.com/kensho/check-more-types",
"license": "MIT",
"ignore": [
Expand Down
13 changes: 12 additions & 1 deletion check-more-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
});
}

/**
Returns true if given item is in the array
@method oneOf
*/
function oneOf(arr, x) {
check.verify.array(arr, 'expected an array');
return arr.indexOf(x) !== -1;
}

/**
Returns true for urls of the format `git@....git`
Expand Down Expand Up @@ -471,7 +481,8 @@
index: index,
git: git,
arrayOf: arrayOf,
badItems: badItems
badItems: badItems,
oneOf: oneOf
};

Object.keys(predicates).forEach(function (name) {
Expand Down
4 changes: 2 additions & 2 deletions 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@

---

### check.oneOf

var colors = ['red', 'green', 'blue'];
var color = 'green';
check.oneOf(colors, color)); // true
check.oneOf(colors, 'brown')); // false

---

### check.same

var foo = {},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "check-more-types",
"description": "Additional type checks for https://github.com/philbooth/check-types.js",
"version": "1.3.0",
"version": "1.4.0",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": {
"url": "https://github.com/kensho/check-more-types/issues"
Expand Down
9 changes: 9 additions & 0 deletions test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ describe('check-more-types', function () {
la(check.fn(check.lowerCase));
});

describe('check/oneOf', function () {
it('validates color', function () {
var colors = ['red', 'green', 'blue'];
var color = 'green';
la(check.oneOf(colors, color), color, 'is one of', colors);
la(!check.oneOf(colors, 'brown'));
});
});

describe('check/arrayOf', function () {
it('validates array of strings', function () {
la(check.arrayOf(check.string, ['foo', '']));
Expand Down

0 comments on commit e566ab6

Please sign in to comment.