Home / File/ formatConsoleArguments.js — react Source File

formatConsoleArguments.js — react Source File

Architecture documentation for formatConsoleArguments.js, a javascript file in the react codebase.

Entity Profile

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */

// Do not add / import anything to this file.
// This function could be used from multiple places, including hook.

// Skips CSS and object arguments, inlines other in the first argument as a template string
export default function formatConsoleArguments(
  maybeMessage: any,
  ...inputArgs: $ReadOnlyArray<any>
): $ReadOnlyArray<any> {
  if (inputArgs.length === 0 || typeof maybeMessage !== 'string') {
    return [maybeMessage, ...inputArgs];
  }

  const args = inputArgs.slice();

  let template = '';
  let argumentsPointer = 0;
  for (let i = 0; i < maybeMessage.length; ++i) {
    const currentChar = maybeMessage[i];
    if (currentChar !== '%') {
      template += currentChar;
      continue;
    }

    const nextChar = maybeMessage[i + 1];
    ++i;

    // Only keep CSS and objects, inline other arguments
    switch (nextChar) {
      case 'c':
      case 'O':
      case 'o': {
        ++argumentsPointer;
        template += `%${nextChar}`;

        break;
      }
      case 'd':
      case 'i': {
        const [arg] = args.splice(argumentsPointer, 1);
        template += parseInt(arg, 10).toString();

        break;
      }
      case 'f': {
        const [arg] = args.splice(argumentsPointer, 1);
        template += parseFloat(arg).toString();

        break;
      }
      case 's': {
        const [arg] = args.splice(argumentsPointer, 1);
        template += String(arg);

        break;
      }

      default:
        template += `%${nextChar}`;
    }
  }

  return [template, ...args];
}

Frequently Asked Questions

What does formatConsoleArguments.js do?
formatConsoleArguments.js is a source file in the react codebase, written in javascript.
Where is formatConsoleArguments.js in the architecture?
formatConsoleArguments.js is located at packages/react-devtools-shared/src/backend/utils/formatConsoleArguments.js (directory: packages/react-devtools-shared/src/backend/utils).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free