'use strict';

module.exports = {
  deref: deref
};

function deref (pointer, doc) {
  const segments = compact(pointer.split('/'));

  if(segments.length === 0) {
    return doc;
  }

  let context = doc;
  segments.forEach((segment) => { context = context[segment]; });
  return context;
}

function compact (collection) {
  return collection.filter((item) => item);
}

'use strict';

module.exports = {
  deref: deref
};

// TODO put the doc first
function deref (pointer, doc) {
  const segments = compact(pointer.split('/'));
  return traverse(doc, segments);
}

function traverse (doc, segments) {
  if(!segments.length) return doc;

  return traverse(doc[first(segments)], segments.slice(1));
}

function compact (collection) {
  return collection.filter((item) => item);
}

function first (arr) {
  return arr[0];
}

Use recursive function for traversal - babel will transform to a linear iterative process
Use recursive function for traversal - babel will transform to a linear iterative process
