Skip to content

Latest commit

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Function Signatures

Flexible argument passing for functions.

DOOM

You're a mathematical genius assigned to build devastating high density plasma rifles to help protect the human race against the alien invasion. You're writing the software for the gun and need to create a function called euclideanDistance so that the rifle can employ a highly accurate auto-targeting system. This function will calculate the distance between 2 points in a three-dimensional space and will be called from many places in the code. For convenience's sake, you'd like to be able to pass arguments in a few different formats. You decide to use @razic's function-signatures component because you know it's well tested and means you don't have to write stupid error checking tests for the many different ways you may accidentally call your function with an incorrect function signature.

Installation

component install razic/function-signatures

API

// Require the functionvarfunctionSignatures=require('function-signatures');// Initialize a `functionSignatures` objectvarsignatures=newfunctionSignatures({"six numbers": function(x1,y1,z1,x2,y2,z2){returntypeofx1==='number'&&typeofy1==='number'&&typeofz1==='number'&&typeofx2==='number'&&typeofy2==='number'&&typeofz2==='number';},"two objects, each with x, y and z properties": function(a,b){returntypeofa.x==='number'&&typeofa.y==='number'&&typeofa.z==='number'&&typeofb.x==='number'&&typeofb.y==='number'&&typeofb.z==='number';},"two arrays, each having three numbers": function(a,b){returnainstanceofArray&&binstanceofArray&&a.length===3&&b.length===3&&typeofa[0]==='number'&&typeofa[1]==='number'&&typeofa[2]==='number'&&typeofb[0]==='number'&&typeofb[1]==='number'&&typeofb[2]==='number';},"one array with six numbers": function(ab){returnabinstanceofArray&&ab.length===6&&typeofab[0]==='number'&&typeofab[1]==='number'&&typeofab[2]==='number'&&typeofab[3]==='number'&&typeofab[4]==='number'&&typeofab[5]==='number';}});// Add event handlers to normalize the argumentssignatures.on("six numbers",function(x1,y1,z1,x2,y2,z2){this.x1=x1;this.y1=y1;this.z1=z1;this.x2=x2;this.y2=y2;this.z2=z2;});signatures.on("two objects, each with x, y and z properties",function(a,b){this.x1=a.x;this.y1=a.y;this.z1=a.z;this.x2=b.x;this.y2=b.y;this.z2=b.z;});signatures.on("two arrays, each having three numbers",function(a,b){this.x1=a[0];this.y1=a[1];this.z1=a[2];this.x2=b[0];this.y2=b[1];this.z2=b[2];});signatures.on("one array with six numbers",function(ab){this.x1=ab[0];this.y1=ab[1];this.z1=ab[2];this.x2=ab[3];this.y2=ab[4];this.z2=ab[5];});// Write your methodfunctioneuclideanDistance(){// Normalize the argumentssignatures.normalize(arguments);// Use the normalized argumentsvardx=signatures.x2-signatures.x1;vardy=signatures.y2-signatures.y1;vardz=signatures.z2-signatures.z1;// ProfitreturnMath.sqrt(Math.pow(dx,2)+Math.pow(dy,2)+Math.pow(dz,2));}

License

MIT

About

Flexible argument passing for functions.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages