utils.js
/**
 * Takes a value in and attempts to flatten the keys of all underlying objects.
 * @param {any} value
 *   Structure whose underlying keys should be flattened.
 * @returns {any}
 *   If `value` is array-like then a new array will be returned with all
 *   underlying items recursively flattened.  If `value` is an iterable object
 *   then a new object will be returned with all keys flattened therein and also
 *   in any objects that are contained in underlying array-like structures.  In
 *   all other cases `value` will simply be returned.
 */
Takes a value in and attempts to flatten the keys of all underlying objects.
@param {any} value
Structure whose underlying keys should be flattened.
@returns {any}
If `value` is array-like then a new array will be returned with all
underlying items recursively flattened.  If `value` is an iterable object
then a new object will be returned with all keys flattened therein and also
in any objects that are contained in underlying array-like structures.  In
all other cases `value` will simply be returned.
Takes a value in and attempts to flatten the keys of all underlying objects . @param { any } value Structure whose underlying keys should be flattened . @returns { any } If ` value ` is array - like then a new array will be returned with all underlying items recursively flattened . If ` value ` is an iterable object then a new object will be returned with all keys flattened therein and also in any objects that are contained in underlying array - like structures . In all other cases ` value ` will simply be returned .
function flattenObjectKeys(value) {
  function recurse(seen, value, path, root) {
    if (value && 'object' === typeof value && !(value instanceof Date)) {
      if (seen.includes(value)) {
        throw new Error('Cannot flatten recursive structures.');
      }
      seen = seen.concat([value]);
      if ('function' === typeof value[Symbol.iterator]) {
        const newArray = [];
        for (let [k, v] of Object.entries(value)) {
          newArray[k] = recurse(seen, v);
        }
        if (root) {
          root[path.join('.')] = newArray;
        }
        return newArray;
      }
      else {
        root ??= {};
        for (let [k, v] of Object.entries(value)) {
          recurse(seen, v, path ? path.concat([k]) : [k], root);
        }
        return root;
      }
    }
    if (root) {
      root[path.join('.')] = value;
    }
    return value;
  }
  return recurse([], value);
}
function flattenObjectKeys ( value ) { function recurse ( seen , value , path , root ) { if ( value && 'object' === typeof value && ! ( value instanceof Date ) ) { if ( seen . includes ( value ) ) { throw new Error ( 'Cannot flatten recursive structures.' ) ; } seen = seen . concat ( [ value ] ) ; if ( 'function' === typeof value [ Symbol . iterator ] ) { const newArray = [ ] ; for ( let [ k , v ] of Object . entries ( value ) ) { newArray [ k ] = recurse ( seen , v ) ; } if ( root ) { root [ path . join ( '.' ) ] = newArray ; } return newArray ; } else { root ??= { } ; for ( let [ k , v ] of Object . entries ( value ) ) { recurse ( seen , v , path ? path . concat ( [ k ] ) : [ k ] , root ) ; } return root ; } } if ( root ) { root [ path . join ( '.' ) ] = value ; } return value ; } return recurse ( [ ] , value ) ; }
Takes a value in and attempts to flatten the keys of all underlying objects.
Takes a value in and attempts to flatten the keys of all underlying objects .
// If is an iterable object or an array.... // Protect against recursive structures. // If is an iterable object... // If is an array... 