Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Aug 16, 2015
0 parents commit 254bd8a
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- 'iojs'
- '0.10'
- '0.12'
before_install:
- npm i -g gulp
after_success:
- gulp coveralls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Javier Cejudo <javier@javiercejudo.com> (http://www.javiercejudo.com)

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.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# every-against-first

[![Build Status](https://travis-ci.org/javiercejudo/every-against-first.svg)](https://travis-ci.org/javiercejudo/every-against-first)
[![Coverage Status](https://coveralls.io/repos/javiercejudo/every-against-first/badge.svg?branch=master)](https://coveralls.io/r/javiercejudo/every-against-first?branch=master)
[![Code Climate](https://codeclimate.com/github/javiercejudo/every-against-first/badges/gpa.svg)](https://codeclimate.com/github/javiercejudo/every-against-first)

Test every element in array against the first

## Install

npm i every-against-first

## Usage

```js
var everyAgainstFirst = require('every-against-first');


```

See more examples of [fixture callbacks](fixtures/callbacks.js).
22 changes: 22 additions & 0 deletions fixtures/callbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*jshint node:true, mocha:true */

'use strict';

module.exports = {
isAscendingPair: isAscendingPair,
isAscendingAbsValue: isAscendingAbsValue,
isConsecutive: isConsecutive
};


function isAscendingPair(a, b) {
return b > a;
}

function isAscendingAbsValue(a, b) {
return this.abs(b) > this.abs(a);
}

function isConsecutive(a, x, index, array) {
return a + index === x && a + index === array[index - 1];
}
28 changes: 28 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var gulp = require('gulp');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var rimraf = require('rimraf');
var coveralls = require('gulp-coveralls');

gulp.task('clean', function (cb) {
rimraf('./coverage', cb);
});

gulp.task('instrument', function () {
return gulp.src(['src/*.js'])
.pipe(istanbul())
.pipe(istanbul.hookRequire());
});

gulp.task('test', ['clean', 'instrument'], function () {
return gulp.src(['test/*.js'])
.pipe(mocha())
.pipe(istanbul.writeReports());
});

gulp.task('coveralls', function () {
gulp.src('coverage/lcov.info')
.pipe(coveralls());
});

gulp.task('default', ['test']);
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "every-against-first",
"version": "0.0.0",
"description": "Test every element in array against the first",
"main": "src/everyAgainstFirst.js",
"scripts": {
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "https://github.com/javiercejudo/every-against-first"
},
"keywords": [
"all",
"array",
"every"
],
"author": "Javier Cejudo",
"license": "MIT",
"bugs": {
"url": "https://github.com/javiercejudo/every-against-first/issues"
},
"homepage": "https://github.com/javiercejudo/every-against-first",
"devDependencies": {
"gulp": "^3.8.11",
"gulp-coveralls": "^0.1.3",
"gulp-istanbul": "^0.6.0",
"gulp-mocha": "^2.0.0",
"rimraf": "^2.3.2",
"should": "^5.0.0"
}
}
9 changes: 9 additions & 0 deletions src/everyAgainstFirst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function everyAgainstFirst(array, callback, thisArg) {
var first = array.shift();

return array.every(function (current, index) {
return callback.call(thisArg, first, current, index + 1, array);
});
};
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

require('should');

var everyAgainstFirst = require('../src/everyAgainstFirst.js');
var fixtures = require('../fixtures/callbacks');

describe('every-against-first', function () {
it('should execute test for the every element against the first', function () {
everyAgainstFirst([5, 7, 10], fixtures.isAscendingPair).should.be.exactly(true);
everyAgainstFirst([5, 7, 4], fixtures.isAscendingPair).should.be.exactly(false);
});

it('should return true when array is shorter than 2', function () {
everyAgainstFirst([1], fixtures.isAscendingPair).should.be.exactly(true);
everyAgainstFirst([], fixtures.isAscendingPair).should.be.exactly(true);
});

it('should pass index and array to the callback', function () {
everyAgainstFirst([1, 2, 3, 4], fixtures.isConsecutive).should.be.exactly(true);
});

it('should execute the callback with the provided thisArg as this', function () {
everyAgainstFirst([3, -5], fixtures.isAscendingAbsValue, Math).should.be.exactly(true);
everyAgainstFirst([-3, -2], fixtures.isAscendingAbsValue, Math).should.be.exactly(false);
});
});

0 comments on commit 254bd8a

Please sign in to comment.