forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang_fix.js
260 lines (255 loc) · 10.5 KB
/
lang_fix.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//=========================================
// 语言补丁模块
//==========================================
define( "lang_fix", !!Array.isArray, function(){
$.log("已加载语言补丁模块");
//fix ie for..in bug
var DONT_ENUM = $.DONT_ENUM = "propertyIsEnumerable,isPrototypeOf,hasOwnProperty,toLocaleString,toString,valueOf,constructor".split(","),
P = "prototype",
hasOwn = ({}).hasOwnProperty;
for (var i in {
toString: 1
}){
DONT_ENUM = false;
}
//第二个参数仅在浏览器支持Object.defineProperties时可用
$.mix(Object,{
create: function(o){
if (arguments.length > 1) {
$.log(" Object.create implementation only accepts the first parameter.")
}
function F() {}
F.prototype = o;
return new F();
},
//取得其所有键名以数组形式返回
keys: function(obj){//ecma262v5 15.2.3.14
var result = [];
for(var key in obj ) if(hasOwn.call(obj,key)){
result.push(key)
}
if(DONT_ENUM && obj){
for(var i = 0 ;key =DONT_ENUM[i++]; ){
if(hasOwn.call(obj,key)){
result.push(key);
}
}
}
return result;
},
getPrototypeOf : typeof P.__proto__ === "object" ? function(obj){
return obj.__proto__;
}:function(obj){
return obj.constructor[P];
}
},false);
//用于创建javascript1.6 Array的迭代器
function iterator(vars, body, ret) {
var fun = 'for(var '+vars+'i=0,n = this.length;i < n;i++){'+
body.replace('_', '((i in this) && fn.call(scope,this[i],i,this))')
+'}'+ret
return Function("fn,scope",fun);
}
$.mix(Array[P],{
//定位操作,返回数组中第一个等于给定参数的元素的索引值。
indexOf: function (item, index) {
var n = this.length, i = ~~index;
if (i < 0) i += n;
for (; i < n; i++)
if ( this[i] === item) return i;
return -1;
},
//定位引操作,同上,不过是从后遍历。
lastIndexOf: function (item, index) {
var n = this.length,
i = index == null ? n - 1 : index;
if (i < 0) i = Math.max(0, n + i);
for (; i >= 0; i--)
if (this[i] === item) return i;
return -1;
},
//迭代操作,将数组的元素挨个儿传入一个函数中执行。Ptototype.js的对应名字为each。
forEach : iterator('', '_', ''),
//迭代类 在数组中的每个项上运行一个函数,如果此函数的值为真,则此元素作为新数组的元素收集起来,并返回新数组
filter : iterator('r=[],j=0,', 'if(_)r[j++]=this[i]', 'return r'),
//收集操作,将数组的元素挨个儿传入一个函数中执行,然后把它们的返回值组成一个新数组返回。Ptototype.js的对应名字为collect。
map : iterator('r=[],', 'r[i]=_', 'return r'),
//只要数组中有一个元素满足条件(放进给定函数返回true),那么它就返回true。Ptototype.js的对应名字为any。
some : iterator('', 'if(_)return true', 'return false'),
//只有数组中的元素都满足条件(放进给定函数返回true),它才返回true。Ptototype.js的对应名字为all。
every : iterator('', 'if(!_)return false', 'return true'),
//归化类 javascript1.8 将该数组的每个元素和前一次调用的结果运行一个函数,返回最后的结果。
reduce: function (fn, lastResult, scope) {
if (this.length == 0) return lastResult;
var i = lastResult !== undefined ? 0 : 1;
var result = lastResult !== undefined ? lastResult : this[0];
for (var n = this.length; i < n; i++)
result = fn.call(scope, result, this[i], i, this);
return result;
},
//归化类 javascript1.8 同上,但从右向左执行。
reduceRight: function (fn, lastResult, scope) {
var array = this.concat().reverse();
return array.reduce(fn, lastResult, scope);
}
},false);
//修正IE67下unshift不返回数组长度的问题
//http://www.cnblogs.com/rubylouvre/archive/2010/01/14/1647751.html
if([].unshift(1) !== 1){
var _unshift = Array[P].unshift;
Array[P].unshift = function(){
_unshift.apply(this, arguments);
return this.length; //返回新数组的长度
}
}
if([1,2,3].splice(1).length === 0){
var _splice = Array[P].splice;
Array[P].splice = function(a){
if(arguments.length === 1){
return _splice.call(this, a, this.length)
}else{
return _splice.apply(this, arguments);
}
}
}
if(!Array.isArray){
Array.isArray = function(obj){
return Object.prototype.toString.call(obj) =="[object Array]";
};
}
//String扩展
$.mix(String[P],{
//ecma262v5 15.5.4.20
//http://www.cnblogs.com/rubylouvre/archive/2009/09/18/1568794.html
//' dfsd '.trim() === 'dfsd''
trim: function(){
return this.replace(/^[\s\xA0]+/,"").replace(/[\s\xA0]+$/,'')
}
},false);
$.mix(Function[P],{
//ecma262v5 15.3.4.5
bind:function(scope) {
if (arguments.length < 2 && scope===void 0) return this;
var fn = this, argv = arguments;
return function() {
var args = [], i;
for(i = 1; i < argv.length; i++)
args.push(argv[i]);
for(i = 0; i < arguments.length; i++)
args.push(arguments[i]);
return fn.apply(scope, args);
};
}
},false);
// Fix Date.get/setYear() (IE5-7)
if ((new Date).getYear() > 1900) {
Date.now = function(){
return +new Date;
}
//http://stackoverflow.com/questions/5763107/javascript-date-getyear-returns-different-result-between-ie-and-firefox-how-to
Date[P].getYear = function() {
return this.getFullYear() - 1900;
};
Date[P].setYear = function(year) {
return this.setFullYear(year );//+ 1900
};
}
/**
* Cross-Browser Split 1.1.1
* Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
* Available under the MIT License
* ECMAScript compliant, uniform cross-browser split method
* // Basic use
* split('a b c d', ' ');
* // -> ['a', 'b', 'c', 'd']
*
* // With limit
* split('a b c d', ' ', 2);
* // -> ['a', 'b']
*
* // Backreferences in result array
* split('..word1 word2..', /([a-z]+)(\d+)/i);
* // -> ['..', 'word', '1', ' ', 'word', '2', '..']
*/
var nativeSplit = String.prototype.split,
compliantExecNpcg = /()??/.exec("")[1] === void 0, // NPCG: nonparticipating capturing group
fix = function (str, separator, limit) {
// If `separator` is not a regex, use `nativeSplit`
if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
return nativeSplit.call(str, separator, limit);
}
var output = [],
flags = (separator.ignoreCase ? "i" : "") +
(separator.multiline ? "m" : "") +
(separator.extended ? "x" : "") + // Proposed for ES6
(separator.sticky ? "y" : ""), // Firefox 3+
lastLastIndex = 0,
// Make `global` and avoid `lastIndex` issues by working with a copy
separator = new RegExp(separator.source, flags + "g"),
separator2, match, lastIndex, lastLength;
str += ""; // Type-convert
if (!compliantExecNpcg) {
// Doesn't need flags gy, but they don't hurt
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
}
/**
* Values for `limit`, per the spec:
* If undefined: 4294967295 // Math.pow(2, 32) - 1
* If 0, Infinity, or NaN: 0
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
* If other: Type-convert, then use the above rules
*/
limit = limit === void 0 ?
-1 >>> 0 : // Math.pow(2, 32) - 1
limit >>> 0; // ToUint32(limit)
while (match = separator.exec(str)) {
// `separator.lastIndex` is not reliable cross-browser
lastIndex = match.index + match[0].length;
if (lastIndex > lastLastIndex) {
output.push(str.slice(lastLastIndex, match.index));
// Fix browsers whose `exec` methods don't consistently return `undefined` for
// nonparticipating capturing groups
if (!compliantExecNpcg && match.length > 1) {
match[0].replace(separator2, function () {
for (var i = 1; i < arguments.length - 2; i++) {
if (arguments[i] === void 0) {
match[i] = void 0;
}
}
});
}
if (match.length > 1 && match.index < str.length) {
Array.prototype.push.apply(output, match.slice(1));
}
lastLength = match[0].length;
lastLastIndex = lastIndex;
if (output.length >= limit) {
break;
}
}
if (separator.lastIndex === match.index) {
separator.lastIndex++; // Avoid an infinite loop
}
}
if (lastLastIndex === str.length) {
if (lastLength || !separator.test("")) {
output.push("");
}
} else {
output.push(str.slice(lastLastIndex));
}
return output.length > limit ? output.slice(0, limit) : output;
};
// For convenience
String.prototype.split = function (separator, limit) {
return fix(this, separator, limit);
};
});
/**
2011.7.26 添加Object.getPrototypeOf方法
2011.11.16重构Array.prototype.unshift (thx @abcd)
2011.12.22 修正命名空间
2012.3.19 添加对split的修复
2012.5.31 添加Object.create的不完全修复
*/