Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 6, 2014
0 parents commit 9930ad1
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
indent_style = space
indent_size = 2

[*.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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"immed": true,
"newcap": true,
"noarg": true,
"undef": true,
"unused": "vars",
"strict": true
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = function (val) {
val = String(val).trim();

if (/^(?:y|yes|true)$/i.test(val)) {
return true;
}

if (/^(?:n|no|false)$/i.test(val)) {
return false;
}

return null;
};
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

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.
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "yn",
"version": "0.0.0",
"description": "Parse yes/no like values",
"license": "MIT",
"repository": "sindresorhus/yn",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "node test.js"
},
"files": [
"index.js"
],
"keywords": [
"yn",
"yes",
"no",
"cli",
"prompt",
"validate",
"input",
"answer",
"true",
"false",
"parse"
],
"devDependencies": {
"ava": "0.0.3"
}
}
46 changes: 46 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# yn [![Build Status](https://travis-ci.org/sindresorhus/yn.svg?branch=master)](https://travis-ci.org/sindresorhus/yn)

> Parse yes/no like values
Useful for validating answers of a CLI prompt.

-

The following case-insensitive values are recognized:

```js
'y', 'yes', 'true', true, 'n', 'no', 'false', false
```


## Install

```sh
$ npm install --save yn
```


## Usage

```js
var yn = require('yn');

yn('y');
//=> true

yn('NO');
//=> false

yn(true);
//=> true

yn('unicorn');
//=> null
```

Unrecognized values returns `null`.


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
var test = require('ava');
var yn = require('./');

test(function (t) {
[
'y',
'Y',
'yes',
'YES',
'Yes',
'true',
true
].forEach(function (el) {
t.assert(yn(el) === true);
});

[
'n',
'N',
'no',
'NO',
'No',
'false',
false
].forEach(function (el) {
t.assert(yn(el) === false);
});

t.assert(yn(NaN) === null);
t.assert(yn('') === null);
t.assert(yn('unicorn') === null);
});

0 comments on commit 9930ad1

Please sign in to comment.