legacy.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*jshint
  2. asi: true,
  3. unused: true,
  4. boss: true,
  5. loopfunc: true,
  6. eqnull: true
  7. */
  8. /*!
  9. * Legacy browser support
  10. */
  11. // Map array support
  12. if ( ![].map ) {
  13. Array.prototype.map = function ( callback, self ) {
  14. var array = this, len = array.length, newArray = new Array( len )
  15. for ( var i = 0; i < len; i++ ) {
  16. if ( i in array ) {
  17. newArray[ i ] = callback.call( self, array[ i ], i, array )
  18. }
  19. }
  20. return newArray
  21. }
  22. }
  23. // Filter array support
  24. if ( ![].filter ) {
  25. Array.prototype.filter = function( callback ) {
  26. if ( this == null ) throw new TypeError()
  27. var t = Object( this ), len = t.length >>> 0
  28. if ( typeof callback != 'function' ) throw new TypeError()
  29. var newArray = [], thisp = arguments[ 1 ]
  30. for ( var i = 0; i < len; i++ ) {
  31. if ( i in t ) {
  32. var val = t[ i ]
  33. if ( callback.call( thisp, val, i, t ) ) newArray.push( val )
  34. }
  35. }
  36. return newArray
  37. }
  38. }
  39. // Index of array support
  40. if ( ![].indexOf ) {
  41. Array.prototype.indexOf = function( searchElement ) {
  42. if ( this == null ) throw new TypeError()
  43. var t = Object( this ), len = t.length >>> 0
  44. if ( len === 0 ) return -1
  45. var n = 0
  46. if ( arguments.length > 1 ) {
  47. n = Number( arguments[ 1 ] )
  48. if ( n != n ) {
  49. n = 0
  50. }
  51. else if ( n !== 0 && n != Infinity && n != -Infinity ) {
  52. n = ( n > 0 || -1 ) * Math.floor( Math.abs( n ) )
  53. }
  54. }
  55. if ( n >= len ) return -1
  56. var k = n >= 0 ? n : Math.max( len - Math.abs( n ), 0 )
  57. for ( ; k < len; k++ ) {
  58. if ( k in t && t[ k ] === searchElement ) return k
  59. }
  60. return -1
  61. }
  62. }
  63. /*!
  64. * Cross-Browser Split 1.1.1
  65. * Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
  66. * Available under the MIT License
  67. * http://blog.stevenlevithan.com/archives/cross-browser-split
  68. */
  69. var nativeSplit = String.prototype.split, compliantExecNpcg = /()??/.exec('')[1] === undefined
  70. String.prototype.split = function(separator, limit) {
  71. var str = this
  72. if (Object.prototype.toString.call(separator) !== '[object RegExp]') {
  73. return nativeSplit.call(str, separator, limit)
  74. }
  75. var output = [],
  76. flags = (separator.ignoreCase ? 'i' : '') +
  77. (separator.multiline ? 'm' : '') +
  78. (separator.extended ? 'x' : '') +
  79. (separator.sticky ? 'y' : ''),
  80. lastLastIndex = 0,
  81. separator2, match, lastIndex, lastLength
  82. separator = new RegExp(separator.source, flags + 'g')
  83. str += ''
  84. if (!compliantExecNpcg) {
  85. separator2 = new RegExp('^' + separator.source + '$(?!\\s)', flags)
  86. }
  87. limit = limit === undefined ? -1 >>> 0 : limit >>> 0
  88. while (match = separator.exec(str)) {
  89. lastIndex = match.index + match[0].length
  90. if (lastIndex > lastLastIndex) {
  91. output.push(str.slice(lastLastIndex, match.index))
  92. if (!compliantExecNpcg && match.length > 1) {
  93. match[0].replace(separator2, function () {
  94. for (var i = 1; i < arguments.length - 2; i++) {
  95. if (arguments[i] === undefined) {
  96. match[i] = undefined
  97. }
  98. }
  99. })
  100. }
  101. if (match.length > 1 && match.index < str.length) {
  102. Array.prototype.push.apply(output, match.slice(1))
  103. }
  104. lastLength = match[0].length
  105. lastLastIndex = lastIndex
  106. if (output.length >= limit) {
  107. break
  108. }
  109. }
  110. if (separator.lastIndex === match.index) {
  111. separator.lastIndex++
  112. }
  113. }
  114. if (lastLastIndex === str.length) {
  115. if (lastLength || !separator.test('')) {
  116. output.push('')
  117. }
  118. } else {
  119. output.push(str.slice(lastLastIndex))
  120. }
  121. return output.length > limit ? output.slice(0, limit) : output
  122. };