stringifier-backup1

by shan10213223

JavaScript

// Returns a stringified version of input,
// behaving in exactly the same way as JSON.stringify()
function stringifier (input) {
  // basic type - number
  if (typeof(input) === 'number') {
    return input.toString();
  }
    
  // basic type - string
  if (typeof(input) === 'string') {
    return '"' + input + '"';
  }

  // basic type - function
  if (typeof(input) === 'function') {
    return undefined;
  }
  // basic type - empty string
  if (input === '') {
    return '""';
  }

  // basic type - null
  if (input === null) {
    return 'null';
  }
  
  if (input === undefined) {
    return undefined;
  }

  // helper function - handle basic types in array
  function stringifierBasics (input) {
    if (input === null) {
      return 'null';
    }
    if (input === '') {
      return '""';
    }
    if (input === undefined) {
      return 'null';
    }
    let text;
    switch (typeof(input)) {
    case 'number':
      text = input;
      break;
    case 'string':
      //text = input;
      text = '"' + input + '"';
      break;
    case 'function':
      text = 'null';
      break;
    }
    return text;
  }
  
  // helper function - check if simple types
  function isSimpleObj (input) {
    if (input === undefined || input === null || input === '' || typeof(input) === 'number' || typeof(input) === 'string' || typeof(input) === 'function' || typeof(input) === 'boolean') {
      return true;
    } else {
      return false;
    }
  }

  // array
  if (Array.isArray(input)) {
    let outArray = [];
    for (let i=0; i<input.length; i++) {
      if (Object.prototype.hasOwnProperty.call(input, i)) {
        // if it's simple object
        if (isSimpleObj(input[i])) {
          let txt = stringifierBasics (input[i]);
          outArray.push(txt);
        // if it's a complex object
        } else {
          let txt = stringifier (input[i]);
          outArray.push(txt);
        }
      }
    }
    return '[' + outArray + ']';
  // object
  } else {
    let...