Skip to content

Instantly share code, notes, and snippets.

@nkhil
Last active December 5, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkhil/16645cb3830fabe2cc0ad3923084e62f to your computer and use it in GitHub Desktop.
Save nkhil/16645cb3830fabe2cc0ad3923084e62f to your computer and use it in GitHub Desktop.

Fixture Maker

Working draft of fixture creator. This is a functional implementation.

const uuid = require('uuid');

const ID = uuid();

function isObject(item) {
  return typeof item === 'object' && !Array.isArray(item) && item !== null;
}

function handleNonObject(value, key) {
  if(Array.isArray(value) && value[1] === ID) {
    return handleOurOwn(value, key);
  } else {
    const result = {};
    result[key] = value;
    return result;
  }
}

function handleOurOwn(value, key) {
  let result;
  switch (value[0]) {
    case 'RANDOM_NUMBER':
      result = 23;
      break;
      
      default:
        break;
      }
  const returnValue = {};
  returnValue[key] = result;
  return returnValue;
}

function randomNumber() {
  return [
    'RANDOM_NUMBER',
    ID,
  ]
}

function recursiveFunction(body) {
  const keys = Object.keys(body);
  keys.forEach(key => {
    if(isObject(body[key])) {
      console.log(`key \'${key}\' value is object`);
      recursiveFunction(body[key])
    } else {
      handleNonObject(body[key], key)
    }
  })
}

function orchestrator(body) {
  return recursiveFunction(body, result);
}

module.exports = {
  orchestrator,
  randomNumber,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment