import { has } from 'lodash'; import { isObject } from 'util'; import { ConfigNodePropertyPath } from './types'; const ConfigNodePropertyPathDelimiters = ['.', '/']; export const configNodePropertyPathToArray = (path: ConfigNodePropertyPath): string[] => { if (!path) { return []; } else if (Array.isArray(path)) { return path; } else { const delim = ConfigNodePropertyPathDelimiters.find(d => path.indexOf(d) > 0); if (delim) { return path.split(delim); } return [path]; } }; export const areConfigNodePropertyPathsEqual = (p1: ConfigNodePropertyPath, p2: ConfigNodePropertyPath) => { const ap1 = configNodePropertyPathToArray(p1); const ap2 = configNodePropertyPathToArray(p2); if (ap1.length === ap2.length) { for (let i = 0; i < ap1.length; ++i) { if (ap1[i] !== ap2[i]) { return false; } } return true; } return false; }; /** * Trys to determine (guess) whether argument is of type ConfigNodeArgs. * @param arg argument that may be a ConfigNodeArgs type */ export const isConfigNodeArgs = (arg: any, extendedProps: string | string[] = []) => { const props = ['inheritedNodes', 'initialValue', 'propertyNodeFactories', 'options'].concat( Array.isArray(extendedProps) ? extendedProps : [extendedProps] ); return !Array.isArray(arg) && isObject(arg) && props.some(p => has(arg, p)); };