Snippets
Created by
Lucian Buzzo
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | ;(function() {
var root = this;
function toArray(val) {
return Array.prototype.slice.call(val);
}
function forEach(collection, fn) {
var i = 0;
for(i; i < collection.length; i++) {
fn(collection[i]);
}
}
function map(collection, fn) {
var i = 0;
var newCollection = [];
for(i; i < collection.length; i++) {
newCollection.push(fn(collection[i]));
}
return newCollection;
}
function filter(collection, fn) {
var i = 0;
var newCollection = [];
for(i; i < collection.length; i++) {
if (fn(collection[i])) {
newCollection.push(collection[i]);
}
}
return newCollection;
}
function isUndefined(value) {
return typeof value == 'undefined';
}
function has(object, key) {
return object ? hasOwnProperty.call(object, key) : false;
}
function keys(object) {
var array = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
array.push(key);
}
}
return array;
}
function isString(value) {
return typeof value == 'string';
}
function isArray(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
function pick(object, keys) {
var newObject = {};
keys = (isString(keys) ? [keys] : keys);
forEach(keys, function(v) {
if (object.hasOwnProperty(v)) {
newObject[v] = clone(object[v]);
}
});
return newObject;
}
function isObject(value) {
// Avoid a V8 JIT bug in Chrome 19-20.
// See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
var type = typeof value;
return type == 'function' || (value && type == 'object') || false;
}
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
var chainFunctions = [map, forEach, isUndefined, has, keys, clone, filter, pick, isString, isArray];
var BalsaConstructor = function() {
function balsa(val) {
var methods = {};
if(val !== undefined) {
forEach(chainFunctions, function(chainable) {
methods[chainable.name] = function() {
var argsArray = toArray(arguments);
argsArray.unshift(val);
return chainable.apply(null, argsArray);
};
});
return methods;
}
}
balsa.map = map;
balsa.forEach = forEach;
balsa.filter = filter;
balsa.isUndefined = isUndefined;
balsa.isString = isString;
balsa.isArray = isArray;
balsa.has = has;
balsa.keys = keys;
balsa.clone = clone;
balsa.pick = pick;
return balsa;
};
root['ð…‰'] = root.b = new BalsaConstructor();
}.call(this));
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.