noodl-loader
Version:
Loader for noodl applications
96 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toPathname = exports.toObject = exports.removeSuffix = exports.quoteIfEmptyStr = exports.pageName = exports.joinPaths = exports.endpoint = exports.ensureSuffix = exports.appKey = exports.configKey = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const noodl_core_1 = require("noodl-core");
const yml_1 = require("./yml");
function configKey(value) {
if (!noodl_core_1.is.str(value))
return String(value);
value = value.replace(/(_en|\.yml$)/g, '');
return value;
}
exports.configKey = configKey;
function appKey(value) {
return configKey(value);
}
exports.appKey = appKey;
function ensureSuffix(suffix, value = '') {
var _a;
if (!((_a = value.endsWith) === null || _a === void 0 ? void 0 : _a.call(value, suffix)))
value += suffix;
return value;
}
exports.ensureSuffix = ensureSuffix;
function endpoint(path1, path2) {
var _a, _b, _c;
let current = [path1, path2];
let endpoint = '';
while (current.length) {
let nextPath = current.shift();
if (endpoint === '') {
if ((_a = nextPath === null || nextPath === void 0 ? void 0 : nextPath.startsWith) === null || _a === void 0 ? void 0 : _a.call(nextPath, 'http')) {
endpoint += nextPath;
continue;
}
}
if (nextPath) {
if ((_b = endpoint.endsWith) === null || _b === void 0 ? void 0 : _b.call(endpoint, '/')) {
endpoint = endpoint.substring(0, endpoint.length - 1);
}
if (!((_c = nextPath.startsWith) === null || _c === void 0 ? void 0 : _c.call(nextPath, '/')))
nextPath = `/${nextPath}`;
endpoint += nextPath;
}
}
return endpoint;
}
exports.endpoint = endpoint;
function joinPaths(...paths) {
return path_1.default.join(...paths.map(String));
}
exports.joinPaths = joinPaths;
function pageName(value) {
return configKey(value);
}
exports.pageName = pageName;
function quoteIfEmptyStr(value) {
return value === '' ? "''" : value;
}
exports.quoteIfEmptyStr = quoteIfEmptyStr;
function removeSuffix(suffix, value = '') {
var _a;
if ((_a = value.endsWith) === null || _a === void 0 ? void 0 : _a.call(value, suffix))
value = value.substring(0, value.length);
return value;
}
exports.removeSuffix = removeSuffix;
function toObject(value) {
const props = {};
if (noodl_core_1.is.obj(value)) {
if ((0, yml_1.isScalar)(value)) {
noodl_core_1.fp.merge(props, toObject(value));
}
else if ((0, yml_1.isPair)(value)) {
props[String(value.key)] = (0, yml_1.unwrap)(value.value);
}
else if ((0, yml_1.isMap)(value)) {
(0, yml_1.merge)(props, value);
}
else if (!(0, yml_1.isSeq)(value) && !noodl_core_1.is.arr(value)) {
(0, yml_1.merge)(props, value);
}
}
return props;
}
exports.toObject = toObject;
function toPathname(value) {
if (!noodl_core_1.is.str(value))
return '/' + String(value);
if (!value.startsWith('/'))
value = `/${value}`;
return value;
}
exports.toPathname = toPathname;
//# sourceMappingURL=format.js.map