Skip to content

Commit 70a2da1

Browse files
committed
[UPDATE] lib, tests, examples.
1 parent 75e07ec commit 70a2da1

File tree

5 files changed

+128
-21
lines changed

5 files changed

+128
-21
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,47 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
1919
To use the module,
2020

2121
``` javascript
22-
var foo = require( 'compute-isnumeric' );
22+
var isnumeric = require( 'compute-isnumeric' );
2323
```
2424

25-
#### foo( arr )
25+
#### isnumeric( arr )
2626

27-
What does this function do?
27+
Computes for each `array` element whether an element is numeric. The function returns an `array` with length equal to that of the input `array`. Each output `array` element is either `0` or `1`. A value of `1` means that an element is numeric and `0` means that an element is __not__ numeric.
28+
29+
``` javascript
30+
var out = isnumeric( [ 5, 1/0, 'beep', 3, 9, NaN, true ] );
31+
// returns [ 1, 1, 0, 1, 1, 0, 0 ]
32+
```
2833

2934

3035
## Examples
3136

3237
``` javascript
33-
var foo = require( 'compute-isnumeric' );
38+
var isnumeric = require( 'compute-isnumeric' );
39+
40+
// Simulate some data...
41+
var data = new Array( 100 ),
42+
len = data.length,
43+
rand;
44+
45+
// Every so often include a non-numeric value...
46+
for ( var i = 0; i < len; i++ ) {
47+
rand = Math.random()*10;
48+
if ( rand < 0.5 ) {
49+
rand = null;
50+
}
51+
data[ i ] = rand;
52+
}
53+
54+
var out = isnumeric( data );
55+
56+
// Count the number of numeric values detected...
57+
var sum = 0;
58+
for ( var i = 0; i < len; i++ ) {
59+
sum += out[ i ];
60+
}
61+
62+
console.log( 'Count: %d', sum );
3463
```
3564

3665
To run the example code from the top-level application directory,

examples/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
'use strict';
22

3-
var module = require( './../lib' );
3+
var isnumeric = require( './../lib' );
4+
5+
// Simulate some data...
6+
var data = new Array( 100 ),
7+
len = data.length,
8+
rand;
9+
10+
// Every so often include a non-numeric value...
11+
for ( var i = 0; i < len; i++ ) {
12+
rand = Math.random()*10;
13+
if ( rand < 0.5 ) {
14+
rand = null;
15+
}
16+
data[ i ] = rand;
17+
}
18+
19+
var out = isnumeric( data );
20+
21+
// Count the number of numeric values detected...
22+
var sum = 0;
23+
for ( var i = 0; i < len; i++ ) {
24+
sum += out[ i ];
25+
}
26+
27+
console.log( 'Count: %d', sum );

lib/index.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,34 @@
2828

2929
'use strict';
3030

31-
// MODULES //
32-
33-
// var module_alias = require( 'module_name' );
34-
35-
36-
// FUNCTIONS //
31+
// ISNUMERIC //
3732

3833
/**
39-
* FUNCTION: foo()
40-
* {{ foo description }}.
34+
* FUNCTION: isnumeric( arr )
35+
* Computes for each array element whether an element is numeric.
36+
*
37+
* @param {Array} arr - input array
38+
* @param {Array} array of 1s and 0s indicating if an element is numeric
4139
*/
42-
function foo() {
43-
44-
} // end FUNCTION foo()
40+
function isnumeric( arr ) {
41+
if ( !Array.isArray( arr ) ) {
42+
throw new TypeError( 'isnumeric()::invalid input argument. Must provide an array.' );
43+
}
44+
var len = arr.length,
45+
out = new Array( len ),
46+
val;
47+
48+
for ( var i = 0; i < len; i++ ) {
49+
out[ i ] = 0;
50+
val = arr[ i ];
51+
if ( typeof val === 'number' && val === val ) {
52+
out[ i ] = 1;
53+
}
54+
}
55+
return out;
56+
} // end FUNCTION isnumeric()
4557

4658

4759
// EXPORTS //
4860

49-
module.exports = foo;
61+
module.exports = isnumeric;

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@
2525
"keywords": [
2626
"compute.io",
2727
"compute",
28-
"computation"
28+
"computation",
29+
"array",
30+
"utilities",
31+
"utils",
32+
"isnumeric",
33+
"numeric",
34+
"logical",
35+
"mathematics",
36+
"math",
37+
"statistics",
38+
"stats"
2939
],
3040
"bugs": {
3141
"url": "https://github.com/compute-io/isnumeric/issues"

test/test.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var // Expectation library:
66
chai = require( 'chai' ),
77

88
// Module to be tested:
9-
lib = require( './../lib' );
9+
isnumeric = require( './../lib' );
1010

1111

1212
// VARIABLES //
@@ -20,9 +20,41 @@ var expect = chai.expect,
2020
describe( 'compute-isnumeric', function tests() {
2121

2222
it( 'should export a function', function test() {
23-
expect( lib ).to.be.a( 'function' );
23+
expect( isnumeric ).to.be.a( 'function' );
2424
});
2525

26-
it( 'should do something' );
26+
it( 'should throw an error if not provided an array', function test() {
27+
var values = [
28+
'5',
29+
5,
30+
null,
31+
undefined,
32+
NaN,
33+
true,
34+
{},
35+
function(){}
36+
];
37+
38+
for ( var i = 0; i < values.length; i++ ) {
39+
expect( badValue( values[i] ) ).to.throw( TypeError );
40+
}
41+
42+
function badValue( value ) {
43+
return function() {
44+
isnumeric( value );
45+
};
46+
}
47+
});
48+
49+
it( 'should compute whether each array element is numeric', function test() {
50+
var data, expected, actual;
51+
52+
data = [ 5, 'beep', 3, 9, true, NaN, 1/0 ];
53+
54+
expected = [ 1, 0, 1, 1, 0, 0, 1 ];
55+
actual = isnumeric( data );
56+
57+
assert.deepEqual( actual, expected );
58+
});
2759

2860
});

0 commit comments

Comments
 (0)