Last active
August 29, 2015 14:18
-
-
Save web20opensource/a567a849921d59821d18 to your computer and use it in GitHub Desktop.
mapping with map js javascript
This file contains hidden or 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
console.time('mapping in js'); | |
var isAType = {}; | |
isAType._array = "[object Array]"; | |
isAType._function = "[object Function]"; | |
isAType._string = "[object String]"; | |
isAType.isFunction = function(fn) { | |
return Object.prototype.toString.call(fn) === this._function; | |
} | |
isAType.isArray = function(arr){ | |
return Object.prototype.toString.call(arr) === this._array; | |
} | |
isAType.isString = function(arg){ | |
return Object.prototype.toString.call(arg) === this._string; | |
} | |
var _map = function (callBack,optionalThis){ | |
//by now assuming this is the Array | |
if (isAType.isArray(this)){ | |
var mapped = []; | |
var originalArray = this; | |
} | |
else if (isAType.isString(this)){ | |
var mapped = []; | |
var originalArray = this.split(''); | |
}else | |
return {'error':void(0)}; | |
var howManyArgs = arguments.length; | |
if (_map.length === howManyArgs){ | |
console.log("we have all the args"); | |
} | |
if ( isAType.isFunction( callBack) ){ | |
if (optionalThis){ | |
callBack.call( optionalThis , originalArray[i], i, originalArray); | |
} | |
for (var i = 0 ; i<originalArray.length; i++){ | |
mapped[i] = callBack.call(originalArray,originalArray[i], i, originalArray); | |
} | |
return mapped; | |
} | |
}; | |
Array.prototype._map = _map; | |
//real map function | |
function _assert(what,describe){ | |
if (what){ | |
console.log("%c"+describe + "is true",'color:green'); | |
} | |
else{ | |
console.log("c%"+describe + "is false",'color:red'); | |
} | |
}; | |
var _r1 = ['1','2','3'].map(Number); | |
_assert( isAType.isArray(_r1)&&_r1.length===3&&Number.isFinite(_r1[0]) , "Real map is mapping! " ) | |
//fake _map function | |
var _f1 = ["1","2","3"]._map(Number); | |
_assert( isAType.isArray(_f1)&&_f1.length===3&&Number.isFinite(_f1[2]) , "f@ke map is m@pping! ;) " ) | |
//real map function | |
var _r2 = ['1','2','3'].map(function(x){return Math.pow(x,2);}); | |
_assert( isAType.isArray(_r2)&&_r2.length===3&&Number.isFinite(_r2[0]) , "Real map is mapping! again " ) | |
//fake _map function | |
var _f2 = ['1','2','3']._map(function(x){return Math.pow(x,2);}); | |
_assert( isAType.isArray(_f2)&&_f2.length===3&&Number.isFinite( _f2[1]), "fake m@p is mapping! again " ) | |
//mozilla developer network example | |
var kvArray = [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}]; | |
var myCallBack = function(obj){ | |
var rObj = {}; | |
rObj[obj.key] = obj.value; | |
return rObj; | |
}; | |
var reformattedArray = kvArray.map(myCallBack); | |
_assert( isAType.isArray(reformattedArray)&&reformattedArray.length===3&&typeof reformattedArray[0] === "object" , "Real map is mapping! other one "); | |
var fakeformattedArray = kvArray._map(myCallBack); | |
_assert( isAType.isArray(fakeformattedArray)&&fakeformattedArray .length===3&&typeof fakeformattedArray [0] === "object" , " Fake map is mapping! other one ;) "); | |
var str = '12345'; | |
var rev = [].map.call(str, function(x) { | |
return x; | |
}).reverse().join(''); | |
_assert(rev==='54321',"real map is reversing!") | |
var _rev = []._map.call(str, function(x) { | |
return x; | |
}).reverse().join(''); | |
_assert(rev==='54321',"f@ke map is reversing!") | |
console.timeEnd('mapping in js'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment