es6.string.starts-with.js 784 B

12345678910111213141516171819
  1. // 21.1.3.18 String.prototype.startsWith(searchString [, position ])
  2. 'use strict';
  3. var $export = require('./$.export')
  4. , toLength = require('./$.to-length')
  5. , context = require('./$.string-context')
  6. , STARTS_WITH = 'startsWith'
  7. , $startsWith = ''[STARTS_WITH];
  8. $export($export.P + $export.F * require('./$.fails-is-regexp')(STARTS_WITH), 'String', {
  9. startsWith: function startsWith(searchString /*, position = 0 */){
  10. var that = context(this, searchString, STARTS_WITH)
  11. , $$ = arguments
  12. , index = toLength(Math.min($$.length > 1 ? $$[1] : undefined, that.length))
  13. , search = String(searchString);
  14. return $startsWith
  15. ? $startsWith.call(that, search, index)
  16. : that.slice(index, index + search.length) === search;
  17. }
  18. });