diff --git a/packager/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js b/packager/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js index 8620d4883..d5c54b7e9 100644 --- a/packager/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js +++ b/packager/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js @@ -82,6 +82,17 @@ describe('HasteDependencyResolver', function() { 'polyfills/console.js' ], }, + { id: 'polyfills/String.prototype.es6.js', + isPolyfill: true, + path: 'polyfills/String.prototype.es6.js', + dependencies: [ + 'polyfills/prelude.js', + 'polyfills/require.js', + 'polyfills/polyfills.js', + 'polyfills/console.js', + 'polyfills/error-guard.js' + ], + }, module ]); }); @@ -142,6 +153,17 @@ describe('HasteDependencyResolver', function() { 'polyfills/console.js' ], }, + { id: 'polyfills/String.prototype.es6.js', + isPolyfill: true, + path: 'polyfills/String.prototype.es6.js', + dependencies: [ + 'polyfills/prelude_dev.js', + 'polyfills/require.js', + 'polyfills/polyfills.js', + 'polyfills/console.js', + 'polyfills/error-guard.js' + ], + }, module ]); }); @@ -203,6 +225,17 @@ describe('HasteDependencyResolver', function() { 'polyfills/console.js' ], }, + { id: 'polyfills/String.prototype.es6.js', + isPolyfill: true, + path: 'polyfills/String.prototype.es6.js', + dependencies: [ + 'polyfills/prelude.js', + 'polyfills/require.js', + 'polyfills/polyfills.js', + 'polyfills/console.js', + 'polyfills/error-guard.js' + ], + }, { path: 'some module', id: 'some module', isPolyfill: true, @@ -212,6 +245,7 @@ describe('HasteDependencyResolver', function() { 'polyfills/polyfills.js', 'polyfills/console.js', 'polyfills/error-guard.js', + 'polyfills/String.prototype.es6.js' ] }, module diff --git a/packager/react-packager/src/DependencyResolver/haste/index.js b/packager/react-packager/src/DependencyResolver/haste/index.js index e700e5fd0..c18639985 100644 --- a/packager/react-packager/src/DependencyResolver/haste/index.js +++ b/packager/react-packager/src/DependencyResolver/haste/index.js @@ -112,6 +112,7 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function( path.join(__dirname, 'polyfills/polyfills.js'), path.join(__dirname, 'polyfills/console.js'), path.join(__dirname, 'polyfills/error-guard.js'), + path.join(__dirname, 'polyfills/String.prototype.es6.js'), ].concat(this._polyfillModuleNames); var polyfillModules = polyfillModuleNames.map( diff --git a/packager/react-packager/src/DependencyResolver/haste/polyfills/String.prototype.es6.js b/packager/react-packager/src/DependencyResolver/haste/polyfills/String.prototype.es6.js new file mode 100644 index 000000000..afc68d76a --- /dev/null +++ b/packager/react-packager/src/DependencyResolver/haste/polyfills/String.prototype.es6.js @@ -0,0 +1,85 @@ +/** + * @provides String.prototype.es6 + * @polyfill + */ + +/*eslint global-strict:0, no-extend-native:0, no-bitwise:0 */ +/*jshint bitwise:false*/ + +/* + * NOTE: We use (Number(x) || 0) to replace NaN values with zero. + */ + +if (!String.prototype.startsWith) { + String.prototype.startsWith = function(search) { + 'use strict'; + if (this == null) { + throw TypeError(); + } + var string = String(this); + var pos = arguments.length > 1 ? + (Number(arguments[1]) || 0) : 0; + var start = Math.min(Math.max(pos, 0), string.length); + return string.indexOf(String(search), pos) === start; + }; +} + +if (!String.prototype.endsWith) { + String.prototype.endsWith = function(search) { + 'use strict'; + if (this == null) { + throw TypeError(); + } + var string = String(this); + var stringLength = string.length; + var searchString = String(search); + var pos = arguments.length > 1 ? + (Number(arguments[1]) || 0) : stringLength; + var end = Math.min(Math.max(pos, 0), stringLength); + var start = end - searchString.length; + if (start < 0) { + return false; + } + return string.lastIndexOf(searchString, start) === start; + }; +} + +if (!String.prototype.contains) { + String.prototype.contains = function(search) { + 'use strict'; + if (this == null) { + throw TypeError(); + } + var string = String(this); + var pos = arguments.length > 1 ? + (Number(arguments[1]) || 0) : 0; + return string.indexOf(String(search), pos) !== -1; + }; +} + +if (!String.prototype.repeat) { + String.prototype.repeat = function(count) { + 'use strict'; + if (this == null) { + throw TypeError(); + } + var string = String(this); + count = Number(count) || 0; + if (count < 0 || count === Infinity) { + throw RangeError(); + } + if (count === 1) { + return string; + } + var result = ''; + while (count) { + if (count & 1) { + result += string; + } + if ((count >>= 1)) { + string += string; + } + } + return result; + }; +}