Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Apr 14, 2016
1 parent 5d1db40 commit 5e04294
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

# Set default charset
[*.js]
indent_style = space
indent_size = 2

[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.idea
npm-debug.log*

/node_modules
/test/coverage
15 changes: 15 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"strict": true,
"node": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"laxbreak": true,
"latedef": "nofunc"
}
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea
.DS_Store
.editorconfig
.gitignore
.npmignore
.jshintrc
.travis.yml

npm-debug.log

/node_modules
/test
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sudo: false
language: node_js
node_js:
- 0.12

cache:
directories:
- node_modules

install: npm install

script:
- npm test

after_script:
- npm run test:rpt
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
# pick-prop
Randomly sampling a property from an object.

> Randomly sampling a property from an object.
[![MIT License](https://img.shields.io/badge/license-MIT_License-green.svg?style=flat-square)](https://github.com/bubkoo/pick-prop/blob/master/LICENSE)

[![build:?](https://img.shields.io/travis/bubkoo/pick-prop/master.svg?style=flat-square)](https://travis-ci.org/bubkoo/pick-prop)
[![coverage:?](https://img.shields.io/coveralls/bubkoo/pick-prop/master.svg?style=flat-square)](https://coveralls.io/github/bubkoo/pick-prop)



## Install

```
$ npm install --save pick-prop
```



## Usage

> For more use-cases see the [tests](https://github.com/bubkoo/pick-prop/blob/master/test/spec/index.js)
```js
var pickProp = require('pick-prop');

// pickProp(object);

pickProp({ a: 1, b: 2, c: 3 }); // => 3
pickProp([2]); // => 2

pickProp(); // => undefined
pickProp(null); // => undefined
pickProp([]); // => undefined
pickProp({}); // => undefined
pickProp(1); // => undefined
pickProp('a'); // => undefined
```

## Related

- [pick-props](https://github.com/bubkoo/pick-props) - Randomly sampling some properties from an object.
- [pick-key](https://github.com/bubkoo/pick-key) - Randomly sampling a key from an object.
- [pick-keys](https://github.com/bubkoo/pick-keys) - Randomly sampling some keys from an object.
- [object-at](https://github.com/bubkoo/object-at) - Get object's property according to the path.
- [object-has](https://github.com/bubkoo/object-has) - Checks if path is a direct property of object.
- [object-set](https://github.com/bubkoo/object-set) - Sets the value at path of object.
- [object-unset](https://github.com/bubkoo/object-unset) - Removes the property at path of object.
- [to-path](https://github.com/bubkoo/to-path) - Converts string to a property path array.


## Contributing

Pull requests and stars are highly welcome.

For bugs and feature requests, please [create an issue](https://github.com/bubkoo/pick-prop/issues/new).

16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var isObject = require('is-object');
var pickKey = require('pick-key');


module.exports = function (object) {

if (!object || !isObject(object)) {
return undefined;
}

var key = pickKey(object);

return key ? object[key] : undefined;
};
56 changes: 56 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "pick-prop",
"version": "1.0.0",
"description": "Randomly sampling a property from an object.",
"main": "index.js",
"scripts": {
"lint": "jshint index.js",
"pretest": "npm run lint",
"test": "mocha -R spec",
"test:cov": "rm -rf ./test/coverage && istanbul cover _mocha --dir ./test/coverage -- -R spec",
"test:rpt": "npm run test:cov && coveralls < ./test/coverage/lcov.info",
"prepublish": "npm test"
},
"keywords": [
"pick",
"sampling",
"array",
"arr",
"key",
"keys",
"value",
"values",
"property",
"properties",
"array-like",
"shuffle",
"random",
"randomly",
"randomize",
"rand"
],
"license": "MIT",
"author": {
"name": "bubkoo",
"email": "bubkoo.wy@gmail.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bubkoo/pick-prop.git"
},
"bugs": {
"url": "https://github.com/bubkoo/pick-prop/issues"
},
"homepage": "https://github.com/bubkoo/pick-prop#readme",
"devDependencies": {
"jshint": "^2.9.1",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"istanbul": "^0.4.2",
"mocha": "^2.4.5"
},
"dependencies": {
"is-object": "^1.0.1",
"pick-key": "^1.0.0"
}
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./spec/');
23 changes: 23 additions & 0 deletions test/spec/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

var expect = require('chai').expect;


describe('pick-key: ', function () {

var pickProp = require('../../');

it('common', function () {

expect(pickProp()).to.be.undefined;
expect(pickProp(null)).to.be.undefined;
expect(pickProp(1)).to.be.undefined;
expect(pickProp('a')).to.be.undefined;

expect(pickProp([])).to.be.undefined;
expect(pickProp({})).to.be.undefined;

expect(pickProp([2])).to.be.equal(2);
expect(pickProp({ a: 1, b: 2, c: 3 })).to.be.oneOf([1, 2, 3]);
});
});

0 comments on commit 5e04294

Please sign in to comment.