-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby.js
More file actions
103 lines (100 loc) · 3.7 KB
/
ruby.js
File metadata and controls
103 lines (100 loc) · 3.7 KB
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
// Object
//****************************************************************************
Object.defineProperty(Object.prototype, 'merge', {
enumerable: false, value: function(obj){ for(key in obj){ this[key] = obj[key] } }
})
// String
//****************************************************************************
String.prototype.merge({
to_i: function(){
result = this.valueOf(); result = result.replace(/\..+/, '');
result = result.replace(/\D/g, ''); return +result;
},
present : function(){ return (this.valueOf() != '') },
include : function(val){ return (this.search(val) != -1) },
downcase : function(){ return this.toLowerCase(); },
//downcase_bang : function(){ this = new String(this.toLowerCase()); return this.valueOf(); },
upcase : function(){ return this.toUpperCase(); }
//upcase_bang : function(){ this.value = this.toUpperCase(); return this; }
})
// Number
//****************************************************************************
Number.prototype.merge({
to_s: function(){ return ''+this.valueOf(); }
})
// Array
//****************************************************************************
Array.prototype.merge({
clone : function(){ var neo = []; for(key in this){ if(this.hasOwnProperty(key)){ neo[key] = this[key] } }; return neo },
clear : function(){ for(key in this){ if(this.hasOwnProperty(key)){ this.delete(this[key]) } } },
uniq : function(){ return this.intersect(this) },
uniq_bang : function(){ return this.intersect_bang(this) },
empty : function(){ return (this.length == 0) },
include : function(value){ return (this.indexOf(value) != -1) },
each : function(yield){ for(var x = 0; x < this.length; x++){ if(typeof(yield(x, this[x])) == 'boolean'){ break } } },
last : function() { return this[this.length - 1]; },
compact : function(){ var clone = this.clone(); clone.delete(null); return clone },
compact_bang : function(){ this.delete(null); return this },
intersect : function(arry){
var results = []
this.each(function(index,element){
if(arry.include(element) && !results.include(element)){
results.push(element)
}
})
return results
},
intersect_bang: function(rhs){
var
results = [],
lhs = this
;
lhs.each(function(index,element){
if(rhs.include(element) && !results.include(element)){
results.push(element)
}
})
lhs.clear()
for(key in results){ if(results.hasOwnProperty(key)){ this[key] = results[key] } }
return results
},
each_slice: function(size, yield){
if(size == 0){ return [] }
var times=Math.ceil(this.length/size)
for(var x = 0; x < times; x++){
var slice = []
for(var y = size*x; true; y++){
if(typeof(this[y]) != 'undefined'){ slice.push(this[y]) }
if((y%size)+1 == size){ break }
}
yield(slice)
}
},
map: function(yield){
var clone = this.clone()
for(var x = 0; x < clone.length; clone[x] = yield(x, clone[x++]));
return clone
},
map_bang: function(yield){
for(var x = 0; x < this.length; this[x] = yield(x, this[x++]));
return this
},
shuffle: function(){
var o = this.clone()
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
},
shuffle_bang: function(){
for(var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
return this;
},
delete: function(query){
var arry=this
while(arry.include(query)){
arry.each(function(i, e){
if(query == e){ arry.splice(i, 1); return false }
})
}
return query
}
})