Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

NonASCIIStringSnapshotSerializer.js.flow 1.1 KiB

il y a 3 ans
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Copyright 2004-present Facebook. All Rights Reserved.
  3. *
  4. * @emails oncall+ads_integration_management
  5. * @flow strict-local
  6. * @format
  7. */
  8. 'use strict';
  9. const 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. const NonASCIIStringSnapshotSerializer = {
  16. test(val: mixed): boolean {
  17. if (typeof val !== 'string') {
  18. return false;
  19. }
  20. for (let 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: (val: string): string => {
  28. return '"' + val.split('').map(char => {
  29. const 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;