_string-trim.js 899 B

123456789101112131415161718192021222324252627282930
  1. var $export = require('./_export');
  2. var defined = require('./_defined');
  3. var fails = require('./_fails');
  4. var spaces = require('./_string-ws');
  5. var space = '[' + spaces + ']';
  6. var non = '\u200b\u0085';
  7. var ltrim = RegExp('^' + space + space + '*');
  8. var rtrim = RegExp(space + space + '*$');
  9. var exporter = function (KEY, exec, ALIAS) {
  10. var exp = {};
  11. var FORCE = fails(function () {
  12. return !!spaces[KEY]() || non[KEY]() != non;
  13. });
  14. var fn = exp[KEY] = FORCE ? exec(trim) : spaces[KEY];
  15. if (ALIAS) exp[ALIAS] = fn;
  16. $export($export.P + $export.F * FORCE, 'String', exp);
  17. };
  18. // 1 -> String#trimLeft
  19. // 2 -> String#trimRight
  20. // 3 -> String#trim
  21. var trim = exporter.trim = function (string, TYPE) {
  22. string = String(defined(string));
  23. if (TYPE & 1) string = string.replace(ltrim, '');
  24. if (TYPE & 2) string = string.replace(rtrim, '');
  25. return string;
  26. };
  27. module.exports = exporter;