$.string-trim.js 941 B

1234567891011121314151617181920212223242526272829
  1. var $export = require('./$.export')
  2. , defined = require('./$.defined')
  3. , fails = require('./$.fails')
  4. , spaces = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' +
  5. '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF'
  6. , space = '[' + spaces + ']'
  7. , non = '\u200b\u0085'
  8. , ltrim = RegExp('^' + space + space + '*')
  9. , rtrim = RegExp(space + space + '*$');
  10. var exporter = function(KEY, exec){
  11. var exp = {};
  12. exp[KEY] = exec(trim);
  13. $export($export.P + $export.F * fails(function(){
  14. return !!spaces[KEY]() || non[KEY]() != non;
  15. }), 'String', exp);
  16. };
  17. // 1 -> String#trimLeft
  18. // 2 -> String#trimRight
  19. // 3 -> String#trim
  20. var trim = exporter.trim = function(string, TYPE){
  21. string = String(defined(string));
  22. if(TYPE & 1)string = string.replace(ltrim, '');
  23. if(TYPE & 2)string = string.replace(rtrim, '');
  24. return string;
  25. };
  26. module.exports = exporter;