0397b08b8d9fbf9d3fe7f530e9dc81a99a731c1e26e70f1c56a03ffad873ca9ef6481b908f869bba2c4fe6d7165ca80255a36a72c4fcf2c37d8552e3c778c0 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var forEach = require('for-each');
  3. module.exports = function (trim, t) {
  4. t.test('normal cases', function (st) {
  5. st.equal(trim(' \t\na \t\n'), 'a', 'strips whitespace off left and right sides');
  6. st.equal(trim('a'), 'a', 'noop when no whitespace');
  7. var allWhitespaceChars = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF';
  8. st.equal(trim(allWhitespaceChars + 'a' + allWhitespaceChars), 'a', 'all expected whitespace chars are trimmed');
  9. st.end();
  10. });
  11. // see https://codeblog.jonskeet.uk/2014/12/01/when-is-an-identifier-not-an-identifier-attack-of-the-mongolian-vowel-separator/
  12. var mongolianVowelSeparator = '\u180E';
  13. var mvsIsWS = (/^\s$/).test('\u180E');
  14. t.test('unicode >= 4 && < 6.3', function (st) {
  15. st.equal(
  16. trim(mongolianVowelSeparator + 'a' + mongolianVowelSeparator),
  17. mvsIsWS ? 'a' : mongolianVowelSeparator + 'a' + mongolianVowelSeparator,
  18. 'mongolian vowel separator is' + (mvsIsWS ? '' : ' not') + ' whitespace'
  19. );
  20. st.equal(
  21. trim(mongolianVowelSeparator),
  22. mvsIsWS ? '' : mongolianVowelSeparator
  23. );
  24. st.equal(
  25. trim('_' + mongolianVowelSeparator),
  26. '_' + (mvsIsWS ? '' : mongolianVowelSeparator)
  27. );
  28. st.equal(
  29. trim(mongolianVowelSeparator + '_'),
  30. (mvsIsWS ? '' : mongolianVowelSeparator) + '_'
  31. );
  32. st.end();
  33. });
  34. t.test('zero-width spaces', function (st) {
  35. var zeroWidth = '\u200b';
  36. st.equal(trim(zeroWidth), zeroWidth, 'zero width space does not trim');
  37. st.end();
  38. });
  39. t.test('non-whitespace characters', function (st) {
  40. // Zero-width space (zws), next line character (nel), and non-character (bom) are not whitespace.
  41. var nonWhitespaces = {
  42. '\\u0085': '\u0085',
  43. '\\u200b': '\u200b',
  44. '\\ufffe': '\ufffe'
  45. };
  46. forEach(nonWhitespaces, function (nonWhitespace, name) {
  47. st.equal(trim(nonWhitespace), nonWhitespace, name + ' does not trim');
  48. });
  49. st.end();
  50. });
  51. };