@el3um4s/svelte-get-component-info
Version:
Typescript NPM Package Starter
41 lines (40 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPropDefaultValue = exports.getPropType = exports.getPropName = exports.getPropInfo = void 0;
const utils_1 = require("./utils");
const regex = {
name: /(?<=let )(.*?)(?=\s|;|=|:)/,
type: /(?<=let [:]|[:])(.*?)(?=;|=)/
};
function getPropInfo(s) {
const name = getPropName(s);
const type = getPropType(s);
const defaultValue = getPropDefaultValue(s);
return { name, type, defaultValue };
}
exports.getPropInfo = getPropInfo;
function getPropName(s) {
const nameRegex = s.match(regex.name);
const nameWithDelimiters = nameRegex ? nameRegex[0] : "";
const name = nameWithDelimiters.replace("let", "").replace(/[:|;|=]/gi, "").trim();
return name;
}
exports.getPropName = getPropName;
function getPropType(s) {
const typeRegex = s.match(regex.type);
const positionEquals = s.indexOf("=");
const positionSemicolon = s.indexOf(";");
const firstDelimiters = positionEquals > -1 ? positionEquals : positionSemicolon;
const positionRegex = (typeRegex === null || typeRegex === void 0 ? void 0 : typeRegex.index) ? typeRegex.index : -1;
const type = positionRegex < firstDelimiters && typeRegex ? typeRegex[0].trim() : undefined;
return type;
}
exports.getPropType = getPropType;
function getPropDefaultValue(s) {
const positionEquals = s.indexOf("=");
const positionSemicolon = s.lastIndexOf(";");
const defaultValue = positionEquals < 0 ? undefined : s.substring(positionEquals + 1, positionSemicolon).trim();
const result = defaultValue && (0, utils_1.isStringType)(defaultValue) ? (0, utils_1.getStringWithoutQuote)(defaultValue) : defaultValue;
return result;
}
exports.getPropDefaultValue = getPropDefaultValue;