You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NonASCIIStringSnapshotSerializer.js 1.1 KiB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright 2004-present Facebook. All Rights Reserved.
  3. *
  4. * @emails oncall+ads_integration_management
  5. *
  6. * @format
  7. */
  8. 'use strict';
  9. var MAX_ASCII_CHARACTER = 127;
  10. /**
  11. * Serializes strings with non-ASCII characters to their Unicode escape
  12. * sequences (eg. \u2022), to avoid hitting this lint rule:
  13. * "Source code should only include printable US-ASCII bytes"
  14. */
  15. var NonASCIIStringSnapshotSerializer = {
  16. test: function test(val) {
  17. if (typeof val !== 'string') {
  18. return false;
  19. }
  20. for (var i = 0; i < val.length; i++) {
  21. if (val.charCodeAt(i) > MAX_ASCII_CHARACTER) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. },
  27. print: function print(val) {
  28. return '"' + val.split('').map(function (char) {
  29. var code = char.charCodeAt(0);
  30. return code > MAX_ASCII_CHARACTER ? "\\u" + code.toString(16).padStart(4, '0') : char;
  31. }).join('') // Keep the same behaviour as Jest's regular string snapshot
  32. // serialization, which escapes double quotes.
  33. .replace(/"/g, '\\"') + '"';
  34. }
  35. };
  36. module.exports = NonASCIIStringSnapshotSerializer;