A Function Argument Type Validator in JavaScript

The JavaScript code below implements simple type checking on function arguments. To see it in action, copy it into an .html file and open that file in a web browser that runs JavaScript, with the debugging console open so the log messages can be seen.

It still needs a little work. Notably, I need to make sure that the “this” value in the called functions maintain the appropriate values.


<html>
<body>
<script type="text/javascript">

// main

function main()
{
	var greeter = new Greeter();

	// The following calls should pass type check.

	console.log(greeter.personGreetOnBirthday("Marcia", 13));
	console.log(greeter.personGreetOnBirthday("Carol", 41, false));
	console.log(greeter.personGreetOnBirthday("Amos", 100, true));

	// The following calls should fail type check.

	var didFailAsExpected = false;	
	var validationWorkingMessage = "Failed type check as expected.";	
	var validationErrorMessage = "Should have failed type check!";
	
	try
	{
		greeter.personGreetOnBirthday("Marcia", "Marcia", "Marcia");
	}
	catch (error)
	{
		didFailAsExpected = true;
	}
	if (didFailAsExpected == true)
	{
		console.log(validationWorkingMessage);
	}
	else
	{
		console.log(validationErrorMessage);
	}
	
	try
	{
		greeter.personGreetOnBirthday("Carol", 41, "Yes I AM aging gracefully!");
	}
	catch (error)
	{
		didFailAsExpected = true;
	}
	if (didFailAsExpected == true)
	{
		console.log(validationWorkingMessage);
	}
	else
	{
		console.log(validationErrorMessage);
	}
	
	try
	{
		greeter.personGreetOnBirthday(true, "Amos", 100);
	}
	catch (error)
	{
		didFailAsExpected = true;
	}
	if (didFailAsExpected == true)
	{
		console.log(validationWorkingMessage);
	}
	else
	{
		console.log(validationErrorMessage);
	}
}

// extensions

function FunctionExtensions()
{
	// extension class
}
{
	Function.prototype.typesCheck = function(parameterTypeNames)
	{
		var functionToValidate = this;
		
		var functionWithTypeChecks = function()
		{
			for (var i = 0; i < arguments.length; i++)
			{
				var argument = arguments[i];

				var argumentTypeName = argument.constructor.name;
				var parameterTypeName = parameterTypeNames[i];
				
				if (argumentTypeName != parameterTypeName)
				{
					throw "Unexpected type.";
				}
			}
			
			var thisFromCall = null; // todo
			var returnValue = functionToValidate.apply(thisFromCall, arguments);
		
			return returnValue;
		}
		
		return functionWithTypeChecks;
	}
}

// classes

function Greeter()
{
	// Do nothing.
}
{
	Greeter.prototype.personGreetOnBirthday = function
	(
		personName, ageInYears, isAgingGracefully
	)
	{
		var message;
		
		if (isAgingGracefully == true || ageInYears < 21)
		{
			message = "Happy birthday";
		}
		else
		{
			message = "Hello";
		}
		
		message = message + ", " + personName + "!";
				
		return message;
		
	}.typesCheck(["String", "Number", "Boolean"]);
	
}

// run

main();

</script>
<body>
</html>

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s