rebuild.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Module dependencies
  3. */
  4. var rebuildRecursive = require('./helpers/rebuild-recursive');
  5. /**
  6. * rebuild()
  7. *
  8. * Rebuild a potentially-recursively-deep value, running
  9. * the specified `handleLeafTransform` lifecycle callback
  10. * (aka transformer function) for every primitive (i.e. string,
  11. * number, boolean, null) and function.
  12. *
  13. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. * @param {Anything} val
  15. *
  16. * @param {Function} handleLeafTransform [run AFTER stringification of Errors, Dates, etc.]
  17. * @param {Anything} leafVal
  18. * @param {String} leafType [either 'string', 'number', 'boolean', 'null', or 'lamda']
  19. * @return {Anything} [transformed version of `leafVal`]
  20. *
  21. * @param {Function} handleCompositeTransform [run BEFORE recursion and stripping of undefined items/props]
  22. * @param {Dictionary|Array} compositeVal
  23. * @param {String} leafType [either 'array' or 'dictionary']
  24. * @return {Dictionary|Array} [transformed version of `compositeVal`-- MUST BE A DICTONARY OR ARRAY THAT IS SAFE TO RECURSIVELY DIVE INTO!!!]
  25. *
  26. * @returns {JSONCompatible}
  27. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  28. * Example usage:
  29. *
  30. * ```
  31. * rttc.rebuild({
  32. * foo: [
  33. * 3,
  34. * ['hey', 'yo', 235, {}]
  35. * ]
  36. * },
  37. * function transformPrimitive(val, type){ return val;},
  38. * function transformDictOrArray(compositeThing, type) {
  39. * if (type === 'array') { return compositeThing.slice(1); }
  40. * else { return compositeThing; }
  41. * })
  42. *
  43. * // Yields:
  44. * // { foo: [ [ 'yo', 235, {} ] ] }
  45. * ```
  46. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  47. */
  48. module.exports = function rebuild(val, handleLeafTransform, handleCompositeTransform){
  49. return rebuildRecursive(val, handleLeafTransform, handleCompositeTransform);
  50. };