compute-anagram-hash
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -418,5 +418,57 @@ /** | ||
/** | ||
* METHOD: copy( [keys] ) | ||
* Returns a copy of the hash table. | ||
* | ||
* @param {String[]} [keys] - list of keys to be copied | ||
* @returns {AnagramHash} new AnagramHash instance | ||
*/ | ||
AnagramHash.prototype.copy = function( arr ) { | ||
var nargs = arguments.length, | ||
hash = this._hash, | ||
nhash, | ||
nlist, | ||
copy, | ||
list, | ||
keys, | ||
key, | ||
len, | ||
N, i, j; | ||
if ( nargs ) { | ||
if ( !isStringArray( arr ) ) { | ||
throw new TypeError( 'copy()::invalid input argument. Keys must be a primitive string array. Value: `' + arr + '`.' ); | ||
} | ||
keys = arr; | ||
} else { | ||
keys = Object.keys( hash ); | ||
} | ||
len = keys.length; | ||
// Create a (n)ew hash table: | ||
copy = new AnagramHash(); | ||
nhash = copy._hash; | ||
// Loop through each key... | ||
for ( i = 0; i < len; i++ ) { | ||
key = keys[ i ]; | ||
if ( !hash.hasOwnProperty( key ) ) { | ||
continue; | ||
} | ||
list = hash[ key ]; | ||
N = list.length; | ||
// Copy the anagrams to the new hash table... | ||
nlist = new Array( N ); | ||
for ( j = 0; j < N; j++ ) { | ||
nlist[ j ] = list[ j ]; | ||
} | ||
nhash[ key ] = nlist; | ||
} | ||
return copy; | ||
}; // end METHOD copy() | ||
// EXPORTS // | ||
module.exports = AnagramHash; |
{ | ||
"name": "compute-anagram-hash", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Anagram hash table.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -159,4 +159,25 @@ Anagram Hash | ||
#### hash.copy( [keys] ) | ||
Copies an [anagram](http://en.wikipedia.org/wiki/Anagram) hash table to a new hash table instance. | ||
``` javascript | ||
var copy = hash.copy(); | ||
var list = copy.get( 'abt', true ); | ||
// returns ['bat','tab'] | ||
``` | ||
To only copy specific __keys__ to a new hash table, provide a __keys__ `array`. | ||
``` javascript | ||
var copy = hash.copy( ['beep'] ); | ||
var keys = copy.keys(); | ||
// returns ['beep'] | ||
``` | ||
## Examples | ||
@@ -163,0 +184,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19526
435
263