Skip to content

Commit

Permalink
Cosmetic -- Removed biological term jargon to make the interface seem…
Browse files Browse the repository at this point in the history
… more general
  • Loading branch information
ptumati committed Dec 5, 2017
1 parent 294a092 commit 2e5afb9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
36 changes: 19 additions & 17 deletions lib/ObjectMutator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @fileOverview
* File contains the definitions for the Mutator class
* @fileOverview File contains the definitions for the Mutator class
* @author <a href="mailto:[email protected]">Pavan Tumati</a
* @version 1.0.0
* @name ObjectMutator
*/

/**
Expand All @@ -11,31 +13,31 @@ class Mutator {
/**
* @constructs Mutator
* @description Constructs mutator object
* @param {chromosome} The base object that gets mutated
* @param {mutationGroup} The set of (joint) mutations to apply to a chromosome
* @param {cullFunction} The function passed to a filter to remove any invalid/unwanted genes in the mutation set
* @param {baseObject} The base object that gets mutated
* @param {mutationGroup} The set of (joint) mutations to apply to a baseObject
* @param {cullFunction} The function passed to a filter to remove any invalid/unwanted mutations to the baseObject
*/
constructor( chromosome, mutationGroup, cullFunction ) {
constructor( baseObject, mutationGroup, cullFunction ) {
this.mutations = [];
this.chromosome = chromosome;
this.baseObject = baseObject;

this.assertGenesToMutatePresent( mutationGroup );
this.assertKeysToMutatePresent( mutationGroup );
function generateMutations( chromosome, mutationGroup ) {
function internalGenerateMutations( mutationGroup ) {
let mutationGroupClone = mutationGroup.slice(0);
let frameGene = mutationGroupClone.shift();
let frameObject = mutationGroupClone.shift();

let nextGeneration = [];
for( let i = frameGene.start; frameGene.checkRange(i); i = frameGene.step(i) ) {
for( let i = frameObject.start; frameObject.checkRange(i); i = frameObject.step(i) ) {
if( mutationGroupClone.length > 0 ) {
let subMutations = internalGenerateMutations( mutationGroupClone );
nextGeneration.push.apply( nextGeneration, subMutations.map( (submutation ) => {
submutation[ frameGene.gene ] = i;
submutation[ frameObject.targetKey ] = i;
return submutation;
}));
} else {
let mutation = {};
mutation[ frameGene.gene ] = i;
mutation[ frameObject.targetKey ] = i;
nextGeneration.push( mutation );
}
}
Expand All @@ -52,8 +54,8 @@ class Mutator {
};

this.mutations = (typeof( cullFunction ) === 'function') ?
generateMutations( chromosome, mutationGroup ).filter( cullFunction ) :
generateMutations( chromosome, mutationGroup );
generateMutations( baseObject, mutationGroup ).filter( cullFunction ) :
generateMutations( baseObject, mutationGroup );
}

/**
Expand All @@ -62,10 +64,10 @@ class Mutator {
*
* @param {mutationGroup} Mutation group that needs to be checked
*/
assertGenesToMutatePresent( mutationGroup ) {
assertKeysToMutatePresent( mutationGroup ) {
mutationGroup.forEach(element => {
if( !this.chromosome.hasOwnProperty( element.gene ) )
throw Error( `Missing gene ${ element.gene }` );
if( !this.baseObject.hasOwnProperty( element.targetKey ) )
throw Error( `Missing gene ${ element.targetKey }` );
});
}
};
Expand Down
12 changes: 6 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ let {expect} = require('chai');
let Mutator = require( '../index.js' );

describe( '#Mutator', function() {
let chromosome = {
let baseObject = {
a: 0,
b: 0,
c: 0,
d: "Unmutated property"
};

let mutationGroup = [
{ gene: "a", start: 5, checkRange: (i) => (i < 10 ), step: (i) => i+1 },
{ gene: "b", start: 10, checkRange: (i) => ( i < 20 ), step: (i) => i+2 },
{ gene: "c", start: -10, checkRange: (i) => ( i < 0 ), step: (i) => i+1 }
{ targetKey: "a", start: 5, checkRange: (i) => (i < 10 ), step: (i) => i+1 },
{ targetKey: "b", start: 10, checkRange: (i) => ( i < 20 ), step: (i) => i+2 },
{ targetKey: "c", start: -10, checkRange: (i) => ( i < 0 ), step: (i) => i+1 }
];

it( 'should leave unmutated properties untouched', function() {
let expectedTotal = 5 * 5 * 10;
let mutationBag = new Mutator( chromosome, mutationGroup );
let mutationBag = new Mutator( baseObject, mutationGroup );
let result = 0;
mutationBag.mutations.forEach(element => {
if( element.d === 'Unmutated property' ) result++;
Expand All @@ -30,7 +30,7 @@ describe( '#Mutator', function() {

// TODO: Can do better on this test
it( 'should filter out unwanted mutations', function() {
let mutationBag = new Mutator( chromosome, mutationGroup, (mutation) => {
let mutationBag = new Mutator( baseObject, mutationGroup, (mutation) => {
return( mutation.a >= 7 )
});

Expand Down

0 comments on commit 2e5afb9

Please sign in to comment.