constants extraction

constants extraction
This commit is contained in:
Luis Tejeda
2022-05-05 12:37:23 -05:00
committed by Erik Mendoza
parent 1a208ab0ed
commit 789f2e3522
2 changed files with 17 additions and 8 deletions

View File

@@ -1,9 +1,12 @@
const slashes = /\//g,
tildes = /~/g,
escapedSlash = /~1/g,
escapedSlashString = '~1',
componentsKey = 'components',
localPointer = '#',
escapedTilde = /~0/g;
escapedTilde = /~0/g,
jsonPointerLevelSeparator = '/',
escapedTildeString = '~0';
/**
* Encodes a filepath name so it can be a json pointer
@@ -12,7 +15,7 @@ const slashes = /\//g,
* @returns {string} - the encoded filepath
*/
function jsonPointerEncodeAndReplace(filePathName) {
return encodeURIComponent(filePathName.replace(tildes, '~0').replace(slashes, '~1'));
return encodeURIComponent(filePathName.replace(tildes, escapedTildeString).replace(slashes, escapedSlashString));
}
/**
@@ -22,7 +25,7 @@ function jsonPointerEncodeAndReplace(filePathName) {
* @returns {string} - the encoded filepath
*/
function jsonPointerDecodeAndReplace(filePathName) {
return decodeURIComponent(filePathName.replace(escapedSlash, '/').replace(escapedTilde, '~'));
return decodeURIComponent(filePathName.replace(escapedSlash, jsonPointerLevelSeparator).replace(escapedTilde, '~'));
}
/**
@@ -39,10 +42,10 @@ function getKeyInComponents(componentName, filePathName, localPath) {
res.push(componentName);
res.push(jsonPointerDecodeAndReplace(filePathName));
if (localPath) {
if (localPath.startsWith('/')) {
if (localPath.startsWith(jsonPointerLevelSeparator)) {
localPathToCheck = localPath.substring(1);
}
pointer = localPathToCheck.split('/');
pointer = localPathToCheck.split(jsonPointerLevelSeparator);
for (let i = 0; i < pointer.length; i++) {
pointer[i] = jsonPointerDecodeAndReplace(pointer[i]);
}
@@ -75,11 +78,12 @@ function getLocalPath(jsonPointer) {
function concatJsonPointer(encodeFunction, filePathName, componentName, localPath) {
let base = '',
local = '';
base = '/' + encodeFunction(filePathName);
base = jsonPointerLevelSeparator + encodeFunction(filePathName);
if (localPath) {
local = localPath;
}
return localPointer + '/' + componentsKey + '/' + componentName + base + local;
return localPointer + jsonPointerLevelSeparator + componentsKey +
jsonPointerLevelSeparator + componentName + base + local;
}
/**
@@ -176,7 +180,7 @@ function getEntityName(jsonPointer) {
if (!jsonPointer) {
return '';
}
let segment = jsonPointer.substring(jsonPointer.lastIndexOf('/') + 1);
let segment = jsonPointer.substring(jsonPointer.lastIndexOf(jsonPointerLevelSeparator) + 1);
return segment;
}

View File

@@ -13,6 +13,11 @@ function added(path, referencesInNode) {
return referencesInNode.find((reference) => { return reference.path === path; }) !== undefined;
}
/**
* Gets the path from a referenced entity
* @param {object} property - current node in process
* @returns {string} - $ref value
*/
function pathSolver(property) {
return property.$ref;
}