Projekt

Obecné

Profil

Stáhnout (16.8 KB) Statistiky
| Větev: | Revize:
1
/* eslint-disable */
2
/* jscs: disable */
3
'use strict';
4

    
5
// pulled specific shims from https://github.com/es-shims/es5-shim
6

    
7
var ArrayPrototype = Array.prototype;
8
var ObjectPrototype = Object.prototype;
9
var FunctionPrototype = Function.prototype;
10
var StringPrototype = String.prototype;
11
var array_slice = ArrayPrototype.slice;
12

    
13
var _toString = ObjectPrototype.toString;
14
var isFunction = function (val) {
15
    return ObjectPrototype.toString.call(val) === '[object Function]';
16
};
17
var isArray = function isArray(obj) {
18
    return _toString.call(obj) === '[object Array]';
19
};
20
var isString = function isString(obj) {
21
    return _toString.call(obj) === '[object String]';
22
};
23

    
24
var supportsDescriptors = Object.defineProperty && (function () {
25
    try {
26
        Object.defineProperty({}, 'x', {});
27
        return true;
28
    } catch (e) { /* this is ES3 */
29
        return false;
30
    }
31
}());
32

    
33
// Define configurable, writable and non-enumerable props
34
// if they don't exist.
35
var defineProperty;
36
if (supportsDescriptors) {
37
    defineProperty = function (object, name, method, forceAssign) {
38
        if (!forceAssign && (name in object)) { return; }
39
        Object.defineProperty(object, name, {
40
            configurable: true,
41
            enumerable: false,
42
            writable: true,
43
            value: method
44
        });
45
    };
46
} else {
47
    defineProperty = function (object, name, method, forceAssign) {
48
        if (!forceAssign && (name in object)) { return; }
49
        object[name] = method;
50
    };
51
}
52
var defineProperties = function (object, map, forceAssign) {
53
    for (var name in map) {
54
        if (ObjectPrototype.hasOwnProperty.call(map, name)) {
55
          defineProperty(object, name, map[name], forceAssign);
56
        }
57
    }
58
};
59

    
60
var toObject = function (o) {
61
    if (o == null) { // this matches both null and undefined
62
        throw new TypeError("can't convert " + o + ' to object');
63
    }
64
    return Object(o);
65
};
66

    
67
//
68
// Util
69
// ======
70
//
71

    
72
// ES5 9.4
73
// http://es5.github.com/#x9.4
74
// http://jsperf.com/to-integer
75

    
76
function toInteger(num) {
77
    var n = +num;
78
    if (n !== n) { // isNaN
79
        n = 0;
80
    } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
81
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
82
    }
83
    return n;
84
}
85

    
86
function ToUint32(x) {
87
    return x >>> 0;
88
}
89

    
90
//
91
// Function
92
// ========
93
//
94

    
95
// ES-5 15.3.4.5
96
// http://es5.github.com/#x15.3.4.5
97

    
98
function Empty() {}
99

    
100
defineProperties(FunctionPrototype, {
101
    bind: function bind(that) { // .length is 1
102
        // 1. Let Target be the this value.
103
        var target = this;
104
        // 2. If IsCallable(Target) is false, throw a TypeError exception.
105
        if (!isFunction(target)) {
106
            throw new TypeError('Function.prototype.bind called on incompatible ' + target);
107
        }
108
        // 3. Let A be a new (possibly empty) internal list of all of the
109
        //   argument values provided after thisArg (arg1, arg2 etc), in order.
110
        // XXX slicedArgs will stand in for "A" if used
111
        var args = array_slice.call(arguments, 1); // for normal call
112
        // 4. Let F be a new native ECMAScript object.
113
        // 11. Set the [[Prototype]] internal property of F to the standard
114
        //   built-in Function prototype object as specified in 15.3.3.1.
115
        // 12. Set the [[Call]] internal property of F as described in
116
        //   15.3.4.5.1.
117
        // 13. Set the [[Construct]] internal property of F as described in
118
        //   15.3.4.5.2.
119
        // 14. Set the [[HasInstance]] internal property of F as described in
120
        //   15.3.4.5.3.
121
        var binder = function () {
122

    
123
            if (this instanceof bound) {
124
                // 15.3.4.5.2 [[Construct]]
125
                // When the [[Construct]] internal method of a function object,
126
                // F that was created using the bind function is called with a
127
                // list of arguments ExtraArgs, the following steps are taken:
128
                // 1. Let target be the value of F's [[TargetFunction]]
129
                //   internal property.
130
                // 2. If target has no [[Construct]] internal method, a
131
                //   TypeError exception is thrown.
132
                // 3. Let boundArgs be the value of F's [[BoundArgs]] internal
133
                //   property.
134
                // 4. Let args be a new list containing the same values as the
135
                //   list boundArgs in the same order followed by the same
136
                //   values as the list ExtraArgs in the same order.
137
                // 5. Return the result of calling the [[Construct]] internal
138
                //   method of target providing args as the arguments.
139

    
140
                var result = target.apply(
141
                    this,
142
                    args.concat(array_slice.call(arguments))
143
                );
144
                if (Object(result) === result) {
145
                    return result;
146
                }
147
                return this;
148

    
149
            } else {
150
                // 15.3.4.5.1 [[Call]]
151
                // When the [[Call]] internal method of a function object, F,
152
                // which was created using the bind function is called with a
153
                // this value and a list of arguments ExtraArgs, the following
154
                // steps are taken:
155
                // 1. Let boundArgs be the value of F's [[BoundArgs]] internal
156
                //   property.
157
                // 2. Let boundThis be the value of F's [[BoundThis]] internal
158
                //   property.
159
                // 3. Let target be the value of F's [[TargetFunction]] internal
160
                //   property.
161
                // 4. Let args be a new list containing the same values as the
162
                //   list boundArgs in the same order followed by the same
163
                //   values as the list ExtraArgs in the same order.
164
                // 5. Return the result of calling the [[Call]] internal method
165
                //   of target providing boundThis as the this value and
166
                //   providing args as the arguments.
167

    
168
                // equiv: target.call(this, ...boundArgs, ...args)
169
                return target.apply(
170
                    that,
171
                    args.concat(array_slice.call(arguments))
172
                );
173

    
174
            }
175

    
176
        };
177

    
178
        // 15. If the [[Class]] internal property of Target is "Function", then
179
        //     a. Let L be the length property of Target minus the length of A.
180
        //     b. Set the length own property of F to either 0 or L, whichever is
181
        //       larger.
182
        // 16. Else set the length own property of F to 0.
183

    
184
        var boundLength = Math.max(0, target.length - args.length);
185

    
186
        // 17. Set the attributes of the length own property of F to the values
187
        //   specified in 15.3.5.1.
188
        var boundArgs = [];
189
        for (var i = 0; i < boundLength; i++) {
190
            boundArgs.push('$' + i);
191
        }
192

    
193
        // XXX Build a dynamic function with desired amount of arguments is the only
194
        // way to set the length property of a function.
195
        // In environments where Content Security Policies enabled (Chrome extensions,
196
        // for ex.) all use of eval or Function costructor throws an exception.
197
        // However in all of these environments Function.prototype.bind exists
198
        // and so this code will never be executed.
199
        var bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder);
200

    
201
        if (target.prototype) {
202
            Empty.prototype = target.prototype;
203
            bound.prototype = new Empty();
204
            // Clean up dangling references.
205
            Empty.prototype = null;
206
        }
207

    
208
        // TODO
209
        // 18. Set the [[Extensible]] internal property of F to true.
210

    
211
        // TODO
212
        // 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
213
        // 20. Call the [[DefineOwnProperty]] internal method of F with
214
        //   arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
215
        //   thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
216
        //   false.
217
        // 21. Call the [[DefineOwnProperty]] internal method of F with
218
        //   arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
219
        //   [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
220
        //   and false.
221

    
222
        // TODO
223
        // NOTE Function objects created using Function.prototype.bind do not
224
        // have a prototype property or the [[Code]], [[FormalParameters]], and
225
        // [[Scope]] internal properties.
226
        // XXX can't delete prototype in pure-js.
227

    
228
        // 22. Return F.
229
        return bound;
230
    }
231
});
232

    
233
//
234
// Array
235
// =====
236
//
237

    
238
// ES5 15.4.3.2
239
// http://es5.github.com/#x15.4.3.2
240
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
241
defineProperties(Array, { isArray: isArray });
242

    
243

    
244
var boxedString = Object('a');
245
var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
246

    
247
var properlyBoxesContext = function properlyBoxed(method) {
248
    // Check node 0.6.21 bug where third parameter is not boxed
249
    var properlyBoxesNonStrict = true;
250
    var properlyBoxesStrict = true;
251
    if (method) {
252
        method.call('foo', function (_, __, context) {
253
            if (typeof context !== 'object') { properlyBoxesNonStrict = false; }
254
        });
255

    
256
        method.call([1], function () {
257
            'use strict';
258
            properlyBoxesStrict = typeof this === 'string';
259
        }, 'x');
260
    }
261
    return !!method && properlyBoxesNonStrict && properlyBoxesStrict;
262
};
263

    
264
defineProperties(ArrayPrototype, {
265
    forEach: function forEach(fun /*, thisp*/) {
266
        var object = toObject(this),
267
            self = splitString && isString(this) ? this.split('') : object,
268
            thisp = arguments[1],
269
            i = -1,
270
            length = self.length >>> 0;
271

    
272
        // If no callback function or if callback is not a callable function
273
        if (!isFunction(fun)) {
274
            throw new TypeError(); // TODO message
275
        }
276

    
277
        while (++i < length) {
278
            if (i in self) {
279
                // Invoke the callback function with call, passing arguments:
280
                // context, property value, property key, thisArg object
281
                // context
282
                fun.call(thisp, self[i], i, object);
283
            }
284
        }
285
    }
286
}, !properlyBoxesContext(ArrayPrototype.forEach));
287

    
288
// ES5 15.4.4.14
289
// http://es5.github.com/#x15.4.4.14
290
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
291
var hasFirefox2IndexOfBug = Array.prototype.indexOf && [0, 1].indexOf(1, 2) !== -1;
292
defineProperties(ArrayPrototype, {
293
    indexOf: function indexOf(sought /*, fromIndex */ ) {
294
        var self = splitString && isString(this) ? this.split('') : toObject(this),
295
            length = self.length >>> 0;
296

    
297
        if (!length) {
298
            return -1;
299
        }
300

    
301
        var i = 0;
302
        if (arguments.length > 1) {
303
            i = toInteger(arguments[1]);
304
        }
305

    
306
        // handle negative indices
307
        i = i >= 0 ? i : Math.max(0, length + i);
308
        for (; i < length; i++) {
309
            if (i in self && self[i] === sought) {
310
                return i;
311
            }
312
        }
313
        return -1;
314
    }
315
}, hasFirefox2IndexOfBug);
316

    
317
//
318
// String
319
// ======
320
//
321

    
322
// ES5 15.5.4.14
323
// http://es5.github.com/#x15.5.4.14
324

    
325
// [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers]
326
// Many browsers do not split properly with regular expressions or they
327
// do not perform the split correctly under obscure conditions.
328
// See http://blog.stevenlevithan.com/archives/cross-browser-split
329
// I've tested in many browsers and this seems to cover the deviant ones:
330
//    'ab'.split(/(?:ab)*/) should be ["", ""], not [""]
331
//    '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""]
332
//    'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not
333
//       [undefined, "t", undefined, "e", ...]
334
//    ''.split(/.?/) should be [], not [""]
335
//    '.'.split(/()()/) should be ["."], not ["", "", "."]
336

    
337
var string_split = StringPrototype.split;
338
if (
339
    'ab'.split(/(?:ab)*/).length !== 2 ||
340
    '.'.split(/(.?)(.?)/).length !== 4 ||
341
    'tesst'.split(/(s)*/)[1] === 't' ||
342
    'test'.split(/(?:)/, -1).length !== 4 ||
343
    ''.split(/.?/).length ||
344
    '.'.split(/()()/).length > 1
345
) {
346
    (function () {
347
        var compliantExecNpcg = /()??/.exec('')[1] === void 0; // NPCG: nonparticipating capturing group
348

    
349
        StringPrototype.split = function (separator, limit) {
350
            var string = this;
351
            if (separator === void 0 && limit === 0) {
352
                return [];
353
            }
354

    
355
            // If `separator` is not a regex, use native split
356
            if (_toString.call(separator) !== '[object RegExp]') {
357
                return string_split.call(this, separator, limit);
358
            }
359

    
360
            var output = [],
361
                flags = (separator.ignoreCase ? 'i' : '') +
362
                        (separator.multiline  ? 'm' : '') +
363
                        (separator.extended   ? 'x' : '') + // Proposed for ES6
364
                        (separator.sticky     ? 'y' : ''), // Firefox 3+
365
                lastLastIndex = 0,
366
                // Make `global` and avoid `lastIndex` issues by working with a copy
367
                separator2, match, lastIndex, lastLength;
368
            separator = new RegExp(separator.source, flags + 'g');
369
            string += ''; // Type-convert
370
            if (!compliantExecNpcg) {
371
                // Doesn't need flags gy, but they don't hurt
372
                separator2 = new RegExp('^' + separator.source + '$(?!\\s)', flags);
373
            }
374
            /* Values for `limit`, per the spec:
375
             * If undefined: 4294967295 // Math.pow(2, 32) - 1
376
             * If 0, Infinity, or NaN: 0
377
             * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
378
             * If negative number: 4294967296 - Math.floor(Math.abs(limit))
379
             * If other: Type-convert, then use the above rules
380
             */
381
            limit = limit === void 0 ?
382
                -1 >>> 0 : // Math.pow(2, 32) - 1
383
                ToUint32(limit);
384
            while (match = separator.exec(string)) {
385
                // `separator.lastIndex` is not reliable cross-browser
386
                lastIndex = match.index + match[0].length;
387
                if (lastIndex > lastLastIndex) {
388
                    output.push(string.slice(lastLastIndex, match.index));
389
                    // Fix browsers whose `exec` methods don't consistently return `undefined` for
390
                    // nonparticipating capturing groups
391
                    if (!compliantExecNpcg && match.length > 1) {
392
                        match[0].replace(separator2, function () {
393
                            for (var i = 1; i < arguments.length - 2; i++) {
394
                                if (arguments[i] === void 0) {
395
                                    match[i] = void 0;
396
                                }
397
                            }
398
                        });
399
                    }
400
                    if (match.length > 1 && match.index < string.length) {
401
                        ArrayPrototype.push.apply(output, match.slice(1));
402
                    }
403
                    lastLength = match[0].length;
404
                    lastLastIndex = lastIndex;
405
                    if (output.length >= limit) {
406
                        break;
407
                    }
408
                }
409
                if (separator.lastIndex === match.index) {
410
                    separator.lastIndex++; // Avoid an infinite loop
411
                }
412
            }
413
            if (lastLastIndex === string.length) {
414
                if (lastLength || !separator.test('')) {
415
                    output.push('');
416
                }
417
            } else {
418
                output.push(string.slice(lastLastIndex));
419
            }
420
            return output.length > limit ? output.slice(0, limit) : output;
421
        };
422
    }());
423

    
424
// [bugfix, chrome]
425
// If separator is undefined, then the result array contains just one String,
426
// which is the this value (converted to a String). If limit is not undefined,
427
// then the output array is truncated so that it contains no more than limit
428
// elements.
429
// "0".split(undefined, 0) -> []
430
} else if ('0'.split(void 0, 0).length) {
431
    StringPrototype.split = function split(separator, limit) {
432
        if (separator === void 0 && limit === 0) { return []; }
433
        return string_split.call(this, separator, limit);
434
    };
435
}
436

    
437
// ECMA-262, 3rd B.2.3
438
// Not an ECMAScript standard, although ECMAScript 3rd Edition has a
439
// non-normative section suggesting uniform semantics and it should be
440
// normalized across all browsers
441
// [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE
442
var string_substr = StringPrototype.substr;
443
var hasNegativeSubstrBug = ''.substr && '0b'.substr(-1) !== 'b';
444
defineProperties(StringPrototype, {
445
    substr: function substr(start, length) {
446
        return string_substr.call(
447
            this,
448
            start < 0 ? ((start = this.length + start) < 0 ? 0 : start) : start,
449
            length
450
        );
451
    }
452
}, hasNegativeSubstrBug);
(10-10/12)