-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. isArrayLike 2. map, each 3. bloop
- Loading branch information
Showing
7 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// bloop: bind loop 를 통해 함수를 만들기 | ||
|
||
var _ = require("./underscore.js"); | ||
|
||
function bloop(new_data, body) { | ||
return function(data, iteratee) { | ||
var result = new_data(data); | ||
if (_.isArrayLike(data)) { | ||
for (var i = 0, len = data.length; i < len; i++) { | ||
body(iteratee(data[i], i, data), result); | ||
} | ||
} else { | ||
for (var key in data) { | ||
if (data.hasOwnProperty(key)) { | ||
body(iteratee(data[key], key, data), result); | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
var map = bloop(function() { | ||
return []; | ||
}, function(val, obj) { | ||
return obj.push(val); | ||
}); | ||
|
||
console.log(map([ 1, 2, 3 ], function(v){ | ||
return v * 2; | ||
})); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var _ = require("./underscore.js"); | ||
|
||
var each = function(data, iteratee) { | ||
if (_.isArrayLike(data)) { | ||
for (var i = 0, len = data.length; i < len; i++) { | ||
iteratee(data[i], i , data); | ||
} | ||
} else { | ||
for (var key in data) { | ||
if (data.hasOwnProperty(key)) iteratee(data[key], key, data); | ||
} | ||
} | ||
|
||
return data; | ||
}; | ||
|
||
each([ 1, 2, 3 ], console.log); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// supporters | ||
var MAX_ARRAY_INDEX = Number.MAX_SAFE_INTEGER; | ||
|
||
// getLength | ||
function getLength(list) { | ||
return list == null ? void 0 : list.length; | ||
} | ||
|
||
// isArrayLike | ||
var isArrayLike = function(list) { | ||
var length = getLength(list); | ||
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX; | ||
}; | ||
// // supporters | ||
|
||
var map = function(data, iteratee) { | ||
var new_list = []; | ||
if (isArrayLike(data)) { | ||
for (var i = 0, len = data.length; i < len; i++) { | ||
new_list.push(iteratee(data[i], i, data)); | ||
} | ||
} else { | ||
for (var key in data) { | ||
if (data.hasOwnProperty(key)) new_list.push(iteratee(data[key], key, data)); | ||
} | ||
|
||
} | ||
return new_list; | ||
}; | ||
|
||
var arr1 = map([1, 2, 3], function(v){ | ||
return v * 2; | ||
}); | ||
var arr2 = map({a: 3, b: 2, c: 1}, function(v){ | ||
return v * 2; | ||
}); | ||
|
||
console.log(arr1); | ||
console.log(arr2); | ||
|
||
// underscore.js에서는 세 번째 인자로 iteratee에서 사용할 this를 전달할 수 있지만 | ||
// this를 적용한 새로운 함수를 만드는 것은 작은 부하가 있다. | ||
// 그 함수를 실행할 때도 부하가 생긴다. 반복하는 횟수가 많을수록 부하도 쌓이므로 | ||
// 개발자에게 iteratee에서의 this 다루기를 위임하고 최대한 깨끗한 함수만 받도록 하자. | ||
|
||
var arr3 = map([1, 2, 3], function(v){ | ||
return v * this; | ||
}.bind(5)); | ||
|
||
console.log(arr3); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
(function() { | ||
var _ = function(){}; | ||
|
||
var root = this; | ||
|
||
var _ = function(obj) { | ||
if (obj instanceof _) return obj; | ||
if (!(this instanceof _)) return new _(obj); | ||
this._wrapped = obj; | ||
}; | ||
|
||
// Export the Underscore object for **Node.js**, with | ||
// backwards-compatibility for the old `require()` API. If we're in | ||
// the browser, add `_` as a global object. | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = _; | ||
} | ||
exports._ = _; | ||
} else { | ||
root._ = _; | ||
} | ||
|
||
|
||
var MAX_ARRAY_INDEX = Number.MAX_SAFE_INTEGER; | ||
|
||
// getLength | ||
function getLength(list) { | ||
return list == null ? void 0 : list.length; | ||
} | ||
|
||
// isArrayLike | ||
_.isArrayLike = function(list) { | ||
var length = getLength(list); | ||
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX; | ||
}; | ||
|
||
// identity | ||
_.identity = function(v) { | ||
return v; | ||
}; | ||
_.idtt = _.identity; | ||
|
||
// args | ||
_.args0 = _.identity; | ||
_.args1 = function(a, b) { | ||
return b; | ||
}; | ||
|
||
// values | ||
_.values = function(list) { | ||
return _.map(list, _.identity); | ||
}; | ||
|
||
// keys | ||
_.keys = function(list) { | ||
return _.map(list, _.args1); | ||
}; | ||
|
||
// map | ||
_.map = function(data, iteratee) { | ||
var new_list = []; | ||
if (_.isArrayLike(data)) { | ||
for (var i = 0, len = data.length; i < len; i++) { | ||
new_list.push(iteratee(data[i], i, data)); | ||
} | ||
} else { | ||
for (var key in data) { | ||
if (data.hasOwnProperty(key)) new_list.push(iteratee(data[key], key, data)); | ||
} | ||
|
||
} | ||
return new_list; | ||
}; | ||
|
||
// each | ||
_.each = function(data, iteratee) { | ||
if (_.isArrayLike(data)) { | ||
for (var i = 0, len = data.length; i < len; i++) { | ||
iteratee(data[i], i , data); | ||
} | ||
} else { | ||
for (var key in data) { | ||
if (data.hasOwnProperty(key)) iteratee(data[key], key, data); | ||
} | ||
} | ||
|
||
return data; | ||
}; | ||
}.call(this)); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var _ = require("./underscore.js"); | ||
|
||
// identity | ||
var identity = function(v) { | ||
return v; | ||
}; | ||
var idtt = _.identity; | ||
|
||
// args | ||
var args0 = _.identity; | ||
var args1 = function(a, b) { | ||
return b; | ||
}; | ||
|
||
// values | ||
var values = function(list) { | ||
return _.map(list, _.identity); | ||
}; | ||
|
||
// keys | ||
var keys = function(list) { | ||
return _.map(list, _.args1); | ||
}; | ||
|
||
console.log(values({ id: 5, name: "MK", age: 30 })); | ||
console.log(keys({ id: 5, name: "MK", age: 30 })); | ||
console.log(keys([ 1, 2, 3 ])); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// _.each, _.map 등에서 사용하는 객체는 2가지에서 4가지 정도다 | ||
// {}, [], arguments, ArrayLike | ||
|
||
// 1. {} | ||
var d1 = { name: 'MK', age: 30 }; | ||
|
||
// 2. [] | ||
var d2 = [ 1, 2, 3 ]; | ||
|
||
// 3. arguments | ||
var d3 = function() { | ||
return arguments; | ||
}(1, 2, 3); | ||
|
||
// 4. ArrayLike | ||
//var d4 = $("div"); | ||
var d5 = { length: 3 }; | ||
d5[0] = 1, d5[1] = 2, d5[2] = 3; | ||
var d6 = "hi"; | ||
|
||
|
||
function getLength(list) { | ||
return list == null ? void 0 : list.length; | ||
} | ||
|
||
var isArrayLike = function(list) { | ||
var MAX_ARRAY_INDEX = Number.MAX_SAFE_INTEGER; | ||
var length = getLength(list); | ||
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX; | ||
}; |