packages/relay-compiler/util/dedupeJSONStringify.js
function printJSCode(isDupedVar, depth, value) {
    if (value == null || typeof value !== 'object') {
      return JSON.stringify(value);
    }
    // Only use variable references at depth beyond the top level.
    if (depth !== '') {
      const metadata = metadataForVal.get(value);
      if (metadata && metadata.isDuplicate) {
        if (!metadata.varName) {
          const refCode = printJSCode(true, '', value);
          metadata.varName = 'v' + varDefs.length;
          varDefs.push(metadata.varName + ' = ' + refCode);
        }
        return '(' + metadata.varName + '/*: any*/)';
      }
    }
    let str;
    let isEmpty = true;
    const depth2 = depth + '  ';
    if (Array.isArray(value)) {
      // Empty arrays can only have one inferred flow type and then conflict if
      // used in different places, this is unsound if we would write to them but
      // this whole module is based on the idea of a read only JSON tree.
      if (isDupedVar && value.length === 0) {
        return '([]/*: any*/)';
      }
      str = '[';
      for (let i = 0; i < value.length; i++) {
        str +=
          (isEmpty ? '\n' : ',\n') +
          depth2 +
          printJSCode(isDupedVar, depth2, value[i]);
        isEmpty = false;
      }
      str += isEmpty ? ']' : `\n${depth}]`;
    } else {
      str = '{';
      for (const k in value) {
        if (value.hasOwnProperty(k) && value[k] !== undefined) {
          str +=
            (isEmpty ? '\n' : ',\n') +
            depth2 +
            JSON.stringify(k) +
            ': ' +
            printJSCode(isDupedVar, depth2, value[k]);
          isEmpty = false;
        }
      }
      str += isEmpty ? '}' : `\n${depth}}`;
    }
    return str;
  }
function printJSCode(isDupedVar, depth, value) {
    if (value == null || typeof value !== 'object') {
      return JSON.stringify(value);
    }
    // Only use variable references at depth beyond the top level.
    if (depth !== '') {
      const metadata = metadataForVal.get(value);
      if (metadata && metadata.isDuplicate) {
        if (!metadata.varName) {
          const refCode = printJSCode(true, '', value);
          metadata.varName = 'v' + varDefs.length;
          varDefs.push(metadata.varName + ' = ' + refCode);
        }
        return '(' + metadata.varName + '/*: any*/)';
      }
    }
    let str;
    let isEmpty = true;
    const depth2 = depth + '  ';
    if (Array.isArray(value)) {
      // Empty arrays can only have one inferred flow type and then conflict if
      // used in different places, this is unsound if we would write to them but
      // this whole module is based on the idea of a read only JSON tree.
      if (isDupedVar && value.length === 0) {
        return '([]/*: any*/)';
      }
      str = '[';
      for (let i = 0; i < value.length; i++) {
        str +=
          (isEmpty ? '\n' : ',\n') +
          depth2 +
          printJSCode(isDupedVar, depth2, value[i]);
        isEmpty = false;
      }
      str += isEmpty ? ']' : `\n${depth}]`;
    } else {
      str = '{';
      for (const k in value) {
        if (value.hasOwnProperty(k) && value[k] !== undefined) {
          str +=
            (isEmpty ? '\n' : ',\n') +
            depth2 +
            JSON.stringify(k) +
            ': ' +
            printJSCode(isDupedVar, depth2, value[k]);
          isEmpty = false;
        }
      }
      str += isEmpty ? '}' : `\n${depth}}`;
    }
    return str;
  }
function printJSCode ( isDupedVar , depth , value ) { if ( value == null || typeof value !== 'object' ) { return JSON . stringify ( value ) ; } // Only use variable references at depth beyond the top level. if ( depth !== '' ) { const metadata = metadataForVal . get ( value ) ; if ( metadata && metadata . isDuplicate ) { if ( ! metadata . varName ) { const refCode = printJSCode ( true , '' , value ) ; metadata . varName = 'v' + varDefs . length ; varDefs . push ( metadata . varName + ' = ' + refCode ) ; } return '(' + metadata . varName + '/*: any*/)' ; } } let str ; let isEmpty = true ; const depth2 = depth + '  ' ; if ( Array . isArray ( value ) ) { // Empty arrays can only have one inferred flow type and then conflict if // used in different places, this is unsound if we would write to them but // this whole module is based on the idea of a read only JSON tree. if ( isDupedVar && value . length === 0 ) { return '([]/*: any*/)' ; } str = '[' ; for ( let i = 0 ; i < value . length ; i ++ ) { str += ( isEmpty ? '\n' : ',\n' ) + depth2 + printJSCode ( isDupedVar , depth2 , value [ i ] ) ; isEmpty = false ; } str += isEmpty ? ']' : ` \n ${ depth } ` ; } else { str = '{' ; for ( const k in value ) { if ( value . hasOwnProperty ( k ) && value [ k ] !== undefined ) { str += ( isEmpty ? '\n' : ',\n' ) + depth2 + JSON . stringify ( k ) + ': ' + printJSCode ( isDupedVar , depth2 , value [ k ] ) ; isEmpty = false ; } } str += isEmpty ? '}' : ` \n ${ depth } ` ; } return str ; }
Stringify JS, replacing duplicates with variable references.
Stringify JS replacing duplicates with variable references .