From 5b1e75e52848a2651787e1058cfe374afb7c27f7 Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Sun, 24 May 2015 05:28:49 -0400 Subject: [PATCH] [UPDATE] todo + accessor --- .gitattributes | 1 + .jshintignore | 14 +++++++++ .npmignore | 1 + .travis.yml | 11 +++++-- LICENSE | 4 +-- Makefile | 34 +++++++++++++++----- README.md | 34 +++++++++++++++----- lib/index.js | 84 ++++++++++++++++++++++++++------------------------ package.json | 21 +++++++------ test/test.js | 51 ++++++++++++++++++++++++++++++ 10 files changed, 186 insertions(+), 69 deletions(-) create mode 100644 .gitattributes create mode 100644 .jshintignore diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.jshintignore b/.jshintignore new file mode 100644 index 0000000..3163c22 --- /dev/null +++ b/.jshintignore @@ -0,0 +1,14 @@ + +# Directories # +############### +build/ +reports/ +dist/ + +# Node.js # +########### +/node_modules/ + +# Git # +####### +.git* diff --git a/.npmignore b/.npmignore index 7de8cda..9db298d 100644 --- a/.npmignore +++ b/.npmignore @@ -47,5 +47,6 @@ Desktop.ini # Utilities # ############# .jshintrc +.jshintignore .travis.yml .editorconfig diff --git a/.travis.yml b/.travis.yml index 7c16620..29cff27 100644 --- a/.travis.yml +++ b/.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 \ No newline at end of file + - npm run coveralls + diff --git a/LICENSE b/LICENSE index 2fe3939..93cf2ea 100644 --- a/LICENSE +++ b/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 @@ -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. \ No newline at end of file +SOFTWARE. diff --git a/Makefile b/Makefile index f5648d1..3234fbb 100644 --- a/Makefile +++ b/Makefile @@ -5,25 +5,29 @@ # 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 @@ -31,6 +35,12 @@ 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 # @@ -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 # @@ -117,7 +138,6 @@ clean-node: # CLEAN # - .PHONY: clean clean: diff --git a/README.md b/README.md index 98cb37c..c83086a 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,13 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse ## Usage -To use the module, - ``` javascript var min = require( 'compute-nanmin' ); ``` -#### min( arr ) +#### min( arr[, accessor] ) -Computes the minimum value of an array ignoring non-numeric values. +Computes the minimum value of an array ignoring non-numeric values. For numeric `arrays`, ``` javascript var data = [ 2, null, 5, 3, null, 7 ]; @@ -33,6 +31,26 @@ var val = min( data ); // returns 2 ``` +For non-numeric `arrays`, provide an accessor `function` for accessing numeric values + +``` javascript +var arr = [ + {'x':2}, + {'x':null}, + {'x':5}, + {'x':3}, + {'x':null}, + {'x':7} +]; + +function getValue( d, i ) { + return d.x; +} + +var val = max( arr, getValue ); +// returns 2 +``` + Note: if an input `array` does not contain any numeric values, the function returns `null`. @@ -66,7 +84,7 @@ $ node ./examples/index.js ### 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 @@ -90,15 +108,15 @@ $ make view-cov ``` +--- ## License -[MIT license](http://opensource.org/licenses/MIT). +[MIT license](http://opensource.org/licenses/MIT). ---- ## Copyright -Copyright © 2014. Athan Reines. +Copyright © 2014-2015. The Compute.io Authors. [npm-image]: http://img.shields.io/npm/v/compute-nanmin.svg diff --git a/lib/index.js b/lib/index.js index d5274bf..5b8bede 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,55 +1,57 @@ -/** -* -* COMPUTE: nanmin -* -* -* DESCRIPTION: -* - Computes the minimum value of an array ignoring non-numeric values. -* -* -* NOTES: -* [1] -* -* -* TODO: -* [1] -* -* -* LICENSE: -* MIT -* -* Copyright (c) 2014. Athan Reines. -* -* -* AUTHOR: -* Athan Reines. kgryte@gmail.com. 2014. -* -*/ - 'use strict'; +// MODULES // + +var isArray = require( 'validate.io-array' ), + isFunction = require( 'validate.io-function' ), + isNumber = require( 'validate.io-number' ); + +// NANMIN // + /** -* FUNCTION: nanmin( arr ) +* FUNCTION: nanmin( arr[, accessor] ) * Computes the minimum value of an array ignoring any non-numeric values. * -* @param {Array} arr - array of values -* @returns {Number} min value +* @param {Number[]|Array} arr - array of values +* @param {Function} [accessor] - accessor function for accessing array values +* @returns {Number|Null} min value or null */ -function nanmin( arr ) { - if ( !Array.isArray( arr ) ) { - throw new TypeError( 'nanmin()::invalid input argument. Must provide an array.' ); +function nanmin( arr, clbk ) { + if ( !isArray( arr ) ) { + throw new TypeError( 'nanmin()::invalid input argument. Must provide an array. Value: `' + arr + '`.' ); + } + if ( arguments.length > 1 && !isFunction( clbk ) ) { + throw new TypeError( 'nanmin()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' ); } + var len = arr.length, min = null, - val; + val, + i; + + if ( !len ) { + return null; + } - for ( var i = 0; i < len; i++ ) { - val = arr[ i ]; - if ( typeof val !== 'number' || val !== val ) { - continue; + if ( clbk ) { + for ( i = 0; i < len; i++ ) { + val = clbk( arr[ i ], i); + if ( !isNumber( val ) ) { + continue; + } + if ( min === null || val < min ) { + min = val; + } } - if ( min === null || val < min ) { - min = val; + } else { + for ( i = 0; i < len; i++ ) { + val = arr[ i ]; + if ( !isNumber( val ) ) { + continue; + } + if ( min === null || val < min ) { + min = val; + } } } return min; diff --git a/package.json b/package.json index ee89dc0..4f943b7 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,10 @@ { "name": "Athan Reines", "email": "kgryte@gmail.com" + }, + { + "name": "Philipp Burckhardt", + "email": "pburckhardt@outlook.com" } ], "scripts": { @@ -39,17 +43,16 @@ "bugs": { "url": "https://github.com/compute-io/nanmin/issues" }, - "dependencies": {}, + "dependencies": { + "validate.io-array": "^1.0.5", + "validate.io-function": "^1.0.2", + "validate.io-number": "^1.0.3" + }, "devDependencies": { - "chai": "1.x.x", - "mocha": "1.x.x", + "chai": "2.x.x", + "mocha": "2.x.x", "coveralls": "^2.11.1", "istanbul": "^0.3.0" }, - "licenses": [ - { - "type": "MIT", - "url": "http://www.opensource.org/licenses/MIT" - } - ] + "licenses": "MIT" } diff --git a/test/test.js b/test/test.js index 2fc774f..610a101 100644 --- a/test/test.js +++ b/test/test.js @@ -1,3 +1,4 @@ +/* global require, describe, it */ 'use strict'; // MODULES // @@ -45,6 +46,30 @@ describe( 'compute-nanmin', function tests() { } }); + + it( 'should throw an error if provided an accessor which is not a function', function test() { + var values = [ + '5', + 5, + [], + undefined, + null, + NaN, + true, + {} + ]; + + for ( var i = 0; i < values.length; i++ ) { + expect( badValue( values[i] ) ).to.throw( TypeError ); + } + + function badValue( value ) { + return function() { + min( [ 1, 2, 3 ], value ); + }; + } + }); + it( 'should return the minimum value', function test() { var data, expected; @@ -54,6 +79,32 @@ describe( 'compute-nanmin', function tests() { assert.strictEqual( min( data ), expected ); }); + it( 'should return the maximum value using an accessor function', function test() { + var data, expected, actual; + + data = [ + [1,null], + [1,4], + [2,2], + [2,NaN], + [3,5], + [4,3], + [3, true], + [3, undefined], + [5,8], + [6,2] + ]; + expected = 2; + actual = min( data, getValue ); + + assert.strictEqual( actual, expected ); + + function getValue( d ) { + return d[ 1 ]; + } + }); + + it( 'should return null if an input array does not contain any numeric values', function test() { var data;