view source

source code
/*  */

if (K345.requireScript) {
	K345.requireScript('is', 'delement', 'object');
}

var reg = new RegExp('(^|\\s)js_on(\\s|$)', 'i');

if (!reg.test(document.documentElement.className)) {
	document.documentElement.className += ' js_on';
}

window.onload = test_is;

function test_is() {
	'use strict';

	/* comparsion ctypes */
	var ctypes = {
		'string': 3, 'number': 3, 'int': 3, 'float': 'flt', 'object': 3, 'boolean': 4,
		'null': 4, 'nan': 3, 'infinity': 3, 'undefined': 'undf', 'array': 3, 'node': 4,
		'text': 4, 'list': 4, 'date': 4, 'regexp': 3, 'function': 3
	};

	/* val: values to test, type: expected type, desc: row description in table */
	var ctests = [{
		val: '',
		type: 'string',
		desc: '""'
	}, {
		val: 'test',
		type: 'string',
		desc: '"test"'
	},{
		val: new String('foo bar'),
		type: 'object',
		stype: 'string',
		desc: 'new String<br>("foo bar")'
	}, {
		val: 0,
		type: ['number', 'int'],
		desc: '0'
	}, {
		val: 1,
		type: ['number', 'int'],
		desc: '1'
	}, {
		val: 3.1415926,
		type: ['number', 'float'],
		desc: '3.1415926'
	}, {
		val: 1E3,
		type: ['number', 'int'],
		desc: '1E3'
	}, {
		val: 0x3B,
		type: ['number', 'int'],
		desc: '0x3B'
	}, {
		val: Math.E,
		type: ['number', 'float'],
		desc: 'Math.E'
	},{
		val: new Number(4),
		type: 'object',
		stype: 'number',
		desc: 'new Number(4)'
	}, {
		val: {},
		type: 'object',
		desc: '{&thinsp;}'
	}, {
		val: {hi: 'm'},
		type: 'object',
		desc: '{hi: "m"}'
	}, {
		val: Object.create({a:1}),
		type: 'object',
		desc: 'Object.create<br>({a:1})'
	}, {
		val: Object.create(null),
		type: 'object',
		desc: 'Object.create<br>(null)'
	}, {
		val: true,
		type: 'boolean',
		desc: 'true'
	}, {
		val: false,
		type: 'boolean',
		desc: 'false'
	},{
		val: new Boolean(false),
		type: 'object',
		stype: 'boolean',
		desc: 'new Boolean<br>(false)'
	}, {
		val: null,
		type: 'null',
		desc: 'null'
	}, {
		val: NaN,
		type: 'nan',
		desc: 'NaN'
	}, {
		val: Infinity,
		type: 'infinity',
		desc: 'Infinity'
	}, {
		val: undefined,
		type: 'undefined',
		desc: 'undefined'
	}, {
		val: [],
		type: 'array',
		desc: '[&thinsp;]'
	}, {
		val: [2, 4],
		type: 'array',
		desc: '[2, 4]'
	}, {
		val: document.body,
		type: 'node',
		desc: 'document.body'
	}, {
		val: document.createElement('b'),
		type: 'node',
		desc: 'createElement<br>("b")'
	}, {
		val: document.createTextNode('hi'),
		type: 'text',
		desc: 'createTextNode<br>("hi")'
	}, {
		val: document.getElementsByTagName('span'),
		type: 'list',
		desc: '.getEl&hellip;TagName<br>("span")'
	}, {
		val: document.links,
		type: 'list',
		desc: 'document<br>.links'
	}, {
		val: new Date(10, 11, 12),
		type: 'date',
		desc: 'new Date<br>(10, 11, 12)'
	}, {
		val: (/^[a-z]+/i),
		type: 'regexp',
		desc: '/^[a-z]+/&thinsp;i'
	}, {
		val: new RegExp("\\d+", "gi"),
		type: 'regexp',
		desc: 'new RegExp<br>("\\d+","gi")'
	}, {
		val: function () {},
		type: 'function',
		desc: 'function&thinsp;()<br>{&hellip;}'
	}, {
		val: parseInt,
		type: 'function',
		desc: 'parseInt'
	}];

	/* will be set to true on false positive results */
	var failed = false;

	/* images for results*/
	var img_true, img_false;
	img_true = {
		element: 'img',
		src: 'http://res.' + myHost + '/img/misc/plus-green-20px.png',
		alt: 'true'
	}
	img_false = {
		element: 'img',
		src: 'http://res.' + myHost + '/img/misc/minus-red-20px.png',
		alt: 'false'
	}

	/* loop */
	var rv, i, j, prop, cty;

	/* HTML table */
	var mCell, tRows = [], cmpNames = [], tHead, tFoot, tCells, cls, thcls;

	/* column description cells */
	for (prop in ctypes) {
		cmpNames.push({
			element: 'th',
			text: (typeof ctypes[prop] === 'string')
				? ctypes[prop]
				: prop.substring(0, ctypes[prop]),
			title: prop
		});
	}

	/* compare every value with every comparsion mode and create table */
	for (i = 0; i < ctests.length; i++) {
		cty = ctests[i].type;
		thcls = (Array.isArray(cty)) ? '.multi' : '';
		tCells = [{ /* row description */
			element: 'th' + thcls,
			child: [{
				element: 'span.desc',
				title: 'test item',
				html: ctests[i].desc
			}, {
				element: 'span.type',
				title: 'return value of typeof operator',
				text: typeof ctests[i].val
			}, {
				element: 'span.type',
				title: 'return value of toString()',
				text: Object.prototype.toString.call(ctests[i].val)
			}]
		}];
		j = 0;
		for (prop in ctypes) {
			rv = K345.is(ctests[i].val, prop); /* compare */
			if (rv === true) {
				if (cty === prop || Array.isArray(cty) && cty.indexOf(prop) > -1) {
					cls = '.true'
				}
				else {
					cls = '.error'
					failed = true;
				}
			}
			else {
				if (cty === prop || Array.isArray(cty) && cty.indexOf(prop) > -1) {
					cls = '.error'
					failed = true;
				}
				else {
					cls = '';
				}
			}
			tCells.push({
				element: 'td' + cls,
				child: (rv) ? img_true : img_false
			});
			j++;
		}
		tRows.push({
			element: 'tr',
			child: tCells
		});
	}

	tHead = [{
		element: 'th.first',
		child: [{
			element: 'span.down',
			text: 'val'
		}, {
			element: 'span.up',
			text: 'comp'
		}]
	}].concat(cmpNames);

	tFoot = [{
		element: 'th.status',
		child: [{
			text: 'Status:'
		}, {
			element: 'img',
			src: 'http://res.' + myHost + '/img/emot/' + ((failed) ? 'sad' : 'grin') + '.png',
			alt: 'emoticon',
			title: (failed) ? 'fucked up' : 'all well'
		}]
	}].concat(cmpNames);

	var cmp = K345.dAppend(
		'tablewrap',
		{
			element: 'table#cmp',
			border: '1',
			child: [{
				element: 'thead',
				child: {
					element: 'tr',
					child: tHead
				}
			}, {
				element: 'tfoot',
				child: {
					element: 'tr',
					child: tFoot
				}
			}, {
				element: 'tbody',
				child: tRows
			}]
		},
		K345.DAPPEND_REPLACE
	);
}

/* */