Skip to content

Commit

Permalink
[UPDATE] rename a row. [TESTS] row rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Feb 19, 2015
1 parent 494eab3 commit 27da40f
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 18 deletions.
63 changes: 45 additions & 18 deletions lib/rowrename.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* FUNCTION: rowrename( key, name )
* Renames a row.
*
* @param {Number|String} key - row identifier (either the row index or the row name)
* @param {Number|String} key - row identifier (either the row index or a row name)
* @param {String} name - new row name
* @returns {DataFrame} DataFrame instance
*/
Expand All @@ -42,36 +42,63 @@ function rowrename( key, name ) {
hash = this._rhash,
type = typeof key,
idx,
i;
n, i;

if ( type !== 'string' ) {
if ( type !== 'number' || key !== key || key%1 !== 0 ) {
if ( type !== 'number' || key !== key || key%1 !== 0 || key < 0 ) {
throw new TypeError( 'rowrename()::invalid input argument. Row identifier must be either a string or a positive integer. Value: `' + key + '`.' );
}
if ( key > names.length-1 ) {
if ( key >= names.length ) {
throw new RangeError( 'rowrename()::invalid input argument. Row index exceeds number of rows. Value: `' + key + '`.' );
}
}
if ( !hash.hasOwnProperty( key ) ) {
throw new Error( 'rowrename()::invalid input argument. Unrecognized row identifier. Value: `' + key + '`.' );
} else {
if ( !hash.hasOwnProperty( key ) ) {
throw new Error( 'rowrename()::invalid input argument. Unrecognized row identifier. Value: `' + key + '`.' );
}
}
if ( typeof name !== 'string' ) {
throw new TypeError( 'rowrename()::invalid input argument. Row name must be a string. Value: `' + name + '`.' );
}
// Get the row indices and update their associated row name...
idx = hash[ key ];
for ( i = 0; i < idx.length; i++ ) {
names[ idx[i] ] = name;
}
// Delete the old name from the row hash table and reassign the indices to the new name...
delete hash[ key ];
if ( hash.hasOwnProperty( key ) ) {
// The name already exists; add the indices to the existing name...
if ( type === 'string' ) {
// Get the row indices and update their associated row name...
idx = hash[ key ];
for ( i = 0; i < idx.length; i++ ) {
hash[ key ].push( idx[ i ] );
names[ idx[i] ] = name;
}
// Delete the old name from the row hash table and reassign the indices to the new name...
delete hash[ key ];
if ( hash.hasOwnProperty( name ) ) {
// The name already exists; add the indices to the existing name...
for ( i = 0; i < idx.length; i++ ) {
hash[ name ].push( idx[ i ] );
}
} else {
hash[ name ] = idx;
}
} else {
hash[ name ] = idx;
// Get the name associated with the index and replace the name in the row name array...
n = names[ key ];
names[ key ] = name;

// Remove the index from the row hash entry...
idx = hash[ n ];
for ( i = 0; i < idx.length; i++ ) {
if ( idx[ i ] === key ) {
break;
}
}
idx.splice( i, 1 );

// Check if any other indices are still associated with this name...
if ( !idx.length ) {
delete hash[ n ];
}
// Update the hash for row name lookups...
if ( hash.hasOwnProperty( n ) ) {
hash[ n ].push( key );
} else {
hash[ n ] = [ key ];
}
}
return this;
} // end FUNCTION rowrename()
Expand Down
139 changes: 139 additions & 0 deletions test/test.rowrename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* global require, describe, it, beforeEach */
'use strict';

// MODULES //

var // Expectation library:
chai = require( 'chai' ),

// Module to be tested:
DataFrame = require( './../lib' );


// VARIABLES //

var expect = chai.expect,
assert = chai.assert;


// TESTS //

describe( 'rowrename', function tests() {

var data, df;

beforeEach( function beforeEach() {
data = [
[1,2,3],
[4,5,6],
[7,8,9]
];
df = new DataFrame( data, {
'rownames': ['beep', 'boop', 'beep' ]
});
});

it( 'should provide a method to rename a data frame row', function test() {
expect( df.rowrename ).to.be.a( 'function' );
});

it( 'should throw an error if not provided either a string or a positive integer for the row identifier', function test() {
var values = [
-1,
5.45,
null,
undefined,
NaN,
true,
function(){},
{},
[]
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
df.rowrename( value, '' );
};
}
});

it( 'should throw a range error if the row identifier exceeds the maximum row index', function test() {
expect( badValue ).to.throw( RangeError );
function badValue() {
df.rowrename( data.length, '' );
}
});

it( 'should throw an error if provided an unknown row name', function test() {
expect( badValue ).to.throw( Error );
function badValue() {
df.rowrename( 'ajfadjfladsj', '' );
}
});

it( 'should throw an error if new row name is not a string', function test() {
var values = [
5,
null,
undefined,
NaN,
true,
function(){},
{},
[]
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
df.rowrename( 'beep', value );
};
}
});

it( 'should rename all rows matching a row name', function test() {
var expected, actual;

expected = [ 'foo', 'boop', 'foo' ];
df.rowrename( 'beep', 'foo' );
actual = df.rownames();

assert.deepEqual( actual, expected );
});

it( 'should allow renaming rows to an existing name', function test() {
var expected, actual;

expected = [ 'beep', 'beep', 'beep' ];
df.rowrename( 'boop', 'beep' );
actual = df.rownames();

assert.deepEqual( actual, expected );
});

it( 'should rename an individual row located at a specific row index', function test() {
var expected, actual;

expected = [ 'beep', 'foo', 'beep' ];
df.rowrename( 1, 'foo' );
actual = df.rownames();

assert.deepEqual( actual, expected );
});

it( 'should allow renaming a row located at a specific row index to an existing name', function test() {
var expected, actual;

expected = [ 'beep', 'boop', 'boop' ];
df.rowrename( 2, 'boop' );
actual = df.rownames();

assert.deepEqual( actual, expected );
});

});

0 comments on commit 27da40f

Please sign in to comment.