/**
 * Copyright 2004-present Facebook. All Rights Reserved.
 *
 * @emails oncall+ads_integration_management
 * @flow strict-local
 * @format
 */

'use strict';

const MAX_ASCII_CHARACTER = 127;

/**
 * Serializes strings with non-ASCII characters to their Unicode escape
 * sequences (eg. \u2022), to avoid hitting this lint rule:
 * "Source code should only include printable US-ASCII bytes"
 */
const NonASCIIStringSnapshotSerializer = {
  test(val: mixed): boolean {
    if (typeof val !== 'string') {
      return false;
    }
    for (let i = 0; i < val.length; i++) {
      if (val.charCodeAt(i) > MAX_ASCII_CHARACTER) {
        return true;
      }
    }
    return false;
  },

  print: (val: string): string => {
    return (
      '"' +
      val
        .split('')
        .map(char => {
          const code = char.charCodeAt(0);
          return code > MAX_ASCII_CHARACTER
            ? '\\u' + code.toString(16).padStart(4, '0')
            : char;
        })
        .join('')
        // Keep the same behaviour as Jest's regular string snapshot
        // serialization, which escapes double quotes.
        .replace(/"/g, '\\"') +
      '"'
    );
  },
};

module.exports = NonASCIIStringSnapshotSerializer;

/**
 * Copyright 2004-present Facebook. All Rights Reserved.
 *
 * @emails oncall+ads_integration_management
 * @flow strict-local
 * @format
 */

'use strict';

const MAX_ASCII_CHARACTER = 127;

/**
 * Serializes strings with non-ASCII characters to their Unicode escape
 * sequences (eg. \u2022), to avoid hitting this lint rule:
 * "Source code should only include printable US-ASCII bytes"
 */
const NonASCIIStringSnapshotSerializer = {
  test(val: mixed): boolean {
    if (typeof val !== 'string') {
      return false;
    }
    for (let i = 0; i < val.length; i++) {
      if (val.charCodeAt(i) > MAX_ASCII_CHARACTER) {
        return true;
      }
    }
    return false;
  },

  print: (val: string): string => {
    return (
      '"' +
      val
        .split('')
        .map((char) => {
          const code = char.charCodeAt(0);
          return code > MAX_ASCII_CHARACTER
            ? '\\u' + code.toString(16).padStart(4, '0')
            : char;
        })
        .join('')
        // Keep the same behaviour as Jest's regular string snapshot
        // serialization, which escapes double quotes.
        .replace(/"/g, '\\"') +
      '"'
    );
  },
};

module.exports = NonASCIIStringSnapshotSerializer;

Format JavaScript files with Prettier: scripts/draft-js/__github__/src
Format JavaScript files with Prettier: scripts/draft-js/__github__/src

Differential Revision: D32201648

fbshipit-source-id: f94342845cbe6454bb7ae4f02814e788fb25de9f
