From a1915ae8a9a4a633d18630102264d266b6e08f08 Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Mon, 31 Aug 2015 01:48:44 -0400 Subject: [PATCH] first commit --- .editorconfig | 22 +++++++++++++++++++ .gitattributes | 10 +++++++++ .gitignore | 15 +++++++++++++ .jshintrc | 18 ++++++++++++++++ .travis.yml | 11 ++++++++++ .verb.md | 37 ++++++++++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++++ index.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 31 +++++++++++++++++++++++++++ test.js | 37 ++++++++++++++++++++++++++++++++ 10 files changed, 259 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 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..3a77c56 --- /dev/null +++ b/.verb.md @@ -0,0 +1,37 @@ +# {%= name %} {%= badge("fury") %} + +> {%= description %} + +## Install +{%= include("install-npm", {save: true}) %} + +## Usage + +```js +var isDataDescriptor = require('{%= name %}'); +``` + +## API + +{%%= apidocs("index.js") %} + +## Related projects + +{%%= related([]) %} + +## 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/index.js b/index.js new file mode 100644 index 0000000..1d81039 --- /dev/null +++ b/index.js @@ -0,0 +1,57 @@ +/*! + * is-data-descriptor + * + * Copyright (c) 2015, Jon Schlinkert. + * Licensed under the MIT License. + */ + +'use strict'; + +var diff = require('arr-diff'); +var typeOf = require('kind-of'); + +// data descriptor properties +var data = { + configurable: 'boolean', + enumerable: 'boolean', + writable: 'boolean' +}; + +function isDataDescriptor(obj) { + if (typeOf(obj) !== 'object') { + return false; + } + + var dataKeys = Object.keys(data); + var keys = getKeys(obj); + + if (obj.hasOwnProperty('value')) { + if (diff(keys, dataKeys).length !== 1) { + return false; + } + for (var key in obj) { + if (key === 'value') continue; + if (typeOf(obj[key]) !== data[key]) { + return false; + } + } + } + return true; +} + +/** + * Get object keys. `Object.keys()` only gets + * enumerable properties. + */ + +function getKeys(obj) { + var keys = []; + for (var key in obj) keys.push(key); + return keys; +} + +/** + * Expose `isDataDescriptor` + */ + +module.exports = isDataDescriptor; diff --git a/package.json b/package.json new file mode 100644 index 0000000..ecdd1cf --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "is-data-descriptor", + "description": "Returns true if a value appears to be a valid JavaScript data descriptor.", + "version": "0.1.0", + "homepage": "https://github.com/jonschlinkert/is-data-descriptor", + "author": "Jon Schlinkert (https://github.com/jonschlinkert)", + "repository": "jonschlinkert/is-data-descriptor", + "bugs": { + "url": "https://github.com/jonschlinkert/is-data-descriptor/issues" + }, + "license": "MIT", + "files": [ + "index.js" + ], + "main": "index.js", + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "mocha" + }, + "dependencies": { + "arr-diff": "^1.1.0", + "kind-of": "^2.0.1" + }, + "devDependencies": { + "mocha": "*", + "should": "*" + }, + "keywords": [] +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..cbf7779 --- /dev/null +++ b/test.js @@ -0,0 +1,37 @@ +'use strict'; + +/* deps: mocha */ +var assert = require('assert'); +var should = require('should'); +var isDescriptor = require('./'); +var noop = function(){}; + +describe('isDescriptor', function () { + describe('value type', function () { + it('should be false when not an object:', function () { + assert(!isDescriptor('a')); + assert(!isDescriptor(null)); + assert(!isDescriptor([])); + }); + }); + + describe('data descriptor:', function () { + it('should be false when the object has invalid properties:', function () { + assert(!isDescriptor({value: 'foo', bar: 'baz'})); + assert(!isDescriptor({value: 'foo', bar: 'baz'})); + assert(!isDescriptor({value: 'foo', get: noop})); + assert(!isDescriptor({get: noop, value: noop})); + }); + + it('should be true when the object has valid properties:', function () { + assert(isDescriptor({value: 'foo'})); + assert(isDescriptor({value: noop})); + }); + + it('should be false when a value is not the correct type:', function () { + assert(!isDescriptor({value: 'foo', enumerable: 'foo'})); + assert(!isDescriptor({value: 'foo', configurable: 'foo'})); + assert(!isDescriptor({value: 'foo', writable: 'foo'})); + }); + }); +});