Created
February 1, 2012 17:46
-
-
Save line-o/1718289 to your computer and use it in GitHub Desktop.
isArrayOf(array2test, type2match)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var typeUtils = | |
literals = ['boolean', 'string', 'number', 'object'], | |
types2test = literals.concat([new RegExp(/asd/), new Number(1), new String('#')]), | |
testArrays = [ | |
[true, false, true], | |
[1, 2, 3], | |
['a', 'b', 'c'], | |
[/sdf/, /asdf/, /asf/], | |
[{}, {}, {}], | |
[new String('a'), new String('b'), new String('c')], | |
[new Number(1), new Number(2), new Number(3)] | |
], | |
results = []; | |
function testIsArrayOf (array, types) { | |
return { | |
'tested':array.toString(), | |
'results':types.map(function(type, idx) { | |
return isArrayOf(array, type)})} | |
} | |
function renderResults (results, types) { | |
var sep = ' | ', | |
maxL = results.reduce(function (prev, curr, idx, array) { | |
var len = curr.tested.toString().length | |
return ((prev > len) ? prev : len)}), | |
rows = [renderTHead(types)] | |
function pad (str, len) { | |
var ret = str, pad = len - str.length | |
for (i = pad - 1; i >= 0; i--) ret += ' ' | |
return ret | |
} | |
function renderTHead (cols) { | |
return [pad('title', maxL)].concat(cols).join(sep) | |
} | |
function renderResHead (tested) { | |
var pad = maxL - tested.length, | |
rowHead = tested, i | |
for (i = pad - 1; i >= 0; i--) rowHead += ' ' | |
return rowHead | |
} | |
function renderResult (el) { | |
var rowParts = [renderResHead(el.tested)], | |
rowRes = el.results.map(function ( el) { | |
return (el === 1 ? 'x':' ')}) | |
return rowParts.concat(rowRes).join(sep) | |
} | |
return rows.concat(results.map(renderResult)) | |
} | |
//run tests | |
results = testArrays.map(function (arr){ | |
return testIsArrayOf(arr, types2test)}) | |
console.log(renderResults(results, types2test)) | |
//module.exports = isArrayOf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TU = require('./TypeUtils'), | |
types2test = TU.LITERALS.concat([new RegExp(/asd/), new Number(1), new String('#')]), | |
testArrays = [ | |
[true, false, true], | |
[1, 2, 3], | |
['a', 'b', 'c'], | |
[/sdf/, /asdf/, /asf/], | |
[{}, {}, {}], | |
[new String('a'), new String('b'), new String('c')], | |
[new Number(1), new Number(2), new Number(3)] | |
], | |
results = []; | |
function testIsArrayOf (array, types) { | |
return { | |
'tested':array.toString(), | |
'results':types.map(function(type, idx) { | |
return TU.isArrayOf(type, array)})} | |
} | |
function renderResults (results, types) { | |
var sep = ' | ', | |
maxLen = results.reduce(function (prev, curr, idx, array) { | |
var len = curr.tested.toString().length | |
return ((prev > len) ? prev : len)}), | |
rows = [renderTHead(types)] | |
function pad (str) { | |
var ret = str, | |
pad = maxLen - str.length | |
for (i = pad - 1; i >= 0; i--) ret += ' ' | |
return ret | |
} | |
function renderTHead (cols) { | |
function firstLetter(el) { return TU.getConstructor(el).substring(0,1) } | |
return [pad('title')] | |
.concat(cols.map(firstLetter)) | |
.join(sep) | |
} | |
function renderResult (el) { | |
var rowParts = [pad(el.tested)], | |
rowRes = el.results.map(function (el) { | |
return (el === 1 ? 'x':' ')}) | |
return rowParts.concat(rowRes).join(sep) | |
} | |
return rows.concat(results.map(renderResult)) | |
} | |
//run tests | |
results = testArrays.map(function (arr){ | |
return testIsArrayOf(arr, types2test)}) | |
console.log(renderResults(results, types2test)) | |
//module.exports = isArrayOf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var literals = ['boolean', 'string', 'number', 'object'] | |
function extractConstructorName (o) { | |
var s = o.constructor.toString() | |
return s.substring(9, s.indexOf('(')) //'function '...'(' | |
} | |
function isArrayOf (type, array) { | |
var reduce, test | |
constructorMatch = extractConstructorName(type) | |
//currying | |
if (array == undefined) | |
return function (a) { return isArrayOf(type, a) } | |
//compile testing functions | |
function testLiteral (o) { | |
return (typeof o == type)} | |
function testPrototype (o) { | |
return (extractConstructorName(o) == constructorMatch)} | |
//choose testing function and run test | |
test = (literals.indexOf(type) >= 0) ? testLiteral : testPrototype | |
return array.reduce(function (prev, curr) { | |
return (prev & test(curr))}, true) | |
} | |
TypeUtils = { | |
getConstructor : extractConstructorName, | |
isArrayOf : isArrayOf, | |
isArrayOfStrings : isArrayOf('string'), | |
LITERALS : literals | |
}; | |
module.exports = TypeUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var literals = ['boolean', 'string', 'number', 'object'], | |
types2test = literals.concat([new RegExp(/asd/), new Number(1), new String('#')]), | |
testArrays = [ | |
[true, false, true], | |
[1, 2, 3], | |
['a', 'b', 'c'], | |
[/sdf/, /asdf/, /asf/], | |
[{}, {}, {}], | |
[new String('a'), new String('b'), new String('c')], | |
[new Number(1), new Number(2), new Number(3)] | |
], | |
results = []; | |
function extractConstructorName (o) { | |
var s = o.constructor.toString() | |
return s.substring(9, s.indexOf('(')) //'function '...'(' | |
} | |
function isArrayOf (array, type) { | |
var reduce, test | |
constructorMatch = extractConstructorName(type) | |
//compile testing functions | |
function testLiteral (o) { | |
return (typeof o == type)} | |
function testPrototype (o) { | |
return (extractConstructorName(o) == constructorMatch)} | |
//choose testing function and run test | |
test = (literals.indexOf(type) >= 0) ? testLiteral : testPrototype | |
return array.reduce(function (prev, curr, idx, array) { | |
return (prev & test(curr))}, true) | |
} | |
function testIsArrayOf (array, types) { | |
return { | |
'tested':array.toString(), | |
'results':types.map(function(type, idx) { | |
return isArrayOf(array, type)})} | |
} | |
function renderResults (results, types) { | |
var sep = ' | ', | |
maxL = results.reduce(function (prev, curr, idx, array) { | |
var len = curr.tested.toString().length | |
return ((prev > len) ? prev : len)}), | |
rows = [renderTHead(types)] | |
function pad (str, len) { | |
var ret = str, pad = len - str.length | |
for (i = pad - 1; i >= 0; i--) ret += ' ' | |
return ret | |
} | |
function renderTHead (cols) { | |
return [pad('title', maxL)] | |
.concat(cols.map(function (el){return el.splice(1)})) | |
.join(sep) | |
} | |
function renderResult (el) { | |
var rowParts = [pad(el.tested)], | |
rowRes = el.results.map(function ( el) { | |
return (el === 1 ? 'x':' ')}) | |
return rowParts.concat(rowRes).join(sep) | |
} | |
return rows.concat(results.map(renderResult)) | |
} | |
//run tests | |
results = testArrays.map(function (arr){ | |
return testIsArrayOf(arr, types2test)}) | |
console.log(renderResults(results, types2test)) | |
//module.exports = isArrayOf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment