From c97befe40e4fece100618f321347f1ba84fc2212 Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Thu, 21 May 2015 18:07:28 -0400 Subject: [PATCH] first commit --- .editorconfig | 22 +++++++++++++++ .gitattributes | 10 +++++++ .gitignore | 15 ++++++++++ .jshintrc | 18 ++++++++++++ .travis.yml | 11 ++++++++ .verb.md | 55 +++++++++++++++++++++++++++++++++++++ LICENSE | 21 ++++++++++++++ README.md | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 26 ++++++++++++++++++ package.json | 39 ++++++++++++++++++++++++++ test.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 359 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 .travis.yml create mode 100644 .verb.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1ff40e6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,22 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +end_of_line = lf +charset = utf-8 +indent_size = 2 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false +insert_final_newline = false + +[{,test/}{actual,fixtures}/**] +trim_trailing_whitespace = false +insert_final_newline = false + +[templates/**] +trim_trailing_whitespace = false +insert_final_newline = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..660957e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +# Enforce Unix newlines +* text eol=lf + +# binaries +*.ai binary +*.psd binary +*.jpg binary +*.gif binary +*.png binary +*.jpeg binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80a228c --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.DS_Store +*.sublime-* +_gh_pages +bower_components +node_modules +npm-debug.log +actual +test/actual +temp +tmp +TODO.md +vendor +.idea +benchmark +coverage diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..1a2f7b9 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,18 @@ +{ + "asi": false, + "boss": true, + "curly": true, + "eqeqeq": true, + "eqnull": true, + "esnext": true, + "immed": true, + "latedef": false, + "laxcomma": false, + "mocha": true, + "newcap": true, + "noarg": true, + "node": true, + "sub": true, + "undef": true, + "unused": true +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0fc9381 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +sudo: false +language: node_js +node_js: + - "0.10" + - "0.12" + - "0.13" + - "iojs" +matrix: + fast_finish: true + allow_failures: + - node_js: "0.13" diff --git a/.verb.md b/.verb.md new file mode 100644 index 0000000..e9f9205 --- /dev/null +++ b/.verb.md @@ -0,0 +1,55 @@ +# {%= name %} {%= badge("fury") %} + +> {%= description %} + +## Install +{%= include("install-npm", {save: true}) %} + +## Usage + +Returns true if a property is strictly `true` or its inverse is strictly `false`. The inverse of `a` is `noa`, the inverse of `b` is `nob`, and so on. + +```js +var isTrue = require('{%= name %}'); + +isTrue({a: true}, 'a'); +//=> true + +isTrue({noa: false}, 'a'); +//=> true +``` + +If a property and it's inverse both exist, both must evaluate to the same result, or `false` is returned. + +**Examples** + +```js +isTrue({noa: false, a: true}, 'a'); +//=> true + +isTrue({noa: false, a: false}, 'a'); +//=> false + +isTrue({noa: true, a: true}, 'a'); +//=> false +``` + +## Related projects +{%= related(['is-plain-object', 'isobject', 'is-primitive']) %} + +## Running tests +{%= include("tests") %} + +## Contributing +{%= include("contributing") %} + +## Author +{%= include("author") %} + +## License +{%= copyright() %} +{%= license() %} + +*** + +{%= include("footer") %} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..65f90ac --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015, Jon Schlinkert. + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..624d3e2 --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# is-true [![NPM version](https://badge.fury.io/js/is-true.svg)](http://badge.fury.io/js/is-true) + +> Returns `true` if the value of an object's property is strictly true, or it's inverse is false. + +## Install + +Install with [npm](https://www.npmjs.com/) + +```sh +$ npm i is-true --save +``` + +## Usage + +Returns true if a property is strictly `true` or its inverse is strictly `false`. The inverse of `a` is `noa`, the inverse of `b` is `nob`, and so on. + +```js +var isTrue = require('is-true'); + +isTrue({a: true}, 'a'); +//=> true + +isTrue({noa: false}, 'a'); +//=> true +``` + +If a property and it's inverse both exist, both must evaluate to the same result, or `false` is returned. + +**Examples** + +```js +isTrue({noa: false, a: true}, 'a'); +//=> true + +isTrue({noa: false, a: false}, 'a'); +//=> false + +isTrue({noa: true, a: true}, 'a'); +//=> false +``` + +## Related projects + +* [is-plain-object](https://github.com/jonschlinkert/is-plain-object): Returns true if an object was created by the `Object` constructor. +* [isobject](https://github.com/jonschlinkert/isobject): Returns true if the value is an object and not an array or null. +* [is-primitive](https://github.com/jonschlinkert/is-primitive): Returns `true` if the value is a primitive. + +## Running tests + +Install dev dependencies: + +```sh +$ npm i -d && npm test +``` + +## Contributing + +Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/is-true/issues/new) + +## Author + +**Jon Schlinkert** + ++ [github/jonschlinkert](https://github.com/jonschlinkert) ++ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) + +## License + +Copyright © 2015 Jon Schlinkert +Released under the MIT license. + +*** + +_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 21, 2015._ \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..d30f81a --- /dev/null +++ b/index.js @@ -0,0 +1,26 @@ +/*! + * is-true + * + * Copyright (c) 2015, Jon Schlinkert. + * Licensed under the MIT License. + */ + +'use strict'; + +var isObject = require('is-plain-object'); + +module.exports = function is(o, key) { + if (!isObject(o)) { + throw new TypeError('is-true expects the first argument to be an object.'); + } + if (typeof key !== 'string') { + throw new TypeError('is-true expects the second argument to be a string.'); + } + + var inverse = 'no' + key; + if (o.hasOwnProperty(key) && o.hasOwnProperty(inverse)) { + return o[key] === true && o[inverse] === false; + } + + return o[key] === true || o[inverse] === false; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..cf3a2cd --- /dev/null +++ b/package.json @@ -0,0 +1,39 @@ +{ + "name": "is-true", + "description": "Returns `true` if the value of an object's property is strictly true, or it's inverse is false.", + "version": "0.1.0", + "homepage": "https://github.com/jonschlinkert/is-true", + "author": "Jon Schlinkert (https://github.com/jonschlinkert)", + "repository": { + "type": "git", + "url": "https://github.com/jonschlinkert/is-true.git" + }, + "bugs": { + "url": "https://github.com/jonschlinkert/is-true/issues" + }, + "license": "MIT", + "files": [ + "index.js" + ], + "main": "index.js", + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "mocha" + }, + "dependencies": { + "is-plain-object": "^2.0.0" + }, + "devDependencies": { + "mocha": "*" + }, + "keywords": [ + "boolean", + "check", + "is", + "object", + "property", + "value" + ] +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..25da0d8 --- /dev/null +++ b/test.js @@ -0,0 +1,68 @@ +/*! + * is-true + * + * Copyright (c) 2015 Jon Schlinkert. + * Licensed under the MIT license. + */ + +'use strict'; + +/* deps:mocha */ +var assert = require('assert'); +var isTrue = require('./'); + +describe('isTrue', function () { + it('should return false if the value is not strictly true:', function () { + assert.equal(isTrue({a: false}, 'a'), false); + assert.equal(isTrue({a: 'false'}, 'a'), false); + assert.equal(isTrue({a: 'true'}, 'a'), false); + assert.equal(isTrue({a: 5}, 'a'), false); + assert.equal(isTrue({a: []}, 'a'), false); + }); + + it('should return true if the value is strictly true:', function () { + assert.equal(isTrue({a: true}, 'a'), true); + }); + + it('should return false if the inverse is not strictly false:', function () { + assert.equal(isTrue({noa: true}, 'a'), false); + assert.equal(isTrue({noa: 'false'}, 'a'), false); + assert.equal(isTrue({noa: 'true'}, 'a'), false); + assert.equal(isTrue({noa: 5}, 'a'), false); + assert.equal(isTrue({noa: []}, 'a'), false); + }); + + it('should return true if the inverse is strictly false:', function () { + assert.equal(isTrue({noa: false}, 'a'), true); + }); + + it('should return true if the value is true and the inverse is false:', function () { + assert.equal(isTrue({noa: false, a: true}, 'a'), true); + assert.equal(isTrue({noa: false, a: false}, 'a'), false); + assert.equal(isTrue({noa: true, a: true}, 'a'), false); + }); + + it('should throw an error when the first argument is not an object:', function () { + assert.throws(function () { + isTrue(null); + }, 'is-true expects the first argument to be an object.'); + + assert.throws(function () { + isTrue(''); + }, 'is-true expects the first argument to be an object.'); + }); + + it('should throw an error when the second argument is not a string:', function () { + assert.throws(function () { + isTrue({}); + }, 'is-true expects the second argument to be a string.'); + + assert.throws(function () { + isTrue({}, {}); + }, 'is-true expects the second argument to be a string.'); + + assert.throws(function () { + isTrue({}, null); + }, 'is-true expects the second argument to be a string.'); + }); +});