16 lines
307 B
TypeScript
16 lines
307 B
TypeScript
import {
|
|
isSafePath,
|
|
} from './isSafePath';
|
|
|
|
export const createGetValueFunctionBody = (path: string): string => {
|
|
if (!isSafePath(path)) {
|
|
throw new Error('Unsafe path.');
|
|
}
|
|
|
|
const body = 'return subject' + path;
|
|
|
|
return body
|
|
.replace(/(\.(\d+))/g, '.[$2]')
|
|
.replace(/\./g, '?.');
|
|
};
|