agalazis/ssm-params-convict-adapter

View on GitHub
src/util/deep-reduce.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { isObject, reduce } from 'lodash';
export const deepReduce = (
  obj: any,
  iterator: any,
  accInitializer: any,
  context = obj,
  path?: string | undefined,
  root = obj,
) => {
  return reduce(
    obj,
    (acc: any, val: any, key: string) => {
      iterator.call(context, acc, val, key, obj, path, root);
      if (isObject(val)) {
        /*&& !_.isDate(val)*/
        deepReduce(
          val,
          iterator,
          acc,
          context,
          (path && `${path}.${key}`) || key,
          root,
        );
      }
      return acc;
    },
    accInitializer,
  );
};