dress
Version:
missing javascript prototypes(experimental)
466 lines (458 loc) • 12.1 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var root,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
root = typeof global === 'undefined' ? window : global;
root.DRESS = {
set: function(object) {
var alias, fn, method, name, _fn, _i, _len, _ref, _ref1, _ref2, _results;
if (object.methods != null) {
_ref = object.methods;
for (name in _ref) {
fn = _ref[name];
root.Object.defineProperty(root[object.name].prototype, "$" + name, {
value: fn
});
}
}
if (object.injectors != null) {
_ref1 = object.injectors;
_fn = function() {
var _method;
_method = method;
return root.Object.defineProperty(root[object.name].prototype, '$$' + method, {
value: function() {
this.$set(this['$' + _method].apply(this, arguments));
return this;
}
});
};
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
method = _ref1[_i];
_fn();
}
}
if (object.aliases != null) {
_ref2 = object.aliases;
_results = [];
for (alias in _ref2) {
method = _ref2[alias];
_results.push(root.Object.defineProperty(root[object.name].prototype, alias, {
value: root[object.name].prototype[method]
}));
}
return _results;
}
}
};
DRESS.set({
name: 'Array',
injectors: ['map', 'slice', 'sort', 'samples', 'where', 'reverse', 'filter', 'reject', 'take', 'drop', 'invoke', 'pluck', 'shuffle'],
aliases: {
'$map': 'map',
'$filter': 'filter'
},
methods: {
set: function(array) {
var item, _i, _len;
this.length = 0;
for (_i = 0, _len = array.length; _i < _len; _i++) {
item = array[_i];
this.push(item);
}
return this;
},
first: function() {
return this[0];
},
last: function() {
return this[this.length - 1];
},
find: function(fn) {
var item, _i, _len;
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
if (fn(item)) {
return item;
}
}
},
contains: function(object) {
if (this.indexOf(object) === -1) {
return false;
} else {
return true;
}
},
max: function(fn) {
var fn_result, item, max, result, _i, _len;
if (fn != null) {
max = fn(this[0]);
result = this[0];
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
fn_result = fn(item);
if (fn_result > max) {
result = item;
max = fn_result;
}
}
return result;
} else {
return Math.max.apply(null, this);
}
},
min: function(fn) {
var fn_result, item, min, result, _i, _len;
if (fn != null) {
min = fn(this[0]);
result = this[0];
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
fn_result = fn(item);
if (fn_result < min) {
result = item;
min = fn_result;
}
}
return result;
} else {
return Math.min.apply(null, this);
}
},
sort: function(fn) {
var item, result, _i, _len;
if (fn == null) {
fn = function(item) {
return item;
};
}
result = [];
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
result.push(item);
}
return root.Array.prototype.sort.call(result, function(a, b) {
return fn(a) > fn(b);
});
},
samples: function(count) {
var checked, index, result;
if (count == null) {
count = 1;
}
checked = [];
result = [];
while (count > 0) {
index = Math.floor(Math.random() * this.length);
if (checked.$contains(index)) {
continue;
}
checked.push(index);
result.push(this[index]);
count--;
}
return result;
},
sample: function() {
return this.$samples().$first();
},
each: function(fn) {
this.forEach(fn);
return this;
},
where: function(object) {
var found, item, k, result, v, _i, _len;
result = [];
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
found = true;
for (k in object) {
v = object[k];
if (item[k] !== v) {
found = false;
}
}
if (found) {
result.push(item);
}
}
return result;
},
reject: function(fn) {
return this.$filter(function(item) {
return !fn(item);
});
},
take: function(count) {
var index, result;
result = [];
index = 0;
while (index !== count) {
result.push(this[index]);
index++;
}
return result;
},
drop: function(count) {
var i, result, _i, _ref;
result = [];
for (i = _i = count, _ref = this.length - 1; count <= _ref ? _i <= _ref : _i >= _ref; i = count <= _ref ? ++_i : --_i) {
result.push(this[i]);
}
return result;
},
invoke: function(fn_name) {
var item, result, _i, _len;
result = [];
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
delete arguments[0];
result.push(item[fn_name].apply(item, arguments.$values()));
}
return result;
},
pluck: function(key) {
var item, result, _i, _len;
result = [];
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
result.push(item[key]);
}
return result;
},
group: function(fn) {
var item, result, _i, _len, _name, _name1;
result = {};
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
if (fn.$is_function()) {
if (result[_name = fn(item)] == null) {
result[_name] = [];
}
result[fn(item)].push(item);
} else {
if (result[_name1 = item[fn]] == null) {
result[_name1] = [];
}
result[item[fn]].push(item);
}
}
return result;
},
count: function(fn) {
var item, object, _i, _len, _name;
object = {};
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
if (object[_name = fn(item)] == null) {
object[_name] = 0;
}
object[fn(item)]++;
}
return object;
},
shuffle: function() {
return this.$samples(this.length);
}
}
});
DRESS.set({
name: 'Object',
injectors: ['pick', 'invert', 'merge', 'omit', 'defaults'],
aliases: {
'$is_function': '$isFunction',
'$is_array': '$isArray',
'$is_empty': '$isEmpty',
'$is_string': '$isString',
'$is_number': '$isNumber',
'$is_boolean': '$isBoolean',
'$is_date': '$isDate'
},
methods: {
set: function(object) {
var k, v;
for (k in this) {
v = this[k];
delete this[k];
}
for (k in object) {
v = object[k];
this[k] = v;
}
return this;
},
pick: function() {
var k, result, v;
result = {};
for (k in this) {
v = this[k];
if (__indexOf.call(arguments, k) >= 0) {
result[k] = v;
}
}
return result;
},
keys: function() {
return Object.keys(this);
},
values: function() {
var k, result, v;
result = [];
for (k in this) {
v = this[k];
result.push(v);
}
return result;
},
isFunction: function() {
return typeof this === 'function';
},
isArray: function() {
return this instanceof Array;
},
invert: function() {
var k, object, v;
object = {};
for (k in this) {
v = this[k];
object[v] = k;
}
return object;
},
each: function(fn) {
var k, v;
for (k in this) {
v = this[k];
fn(k, v);
}
return this;
},
pairs: function() {
var result;
result = [];
this.$each(function(k, v) {
return result.push([k, v]);
});
return result;
},
functions: function() {
var result;
result = [];
this.$each(function(k, v) {
if (v.$is_function()) {
return result.push(k);
}
});
return result;
},
merge: function(object) {
var result;
result = this.$clone();
object.$each(function(k, v) {
return result[k] = v;
});
return result;
},
omit: function(list) {
var object;
if (!list.$is_array()) {
list = arguments.$values();
}
object = {};
this.$each(function(k, v) {
if (!list.$contains(k)) {
return object[k] = v;
}
});
return object;
},
defaults: function(object) {
var result;
result = this.$clone();
object.$each(function(k, v) {
if (result[k] == null) {
return result[k] = v;
}
});
return result;
},
clone: function(object) {
var result;
result = {};
this.$each(function(k, v) {
return result[k] = v;
});
return result;
},
tap: function(fn) {
fn();
return this;
},
isEmpty: function() {
return Object.keys(this).length === 0;
},
isString: function() {
return toString.call(this) === '[object String]';
},
isNumber: function() {
return toString.call(this) === '[object Number]';
},
isBoolean: function() {
return toString.call(this) === '[object Boolean]';
},
isDate: function() {
return toString.call(this) === '[object Date]';
},
json: function() {
return JSON.stringify(this);
}
}
});
DRESS.set({
name: 'String',
methods: {
stars: function(string) {
return this.indexOf(string) === 0;
},
ends: function(string) {
return this.slice(-string.length) === string;
},
capitalize: function() {
return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
},
titleize: function() {
return (this.split(' ').map(function(word) {
return word.$capitalize();
})).join(' ');
},
integer: function() {
return parseInt(this);
},
number: function() {
return parseFloat(this);
},
date: function() {
return new Date(Date.parse(this));
},
parse: function() {
return JSON.parse(this);
}
}
});
DRESS.set({
name: 'Number',
methods: {
times: function(fn) {
var i, _i, _ref;
for (i = _i = 0, _ref = this - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
fn(i);
}
return this;
},
date: function() {
return new Date(this);
}
}
});
}).call(this);