Skip to content
This repository has been archived by the owner on Nov 2, 2019. It is now read-only.

Commit

Permalink
even index array
Browse files Browse the repository at this point in the history
  • Loading branch information
Prosen-Ghosh committed Oct 4, 2017
1 parent 35b33dd commit e1e72d3
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# even-index
Returns an array with items evenly indexed

![npm](https://img.shields.io/npm/v/even-index.svg) ![license](https://img.shields.io/npm/l/even-index.svg) ![github-issues](https://img.shields.io/github/issues/Prosen-Ghosh/even-index.svg)


![travis-status](https://img.shields.io/travis/Prosen-Ghosh/even-index.svg)
![stars](https://img.shields.io/github/stars/Prosen-Ghosh/even-index.svg)
![forks](https://img.shields.io/github/forks/Prosen-Ghosh/even-index.svg)

## Features


## `npm` Install

`npm install --save even-index`


## Script Tag

#### For Development
```js
<script src=""></script>
```
#### For Production
```js
<script src=""></script>
```

## Usage

```js

const evenIndex = require('even-index');

evenIndex([]);
//=> []

evenIndex([1]);
//=> [ 1 ]

evenIndex([1,-10]);
//=> [ 1 ]

evenIndex([1,-10,45]);
//=> [ 1, 45 ]

evenIndex(["foo","baz","bar","bob","tim"]);
//=> [ 'foo', 'bar', 'tim' ]

evenIndex(["foo","baz","bar",{a : 10},[10,20,30]]);
//=> [ 'foo', 'bar', [ 10, 20, 30 ] ]

evenIndex(["foo","baz","bar",[10,30],function(){}]);
//=> [ 'foo', 'bar', [Function] ]

evenIndex(); // without parameter this function will return a type error
//=> TypeError: expect an array got undefined

evenIndex({}); // this function only expect an array
//=> TypeError: expect an array got object

```

## Author

Prosen Ghosh <[email protected]> (https://bd.linkedin.com/in/prosen-ghosh-baba9aa8)

## License

- **MIT**
15 changes: 15 additions & 0 deletions even-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const evenIndex = (function(){
'use strict';
const fn = function(arr){
if(Object.prototype.toString.call(arr).toLowerCase() !== '[object array]')throw new TypeError('expect an array got ' + typeof arr);
return arr.reduce((p,c,i) => {
if(!(i&1))p.push(c);
return p;
},[]);
}
return fn;
})();

if (typeof module === 'object' && module.exports) {
module.exports = evenIndex;
}
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "even-index",
"version": "1.0.0",
"description": "Returns an array with items evenly indexed",
"main": "even-index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha",
"readme": "node ./node_modules/.bin/node-readme"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Prosen-Ghosh/even-index.git"
},
"keywords": [
"even",
"number",
"array",
"index",
"elements"
],
"author": "Prosen Ghosh <[email protected]> (https://bd.linkedin.com/in/prosen-ghosh-baba9aa8)",
"license": "MIT",
"bugs": {
"url": "https://github.com/Prosen-Ghosh/even-index/issues"
},
"homepage": "https://github.com/Prosen-Ghosh/even-index#readme",
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^4.0.0",
"node-readme": "^0.1.9"
}
}
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const evenIndex = require('../even-index');
const assert = require('chai').assert;
describe('Even Index Array',function(){
it('Function Shuld Return Even Index Of Array from the orginal array',function(){
assert.deepEqual(evenIndex([1]),[1]);
assert.deepEqual(evenIndex([]),[]);
assert.deepEqual(evenIndex([-2,-1]),[-2]);
assert.deepEqual(evenIndex([[],-2,[]]),[[],[]]);
assert.deepEqual(evenIndex(["foo","baz","jam","bar","bob"]),["foo","jam","bob"]);
});
});

0 comments on commit e1e72d3

Please sign in to comment.