便å©æ©è½æºè¼ã®ã©ã¤ãã©ãªUnderscore.js
Javascriptã©ã¤ãã©ãªUnderscore.jsãããã便å©ã
ããã便å©ãªã®ã«ãããããããããã¾ãæ¥æ¬èªã®æ
å ±ããªãã£ãã®ã§ãå
¬å¼ã®ããã¥ã¡ã³ããã¹ããªãã¥ã¢ã«æ訳ãã¦ã¿ãã
http://documentcloud.github.com/underscore/
Collection Functions
each
_.each(list, iterator, [context])
listã®è¦ç´ ãã¤ãã¬ã¼ãããããã¹ã¦ã®è¦ç´ ã¯iteratoré¢æ°ã«yieldããããiteratorã«ã¯(element, index, list)ã®3ã¤ã®å¼æ°ã渡ããããããlistãJavascript Objectã ã£ãå ´åã¯(value, key, list)ã«ãªãã
_.each([1, 2, 3], function(num){ alert(num); }); => alerts each number in turn... _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); }); => alerts each number in turn...
map
_.map(list, iterator, [context])
listã®ãã¹ã¦ã®è¦ç´ ãæä½é¢æ°(iterator)ã§ãããã³ã°ããæ°ããé åãä½ããããlistãJavascript Objectã ã£ãå ´åãiteratorã¸ã®å¼æ°ã¯(value, key, list)ã«ãªãã
_.map([1, 2, 3], function(num){ return num * 3; }); => [3, 6, 9] _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; }); => [3, 6, 9]
reduce
_.reduce(list, iterator, memo, [context])
listã®è¦ç´ ãï¼ã¤ã®å¤ã«éç´ãããmemoãåæå¤ãiteratorã¯ã¹ããããã¨ã«å¤ãè¿ããªããã°ãããªãã
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0); => 6
reduceRight
_.reduceRight(list, iterator, memo, [context])
å³æ¹åããã®reduceã
var list = [[0, 1], [2, 3], [4, 5]]; var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); => [4, 5, 2, 3, 0, 1]
detect
_.detect(list, iterator, [context])
ãªã¹ãã®è¦ç´ ã®ä¸ãããä¸çªæåã«çå¤ãã¹ã(iterator)ãééãããã®ãè¿ãã
var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); => 2
select
_.select(list, iterator, [context])
ãªã¹ãã®è¦ç´ ã®ä¸ãããçå¤ãã¹ã(iterator)ãééãããã®ãè¿ãã
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); => [2, 4, 6]
reject
_.reject(list, iterator, [context])
ãªã¹ãã®è¦ç´ ã®ä¸ãããçå¤ãã¹ã(iterator)ãééããªãã£ããã®ãè¿ãã
selectã®å対ã
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); => [1, 3, 5]
all
_.all(list, [iterator], [context])
ãªã¹ãã®è¦ç´ ããã¹ã¦çå¤ãã¹ã(iterator)ãééããå ´åãtrueãè¿ããããã§ãªãå ´åã¯falseãè¿ãã
iteratorãä¸ããããªãã£ãå ´åã代ããã«ççå¤ã使ç¨ãããã
_.all([true, 1, null, 'yes']); => false
any
_.any(list, [iterator], [context])
listã®è¦ç´ ã1ã¤ã§ãçå¤ãã¹ããééããå ´åãtrueãè¿ããããã§ãªãå ´åã¯falseãè¿ãã
_.any([null, 0, 'yes', false]); => true
include
_.include(list, value)
valueãlistã«å«ã¾ãã¦ããå ´åãtrueãè¿ããããã§ãªãå ´åã¯falseãè¿ããå¤ã®æ¯è¼ã¯===æ¼ç®åã§è¡ãããã
_.include([1, 2, 3], 3); => true
invoke
_.invoke(list, methodName, [*arguments])
listã®åè¦ç´ ã«å¯¾ãã¦methodNameã§æå®ããé¢æ°ãå®è¡ããã*argumentsã¯ãæå®ãããé¢æ°ã«å¼ã渡ãããã
_.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); => [[1, 5, 7], [1, 2, 3]]
pluck
_.pluck(list, propertyName)
mapã®ä¾¿å©ãã¼ã¸ã§ã³ãæå®ããããããã£åã«å¯¾å¿ããå¤ãéããã
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.pluck(stooges, 'name'); => ["moe", "larry", "curly"]
max
_.max(list, [iterator], [context])
listã®ä¸ã®æ大å¤ãæ±ãããiteratorã渡ãããå ´åãiteratorã®è¿ãå¤ãã©ã³ã¯ä»ãã®åºæºã¨ãã¦ä½¿ç¨ãããã
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.max(stooges, function(stooge){ return stooge.age; }); => {name : 'curly', age : 60};
min
_.min(list, [iterator], [context])
listã®ä¸ã®æå°å¤ãæ±ãããiteratorã渡ãããå ´åãiteratorã®è¿ãå¤ãã©ã³ã¯ä»ãã®åºæºã¨ãã¦ä½¿ç¨ãããã
var numbers = [10, 5, 100, 2, 1000]; _.min(numbers); => 2
sortBy
_.sortBy(list, iterator, [context])
åè¦ç´ ãiteratorã®è¿ãå¤ã§ã©ã³ã¯ä»ãããããã¨å ã«ã½ã¼ããããlistãè¿ãã
_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); }); => [5, 4, 6, 3, 1, 2]
sortedIndex
_.sortedIndex(list, value, [iterator])
äºåæ¢ç´¢ã§ãvalueãlistã®æ¿å ¥ãããã¹ãä½ç½®ã調ã¹ããããè¿ããiteratorã渡ãããå ´åãiteratorã®è¿ãå¤ãã©ã³ã¯ä»ãã®åºæºã¨ãã¦ä½¿ç¨ãããã
_.sortedIndex([10, 20, 30, 40, 50], 35); => 3
toArray
_.toArray(list)
list(ã¤ãã¬ã¼ãã§ãããã®ãªããªãã§ã)ãArrayã«å¤æãããå¼æ°ãå¤å½¢ããã®ã«ä¾¿å©ã
(function(){ return _.toArray(arguments).slice(0); })(1, 2, 3); => [1, 2, 3]
size
_.size(list)
listã«å«ã¾ãã¦ããè¦ç´ ã®æ°ãè¿ãã
_.size({one : 1, two : 2, three : 3}); => 3
Arrays
first
first_.first(array, [n])
é åã®ä¸çªç®ã®è¦ç´ ãè¿ããnãæå®ãããã¨ãå é ããnçªç®ã®è¦ç´ ã¾ã§ã®é åãè¿ãã
_.first([5, 4, 3, 2, 1]); => 5
rest
rest_.rest(array, [index])
å é ã®è¦ç´ ãåãé¤ããé åãè¿ããindexãæå®ããã¨ãindexçªç®ä»¥éã®è¦ç´ ãé åã¨ãã¦è¿ãã
_.rest([5, 4, 3, 2, 1]); => [4, 3, 2, 1]
last
_.last(array)
é åã®æå¾ã«ããè¦ç´ ãè¿ãã
_.last([5, 4, 3, 2, 1]); => 1
compact
_.compact(array)
ææ§ãªå¤ã§ããfalse, null, 0, "", undefinedãåãé¤ããé åãè¿ããéç ´å£çã
_.compact([0, 1, false, 2, '', 3]); => [1, 2, 3]
flatten
_.flatten(array)
å¤æ¬¡å é åãä¸æ¬¡å åãããéç ´å£çã
_.flatten([1, [2], [3, [[[4]]]]]); => [1, 2, 3, 4];
without
_.without(array, [*values])
valuesãåãé¤ããé
åãè¿ãã===æ¼ç®åã§æ¯è¼ããããéç ´å£çã
|
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); => [2, 3, 4]
uniq
_.uniq(array, [isSorted])
é åããéè¤ãåãé¤ãã===æ¼ç®åã§æ¯è¼ããããé åãã½ã¼ãããã¦ããå ´åã¯ãisSrotedãtrueã«ããã¨å¦çãæ©ããªããéç ´å£çã
_.uniq([1, 2, 1, 3, 1, 4]); => [1, 2, 3, 4]
intersect
_.intersect(*arrays)
è¤æ°ã®é åã®å ±ééåãè¿ãã
_.intersect([1, 2, 3], [101, 2, 1, 10], [2, 1]); => [1, 2]
zip
_.zip(*arrays)
è¤æ°ã®é åã®ãåãä½ç½®ã«ããè¦ç´ ããã¼ã¸ããã
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]); => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
indexOf
_.indexOf(array, value, [isSorted])
é åã«valueãå«ã¾ãã¦ããå ´åãvalueãä¸çªæåã«ç¾ããä½ç½®ãè¿ããå«ã¾ãã¦ããªãã£ãå ´åã¯-1ãè¿ããé åãã½ã¼ãããã¦ããå ´åã¯ãisSrotedãtrueã«ããã¨å¦çãæ©ããªãã
_.indexOf([1, 2, 3], 2); => 1
lastIndexOf
_.lastIndexOf(array, value)
é åã«valueãå«ã¾ãã¦ããå ´åãvalueãä¸çªæå¾ã«ç¾ããä½ç½®ãè¿ããå«ã¾ãã¦ããªãã£ãå ´åã¯-1ãè¿ãã
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2); => 4
range
_.range([start], stop, [step])
startããstopã¾ã§stepãã¤å¢å (ã¾ãã¯æ¸å°)ããæ´æ°ã®ãªã¹ããä½ããæä»çã
startã®ããã©ã«ãå¤ã¯0ãstepã®ããã©ã«ãå¤ã¯1ã
_.range(10); => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] _.range(1, 11); => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] _.range(0, 30, 5); => [0, 5, 10, 15, 20, 25] _.range(0, -10, -1); => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] _.range(0); => []
Function (uh, ahem) Functions
bind
_.bind(function, object, [*arguments])
ãªãã¸ã§ã¯ããé¢æ°ã«ãã¤ã³ããããé¢æ°ãå¼ã°ããã¨ããthisãobjectã®å¤ãã¨ãã¨ãããã¨ãã«ãªã¼åã
var func = function(greeting){ return greeting + ': ' + this.name }; func = _.bind(func, {name : 'moe'}, 'hi'); func(); => 'hi: moe'
bindAll
_.bindAll(object, [*methodNames])
è¤æ°ã®é¢æ°ããªãã¸ã§ã¯ãã«ãã¤ã³ããããã¤ãã³ããã³ãã©ã¼ã«é¢æ°ããã¤ã³ãããã¨ãã«ä¾¿å©ã
var buttonView = { label : 'underscore', onClick : function(){ alert('clicked: ' + this.label); }, onHover : function(){ console.log('hovering: ' + this.label); } }; _.bindAll(buttonView); jQuery('#underscore_button').bind('click', buttonView.onClick); => When the button is clicked, this.label will have the correct value...
memoize
_.memoize(function, [hashFunction])
é¢æ°ã®è¨ç®çµæããã£ãã·ã¥ãããã¡ã¢åãhasFunctionãæå®ãããå ´åãçµæãã¹ãã¢ããããã®ããã·ã¥ãã¼ã¨ãã¦å©ç¨ããããhasFunctionã®ããã©ã«ãå¤ã¯ä¸çªæåã®å¼æ°ã
var fibonacci = function(n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); }; var fastFibonacci = _.memoize(fibonacci);
delay
_.delay(function, wait, [*arguments])
setTimeoutã®ããã«ãããªç§å¾ã«é¢æ°ãå¼ã³åºããargumentsãæå®ããå ´åããããå¼ã³åºãããé¢æ°ã«å¼ã渡ãããã
var log = _.bind(console.log, console); _.delay(log, 1000, 'logged later'); => 'logged later' // Appears after one second.
defer
_.defer(function)
ã³ã¼ã«ã¹ã¿ãã¯ã空ã«ãªãã¾ã§ãé¢æ°ã®å¼ã³åºããé 延ããããdelay = 0ã§setTimeoutã使ãå ´åã¨åããè¤éãªè¨ç®ãè¡ã£ãããUIã¹ã¬ããããããã¯ããã«ã¾ã¨ã¾ã£ãHTMLã¬ã³ããªã³ã°ãè¡ãããã¨ãã«ä¾¿å©ã
_.defer(function(){ alert('deferred'); }); // Returns from the function before the alert runs.
throttle
_.throttle(function, wait)
ã¹ãããã«åãããé¢æ°ãè¿ããã©ãããããé¢æ°ã¯waitããªç§ã«å¤ãã¦ãä¸åº¦ããå®è¡ãããªããã¬ã¼ããå¶éãããã¤ãã³ããèµ·ããã®ã«ä¾¿å©ã
var throttled = _.throttle(updatePosition, 100); $(window).scroll(throttled);
debounce
_.debounce(function, wait)
é¢æ°ãå®è¡ããã¦ããwaitããªç§ãçµéããã¾ã§ãç¹°ãè¿ãã®å¼ã³åºããææ¢ãããä½ããã®å ¥åãã¹ãããããã¨ãã«å®è¡ãããæ¯ãèããå®è£ ãããã¨ãã«ä¾¿å©ã
var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout);
once
_.once(function)
ä¸åº¦ããå®è¡ãããªãé¢æ°ãä½æãããä¿®æ£ãããé¢æ°ã®äºåº¦ç®ä»¥éã®å¼ã³åºãã¯ããªãã®å½±é¿ãä¸ãããæåã«å¼ã³åºãããã¨ãã®è¿ãå¤ãè¿ããåæåå¦çã«ä¾¿å©ã
var initialize = _.once(createApplication); initialize(); initialize(); // Application is only created once.
wrap
_.wrap(function, wrapper)
æåã®å¼æ°ã§æå®ããé¢æ°ãwrapperé¢æ°ã®ä¸ã«ã©ãããããwrapperé¢æ°ã«ã¯æå®ãããé¢æ°ã®å®è¡ã®åå¾ã§è¡ãããå¦çãè¨è¿°ãããã¨ãã§ããã
var hello = function(name) { return "hello: " + name; }; hello = _.wrap(hello, function(func) { return "before, " + func("moe") + ", after"; }); hello(); => 'before, hello: moe, after'
compose
_.compose(*functions)
è¤æ°ã®é¢æ°ã®åæé¢æ°ãè¿ããé¢æ°ã¯æ¬¡ã®é¢æ°ã®è¿ãå¤ãå¼æ°ã¨ãã¦ã¨ããé¢æ°f(), g(), h()ãåæãããã¨é¢æ°f(g(h()))ãã§ããã
var greet = function(name){ return "hi: " + name; }; var exclaim = function(statement){ return statement + "!"; }; var welcome = _.compose(exclaim, greet); welcome('moe'); => 'hi: moe!'
Object function
keys
_.keys(object)
objectã®ããããã£åããã¹ã¦åãåºãã
_.keys({one : 1, two : 2, three : 3}); => ["one", "two", "three"]
values
_.values(object)
objectã®ããããã£å¤ããã¹ã¦åãåºãã
_.values({one : 1, two : 2, three : 3}); => [1, 2, 3]
functions
_.functions(object)
objectãæã¤ãã¹ã¦ã®é¢æ°ããããã£åãã½ã¼ããã¦è¿ãã
_.functions(_); => ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
extend
_.extend(destination, *sources)
soucesãªãã¸ã§ã¯ãã®ãã¹ã¦ã®ããããã£ãdestinationãªãã¸ã§ã¯ãã«ã³ãã¼ãããsourcesã®ä¸ã«åãååã®ããããã£ãå«ã¾ãã¦ããå ´åãããå¾ã®ãã®ã§ä¸æ¸ããããã
_.extend({name : 'moe'}, {age : 50}); => {name : 'moe', age : 50}
defaults
_.defaults(object, *defaults)
objectãdefaultsãªãã¸ã§ã¯ãã®ããããã£ãæã£ã¦ããªãå ´åãobjectã«ãã®defaultsãªãã¸ã§ã¯ãã®ããããã£ãä»ä¸ããã
var iceCream = {flavor : "chocolate"}; _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"}); => {flavor : "chocolate", sprinkles : "lots"}
clone
_.clone(object)
objectã®ã·ã£ãã¼ã³ãã¼ããä½ãããã¹ãããããªãã¸ã§ã¯ããé åã¯ãåç §ã³ãã¼ãããã
_.clone({name : 'moe'}); => {name : 'moe'};
åå°½ãããæ®ãããã®ãã¡ã