This is a fix for the published but broken kata
function isArray(arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
};
function runInVM(str) {
require("vm").runInNewContext(str, { isArray:isArray, Test:Test });
}
Array.isArray = function(arr) {
if(wasCallerFrom(arguments.callee, 'isArray')) {
throw 'Array.isArray() is not available, sorry.'
}
function wasCallerFrom(args, from) {
if(args.caller != null)
try { return args.caller.name === from ? true : wasCallerFrom(args.caller, from); }
catch(e) { return false; }
return false;
}
};
runInVM(`
Test.describe('Tests', _ => {
Test.expect(isArray([]), "isArray([]) returned false");
Test.expect(isArray([1]), "isArray([1]) returned false");
Test.expect(isArray(new Array()), "isArray(new Array()) returned false");
Test.expect(isArray(Array.prototype), "isArray(Array.prototype) returned false");
Test.expect(!isArray(), "isArray() returned true");
Test.expect(!isArray({}), "isArray({}) returned true");
Test.expect(!isArray(null), "isArray(null) returned true");
Test.expect(!isArray(undefined), "isArray(undefined) returned true");
Test.expect(!isArray(0), "isArray(0) returned true");
Test.expect(!isArray(1), "isArray(1) returned true");
Test.expect(!isArray('Array') , "isArray('Array') returned true");
Test.expect(!isArray('[]'), "isArray('[]') returned true");
Test.expect(!isArray(true), "isArray(true) returned true");
Test.expect(!isArray(false), "isArray(false) returned true");
Test.expect(!isArray({ __proto__ : Array.prototype }), "isArray({ __proto__ : Array.prototype }) returned true");
Test.expect(!isArray({length: 0}), "isArray({length: 0}) returned true");
Test.expect(!isArray(function() {}), "isArray(function() {}) returned true");
Test.expect(!isArray('[object Array]'), "isArray('[object Array]') returned true");
});
`)