tomekwi/map-to.js

View on GitHub
source/array.js

Summary

Maintainability
A
1 hr
Test Coverage
//
// as/array
// -------------------------------------------------------------------------------------------------
// Maps an `{a: "b"}` object to a new array of `{key: "a", value: "b"}` pairs.
//
/**
 * @function asArray
 *
 * @param {Object} object
 *    The object to be mapped.
 *
 * @param {Object} [options]
 * @param {Number} [options.depth=1]
 *    The depth to which the `object`'s tree should be mapped. Set it to `Infinity` to map the
 *    entire tree structure.
 * @param {Boolean} [options.transformArrays=false]
 *    If true, arrays will be transformed. `["a", "b"]` will map to
 *    `[{key: "0", value: "a"}, {key: "1", value: "b"}]`. *
 *
 * @returns {Array}
 *    A new array of key-value pairs mapped from the object.
 */
export default function asArray (object, options) {
  // Parse options.
  if (!options) options = {};
  var depth = (typeof options.depth != "undefined"
    ? options.depth
    : 1
    );
  var transformArrays = !!options.transformArrays;

  // End recursion if we've reached a depth of 0.
  if (!depth) return object;

  // Return if `object` is an Array – unless `options.transformArrays` is set to true.
  if (object instanceof Array && !transformArrays) return object;

  // Create an empty `result` array.
  var result = [];
  // For every `key` of the given `object`:
  for (let key in object) if (object.hasOwnProperty(key)) {  // Can't use iterator because of https://6to5.org/docs/usage/caveats/
    let value = object[key];
    // - recurse if the value is an object
    if (typeof value == "object" && value !== null) {
      value = asArray(value
        , { depth: depth - 1
          , transformArrays: transformArrays
          }
        );
      }
    // - append {key: `key`, value: `object[key]`} to `result`
    result.push({key: key, value: value});
    }

  // Return the `result`.
  return result;
  }