toggle-array
Advanced tools
Comparing version 0.1.0 to 1.0.0
35
index.js
@@ -5,3 +5,5 @@ 'use strict'; | ||
module.exports = function(arr, prop, idx) { | ||
function toggle(arr, prop, idx) { | ||
var ele = arr[idx]; | ||
var init = isObject(ele) ? ele[prop] : false; | ||
var len = arr.length; | ||
@@ -11,11 +13,32 @@ var i = -1; | ||
while (++i < len) { | ||
var ele = arr[i]; | ||
if (!isObject(ele)) continue; | ||
if (i === idx) { | ||
ele[prop] = true; | ||
ele = arr[i]; | ||
if (!isObject(ele)) { | ||
continue; | ||
} | ||
if (i !== idx) { | ||
ele[prop] = !!init; | ||
} else { | ||
ele[prop] = false; | ||
ele[prop] = !init; | ||
} | ||
} | ||
return arr; | ||
} | ||
toggle.enable = function(arr, prop, i) { | ||
return initial(arr, prop, i, false); | ||
}; | ||
toggle.disable = function(arr, prop, i) { | ||
return initial(arr, prop, i, true); | ||
}; | ||
function initial(arr, prop, i, init) { | ||
if (isObject(arr[i])) arr[i][prop] = init; | ||
return toggle(arr, prop, i); | ||
} | ||
/** | ||
* Expose `toggle` | ||
*/ | ||
module.exports = toggle; |
{ | ||
"name": "toggle-array", | ||
"description": "In an array of objects, this enables a property on the object at the specified index, while disabling the property on all other objects.", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/toggle-array", | ||
@@ -11,6 +11,5 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
}, | ||
"license": "Released under the MIT license.", | ||
"license": "MIT", | ||
"files": [ | ||
"index.js", | ||
"LICENSE" | ||
"index.js" | ||
], | ||
@@ -24,2 +23,9 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
"isobject": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.12", | ||
"mocha": "^3.3.0" | ||
}, | ||
"keywords": [ | ||
@@ -29,9 +35,2 @@ "array", | ||
], | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.10", | ||
"mocha": "^3.0.2" | ||
}, | ||
"dependencies": { | ||
"isobject": "^2.1.0" | ||
}, | ||
"verb": { | ||
@@ -46,13 +45,13 @@ "toc": false, | ||
], | ||
"related": { | ||
"list": [ | ||
"prompt-choices", | ||
"arr-flatten", | ||
"array-unique" | ||
] | ||
}, | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"related": { | ||
"list": [] | ||
}, | ||
"reflinks": [ | ||
"verb", | ||
"verb-generate-readme" | ||
] | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# toggle-array [![NPM version](https://img.shields.io/npm/v/toggle-array.svg?style=flat)](https://www.npmjs.com/package/toggle-array) [![NPM downloads](https://img.shields.io/npm/dm/toggle-array.svg?style=flat)](https://npmjs.org/package/toggle-array) [![Build Status](https://img.shields.io/travis/jonschlinkert/toggle-array.svg?style=flat)](https://travis-ci.org/jonschlinkert/toggle-array) | ||
# toggle-array [![NPM version](https://img.shields.io/npm/v/toggle-array.svg?style=flat)](https://www.npmjs.com/package/toggle-array) [![NPM monthly downloads](https://img.shields.io/npm/dm/toggle-array.svg?style=flat)](https://npmjs.org/package/toggle-array) [![NPM total downloads](https://img.shields.io/npm/dt/toggle-array.svg?style=flat)](https://npmjs.org/package/toggle-array) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/toggle-array.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/toggle-array) | ||
@@ -16,12 +16,12 @@ > In an array of objects, this enables a property on the object at the specified index, while disabling the property on all other objects. | ||
```js | ||
var toggleArray = require('toggle-array'); | ||
var toggle = require('toggle-array'); | ||
var arr = [ | ||
{}, | ||
{}, | ||
{}, | ||
{}, | ||
{} | ||
{foo: true}, | ||
{foo: true}, | ||
{foo: true}, | ||
{foo: true}, | ||
{foo: true} | ||
]; | ||
console.log(toggleArray(arr, 'foo', 2)); | ||
console.log(toggle(arr, 'foo', 2)); | ||
// [ { foo: false }, | ||
@@ -34,4 +34,54 @@ // { foo: false }, | ||
## .enable | ||
Enables the element at the given index, and disables all other items: | ||
```js | ||
var toggle = require('toggle-array'); | ||
var arr = [ | ||
{foo: true}, | ||
{foo: true}, | ||
{foo: true}, | ||
{foo: true}, | ||
{foo: true} | ||
]; | ||
console.log(toggle.enable(arr, 'foo', 2)); | ||
// [ { foo: false }, | ||
// { foo: false }, | ||
// { foo: true }, | ||
// { foo: false }, | ||
// { foo: false } ] | ||
``` | ||
## .disable | ||
Disabled the element at the given index, and enables all other items: | ||
```js | ||
var toggle = require('toggle-array'); | ||
var arr = [ | ||
{foo: false}, | ||
{foo: false}, | ||
{foo: false}, | ||
{foo: false}, | ||
{foo: false} | ||
]; | ||
console.log(toggle.disable(arr, 'foo', 2)); | ||
// [ { foo: true }, | ||
// { foo: true }, | ||
// { foo: false }, | ||
// { foo: true }, | ||
// { foo: true } ] | ||
``` | ||
## About | ||
### Related projects | ||
* [arr-flatten](https://www.npmjs.com/package/arr-flatten): Recursively flatten an array or arrays. | [homepage](https://github.com/jonschlinkert/arr-flatten "Recursively flatten an array or arrays.") | ||
* [array-unique](https://www.npmjs.com/package/array-unique): Remove duplicate values from an array. Fastest ES5 implementation. | [homepage](https://github.com/jonschlinkert/array-unique "Remove duplicate values from an array. Fastest ES5 implementation.") | ||
* [prompt-choices](https://www.npmjs.com/package/prompt-choices): Create an array of multiple choice objects for use in prompts. | [homepage](https://github.com/enquirer/prompt-choices "Create an array of multiple choice objects for use in prompts.") | ||
### Contributing | ||
@@ -43,8 +93,8 @@ | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme and API documentation with [verb](https://github.com/verbose/verb): | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verb verb-generate-readme && verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
@@ -54,6 +104,6 @@ | ||
Install dev dependencies: | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
```sh | ||
$ npm install -d && npm test | ||
$ npm install && npm test | ||
``` | ||
@@ -66,11 +116,11 @@ | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
### License | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/toggle-array/blob/master/LICENSE). | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on August 29, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 07, 2017._ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6640
0
34
0
121
+ Addedisobject@3.0.1(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@2.1.0(transitive)
Updatedisobject@^3.0.0