stoplightio/json

View on GitHub
src/toPropertyPath.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { decodePointerFragment } from './decodePointerFragment';

export function toPropertyPath(path: string) {
  return path
    .replace(/^(\/|#\/)/, '')
    .split('/')
    .map(decodePointerFragment)
    .map(sanitize)
    .join('.');
}

function sanitize(fragment: string) {
  if (fragment.includes('.')) {
    return `["${fragment.replace(/"/g, '\\"')}"]`;
  } else {
    return fragment;
  }
}