@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
79 lines (78 loc) • 1.93 kB
JavaScript
const notAReference = {
context: undefined,
selection: undefined,
};
export function parseContextReference(input) {
let pos = skipIgnoredTokens(input, 0);
if (input[pos] !== "$") {
return notAReference;
}
pos = skipIgnoredTokens(input, pos + 1);
if (!isNameStart(input[pos])) {
return notAReference;
}
const contextStart = pos++;
while (isNameContinue(input[pos])) {
pos++;
}
return {
context: input.slice(contextStart, pos),
selection: input.slice(pos),
};
}
export function isValidContextName(name) {
if (!isAsciiLetter(name[0])) {
return false;
}
for (let pos = 1; pos < name.length; pos++) {
if (!isAsciiLetter(name[pos]) && !isDigit(name[pos])) {
return false;
}
}
return true;
}
function skipIgnoredTokens(input, start) {
let pos = start;
while (pos < input.length) {
const char = input[pos];
if (char === " " ||
char === "\t" ||
char === "\n" ||
char === "\r" ||
char === "," ||
char === "\ufeff") {
pos++;
continue;
}
if (char === "#") {
pos++;
while (pos < input.length && input[pos] !== "\n" && input[pos] !== "\r") {
pos++;
}
continue;
}
break;
}
return pos;
}
function isNameStart(char) {
return char === "_" || isAsciiLetter(char);
}
function isNameContinue(char) {
return isNameStart(char) || isDigit(char);
}
function isAsciiLetter(char) {
if (!char) {
return false;
}
const code = char.charCodeAt(0);
return ((code >= 65 && code <= 90) ||
(code >= 97 && code <= 122));
}
function isDigit(char) {
if (!char) {
return false;
}
const code = char.charCodeAt(0);
return code >= 48 && code <= 57;
}