Files
omnivore/packages/liqe/src/createGetValueFunctionBody.ts
2023-12-07 18:47:25 +08:00

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, '?.');
};