Skip to content

Commit

Permalink
Merge pull request #1 from compute-io/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed May 15, 2015
2 parents 7a59b98 + a66b19f commit 80992d2
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto
14 changes: 14 additions & 0 deletions .jshintignore
@@ -0,0 +1,14 @@

# Directories #
###############
build/
reports/
dist/

# Node.js #
###########
/node_modules/

# Git #
#######
.git*
2 changes: 1 addition & 1 deletion .jshintrc
Expand Up @@ -68,4 +68,4 @@
"wsh": false,
"yui": false,
"globals": {}
}
}
2 changes: 2 additions & 0 deletions .npmignore
Expand Up @@ -13,6 +13,7 @@ examples/
reports/
support/
test/
benchmark/

# Node.js #
###########
Expand Down Expand Up @@ -46,5 +47,6 @@ Desktop.ini
# Utilities #
#############
.jshintrc
.jshintignore
.travis.yml
.editorconfig
11 changes: 9 additions & 2 deletions .travis.yml
@@ -1,5 +1,12 @@
language: node_js
node_js:
- "0.10"
- '0.12'
- '0.11'
- '0.10'
- '0.8'
- 'iojs'
before_install:
- npm update -g npm
after_script:
- npm run coveralls
- npm run coveralls

4 changes: 2 additions & 2 deletions LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Athan Reines.
Copyright (c) 2014-2015 The Compute.io Authors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
34 changes: 27 additions & 7 deletions Makefile
Expand Up @@ -5,32 +5,42 @@
# Set the node.js environment to test:
NODE_ENV ?= test

# Kernel name:
KERNEL ?= $(shell uname -s)

ifeq ($(KERNEL), Darwin)
OPEN ?= open
else
OPEN ?= xdg-open
endif

# NOTES #

NOTES ?= 'TODO|FIXME'
NOTES ?= 'TODO|FIXME|WARNING|HACK|NOTE'


# MOCHA #

# Specify the test framework bin locations:
MOCHA ?= ./node_modules/.bin/mocha
_MOCHA ?= ./node_modules/.bin/_mocha

# Specify the mocha reporter:
MOCHA_REPORTER ?= spec


# ISTANBUL #

# Istanbul configuration:
ISTANBUL ?= ./node_modules/.bin/istanbul
ISTANBUL_OUT ?= ./reports/coverage
ISTANBUL_REPORT ?= lcov
ISTANBUL_LCOV_INFO_PATH ?= $(ISTANBUL_OUT)/lcov.info
ISTANBUL_HTML_REPORT_PATH ?= $(ISTANBUL_OUT)/lcov-report/index.html


# JSHINT #

JSHINT ?= ./node_modules/.bin/jshint
JSHINT_REPORTER ?= ./node_modules/jshint-stylish/stylish.js



# FILES #

Expand Down Expand Up @@ -96,9 +106,20 @@ test-istanbul-mocha: node_modules
view-cov: view-istanbul-report

view-istanbul-report:
open $(ISTANBUL_HTML_REPORT_PATH)
$(OPEN) $(ISTANBUL_HTML_REPORT_PATH)


# LINT #

.PHONY: lint lint-jshint

lint: lint-jshint

lint-jshint: node_modules
$(JSHINT) \
--reporter $(JSHINT_REPORTER) \
./


# NODE #

Expand All @@ -117,7 +138,6 @@ clean-node:


# CLEAN #

.PHONY: clean

clean:
Expand Down
116 changes: 91 additions & 25 deletions README.md
Expand Up @@ -2,7 +2,7 @@ Subtract
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Computes an element-wise subtraction of a numeric array.
> Computes an element-wise subtraction.

## Installation
Expand All @@ -16,40 +16,117 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse

## Usage

To use the module,

``` javascript
var subtract = require( 'compute-subtract' );
```

#### subtract( arr, x )
#### subtract( arr, x[, opts] )

Computes an element-wise subtraction of an input `array`. `x` may be either an `array` of equal length or a scalar.
Computes an element-wise subtraction. `x` may be either an `array` of equal length or a `numeric` value.

``` javascript
subtract( [ 2, 1, 4, 2 ], 1 );
var arr = [ 2, 1, 4, 2 ],
out;

out = subtract( arr, 1 );
// returns [ 1, 0, 3, 1 ]

subtract( [ 2, 1, 4, 2 ], [ 1, 2, 3, 3 ] );
out = subtract( arr, [ 1, 2, 3, 3 ] );
// returns [ 1, -1, 1, -1 ]
```

The function accepts the following `options`:

* __copy__: `boolean` indicating whether to return a new `array`. Default: `true`.
* __accessor__: accessor `function` for accessing values in object `arrays`.

To mutate the input `array` (e.g., when input values can be discarded or when optimizing memory usage), set the `copy` option to `false`.

``` javascript
var arr = [ 5, 3, 8, 3, 2 ];

var out = subtract( arr, 4, {
'copy': false
});
// returns [ 1, -1, 4, -1, -2 ]

console.log( arr === out );
// returns true
```

__Note__: mutation is the `array` equivalent of a __minus-equal__ (`-=`).

For object `arrays`, provide an accessor `function` for accessing `array` values.

``` javascript
var data = [
['beep', 5],
['boop', 3],
['bip', 8],
['bap', 3],
['baz', 2]
];

function getValue( d, i ) {
return d[ 1 ];
}

var out = subtract( data, 4, {
'accessor': getValue
});
// returns [ 1, -1, 4, -1, -2 ]
```

When subtracting values between two object `arrays`, provide an accessor `function` which accepts `3` arguments.

``` javascript
var data = [
['beep', 5],
['boop', 3],
['bip', 8],
['bap', 3],
['baz', 2]
];

var arr = [
{'x': 4},
{'x': 5},
{'x': 6},
{'x': 5},
{'x': 3}
];

function getValue( d, i, j ) {
if ( j === 0 ) {
return d[ 1 ];
}
return d.x;
}

var out = subtract( data, arr, {
'accessor': getValue
});
// returns [ 1, -2, 2, -2, -1 ]
```

__Note__: `j` corresponds to the input `array` index, where `j=0` is the index for the first input `array` and `j=1` is the index for the second input `array`.




## Examples

``` javascript
var subtract = require( 'compute-subtract' );

// Simulate some data...
var data = new Array( 100 );

for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random()*100 );
}

subtract( data, 10 );
var out = subtract( data, 10 );

console.log( data.join( '\n' ) );
console.log( out.join( '\n' ) );
```

To run the example code from the top-level application directory,
Expand All @@ -58,23 +135,12 @@ To run the example code from the top-level application directory,
$ node ./examples/index.js
```

## Notes

This function mutates the input `array`. If mutation is undesired,

``` javascript
var data = [ 1, 2, 3, 4 ],
copy = data.slice();

subtract( copy, 2 );
```


## Tests

### Unit

Unit tests use the [Mocha](http://visionmedia.github.io/mocha) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
Unit tests use the [Mocha](http://mochajs.org) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:

``` bash
$ make test
Expand All @@ -98,15 +164,15 @@ $ make view-cov
```


---
## License

[MIT license](http://opensource.org/licenses/MIT).


---
## Copyright

Copyright &copy; 2014. Athan Reines.
Copyright &copy; 2014-2015. The Compute.io Authors.


[npm-image]: http://img.shields.io/npm/v/compute-subtract.svg
Expand Down
7 changes: 2 additions & 5 deletions examples/index.js
Expand Up @@ -2,13 +2,10 @@

var subtract = require( './../lib' );

// Simulate some data...
var data = new Array( 100 );

for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random()*100 );
}
var out = subtract( data, 10 );

subtract( data, 10 );

console.log( data.join( '\n' ) );
console.log( out.join( '\n' ) );

0 comments on commit 80992d2

Please sign in to comment.