Created
June 10, 2009 17:23
-
-
Save wilkerlucio/127366 to your computer and use it in GitHub Desktop.
Javascript Core Utils
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
/* | |
* Copyright 2009 Wilker Lucio <wilkerlucio@gmail.com> | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/** | |
* This file contains a series of common used extensions for Javascript | |
*/ | |
//object extensions | |
Object.extend = function(target, mixin, send_attributes) { | |
for (var p in mixin) { | |
target[p] = mixin[p]; | |
} | |
return target; | |
}; | |
Object.extend(Object, { | |
alias: function(target, original, alias) { | |
target[alias] = target[original]; | |
return target; | |
}, | |
is_array: function(object) { | |
return object != null && typeof object == "object" && 'splice' in object && 'join' in object; | |
}, | |
is_function: function(is_function) { | |
return typeof object == "function"; | |
} | |
}); | |
//enumerable | |
Enumerable = { | |
all: function(iterator) { | |
var all = true; | |
this.each(function(value) { | |
if (!iterator(value)) { | |
all = false; | |
return false; | |
} | |
}); | |
return all; | |
}, | |
any: function(iterator) { | |
var any = false; | |
this.each(function(value) { | |
if (iterator(value)) { | |
any = true; | |
return false; | |
} | |
}); | |
return any; | |
}, | |
find: function(iterator) { | |
var element = null; | |
this.each(function(value) { | |
if (iterator(value)) { | |
element = value; | |
return false; | |
} | |
}); | |
return element; | |
}, | |
find_all: function(iterator) { | |
var elements = []; | |
this.each(function(value) { | |
if (iterator(value)) { | |
elements.push(value); | |
} | |
}); | |
return elements; | |
}, | |
include: function(element) { | |
var include = false; | |
this.each(function(value) { | |
if (element == value) { | |
include = true; | |
return false; | |
} | |
}); | |
return include; | |
}, | |
inject: function(iterator, first) { | |
first = first || null; | |
this.each(function(value) { | |
first = iterator(first, value); | |
}); | |
return first; | |
}, | |
invoke: function() { | |
var args = $A(arguments); | |
var method = args.shift(); | |
return this.map(function(value) { | |
return value[method].apply(value, args); | |
}); | |
}, | |
map: function(iterator) { | |
var mapped = []; | |
this.each(function(value, key) { | |
mapped.push(iterator(value, key)); | |
}); | |
return mapped; | |
}, | |
partition: function(iterator) { | |
var accept = []; | |
var reject = []; | |
this.each(function(value) { | |
if (iterator(value)) { | |
accept.push(value); | |
} else { | |
reject.push(value); | |
} | |
}); | |
return [accept, reject]; | |
}, | |
pluck: function(property) { | |
return this.map(function(value) { | |
return value[property]; | |
}); | |
}, | |
reject: function(iterator) { | |
var elements = []; | |
this.each(function(value) { | |
if (!iterator(value)) { | |
elements.push(value); | |
} | |
}); | |
return elements; | |
}, | |
sort_by: function(criteria) { | |
return $A(this).sort(function(a, b) { | |
a = a[criteria]; | |
b = b[criteria]; | |
if (Object.is_function(a)) a = a(); | |
if (Object.is_function(b)) b = b(); | |
return a > b ? 1 : (a < b ? -1 : 0); | |
}); | |
}, | |
zip: function() { | |
var items = $A(arguments); | |
var iterator = items.shift(); | |
var results = []; | |
items.unshift($A(this)); | |
for (var i = 0; i < items[0].length; i++) { | |
var line = []; | |
for (var x = 0; x < items.length; x++) { | |
line.push(items[x][i]); | |
} | |
results.push(iterator.apply(this, line)); | |
} | |
return results; | |
} | |
}; | |
Object.alias(Enumerable, 'map', 'collect'); | |
Object.alias(Enumerable, 'include', 'member'); | |
Object.alias(Enumerable, 'find', 'detect'); | |
Object.alias(Enumerable, 'find_all', 'select'); | |
//array extensions | |
Object.extend(Array.prototype, Enumerable); | |
Array.prototype.each = function(iterator) { | |
for (var i = 0; i < this.length; i++) { | |
if (!iterator(this[i])) break; | |
} | |
return this; | |
} | |
Array.to_a = function(enumerable) { | |
var array = []; | |
for (var i = 0; i < enumerable; i++) { | |
array.push(enumerable[i]); | |
} | |
return array; | |
}; | |
$A = Array.to_a(); | |
//function extensions | |
Object.extend(Function.prototype, { | |
bind: function() { | |
if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; | |
var __method = this, args = $A(arguments), object = args.shift(); | |
return function() { | |
return __method.apply(object, args.concat($A(arguments))); | |
}; | |
}, | |
bindAsEventListener: function() { | |
var __method = this, args = $A(arguments), object = args.shift(); | |
return function(event) { | |
return __method.apply(object, [event || window.event].concat(args)); | |
}; | |
}, | |
curry: function() { | |
if (!arguments.length) return this; | |
var __method = this, args = $A(arguments); | |
return function() { | |
return __method.apply(this, args.concat($A(arguments))); | |
}; | |
}, | |
delay: function() { | |
var __method = this, args = $A(arguments), timeout = args.shift() * 1000; | |
return window.setTimeout(function() { | |
return __method.apply(__method, args); | |
}, timeout); | |
}, | |
defer: function() { | |
var args = [0.01].concat($A(arguments)); | |
return this.delay.apply(this, args); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment