UNPKG

@theguild/federation-composition

Version:
83 lines (82 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseContextReference = parseContextReference; exports.isValidContextName = isValidContextName; const notAReference = { context: undefined, selection: undefined, }; 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), }; } 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; }