react/packages/react-devtools-shared/src/backend/utils.js

40 lines
980 B
JavaScript
Raw Normal View History

2019-01-23 03:04:37 +08:00
// @flow
import { dehydrate } from '../hydration';
2019-04-03 02:53:46 +08:00
import type { DehydratedData } from 'src/devtools/views/Components/types';
export function cleanForBridge(
data: Object | null,
2019-06-18 05:30:34 +08:00
isPathWhitelisted: (path: Array<string | number>) => boolean,
path?: Array<string | number> = []
): DehydratedData | null {
if (data !== null) {
const cleaned = [];
return {
2019-06-18 05:30:34 +08:00
data: dehydrate(data, cleaned, path, isPathWhitelisted),
cleaned,
};
} else {
return null;
}
}
export function copyWithSet(
obj: Object | Array<any>,
path: Array<string | number>,
value: any,
index: number = 0
): Object | Array<any> {
2019-06-18 05:30:34 +08:00
console.log('[utils] copyWithSet()', obj, path, index, value);
if (index >= path.length) {
return value;
}
const key = path[index];
const updated = Array.isArray(obj) ? obj.slice() : { ...obj };
// $FlowFixMe number or string is fine here
updated[key] = copyWithSet(obj[key], path, value, index + 1);
return updated;
}