Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function typeDecorator(func, arr) {
- if (arguments.length > 2) {
- arr = [].slice.call(arguments, 1);
- }
- //Resolve arr. Make possible to pass both array of settings and list of them.
- for (var val of arr) {
- if (!Array.isArray(val)) {
- throw "Wrong type of argument(s).\n All arguments must be arrays.";
- }
- }
- return function() {
- while(arguments.length > arr.length) {
- arr.push([]);
- }
- for (var i = 0;i<arguments.length;i++) {
- for (var j = 0;j<arr[i].length;j++) {
- if (!arr[i][j](arguments[i]) && arr[i][j].length != 0) {
- return false;
- }
- }
- }
- return func.apply(this, arguments);
- }
- }
- //Some predefined stuff.
- function isNumber(value) {
- return typeof value == 'number';
- }
- function isString(value) {
- return typeof value == 'string';
- }
- function isSmall(value) {
- return value < 100;
- }
- function isRound(value) {
- return !(value % 10);
- }
- //Example.
- var sum = function(a,b,c) {
- return a+b+c;
- }
- sum = typeDecorator(sum, [isSmall, isNumber],[isNumber],[isRound]);
- //Excessive Arguments and Tests are ok because of While;
- //Each set of tests(Even one test) for each argument should be an array.
- //The whole thing could be both list of arrays and two-dimensional array.
- console.log(sum(10,501, 180)); //511
- console.log(sum(10,501, 181)); //false
- console.log(sum(101,501, 180)); //false
- console.log(sum("101",501, 180)); //false
- console.log(sum(101,"501", 180)); //false
Add Comment
Please, Sign In to add comment