@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
206 lines • 6.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultConfigProvider = void 0;
exports.newFromParams = newFromParams;
exports.newDefaultProvider = newDefaultProvider;
/**
* DefaultConfigProvider is a Provider backed by a map where all keys are lower case
* All methods are thread safe (in single-threaded JS environment)
*/
class DefaultConfigProvider {
constructor(params = {}) {
this.root = this.prepareParams(params);
this.keyCache = new Map();
}
prepareParams(params) {
const prepared = {};
for (const [key, value] of Object.entries(params)) {
prepared[key.toLowerCase()] = value;
}
return prepared;
}
getNestedKeyAndMap(key, create) {
let parts;
if (this.keyCache.has(key)) {
parts = this.keyCache.get(key);
}
else {
parts = key.split('.');
this.keyCache.set(key, parts);
}
let current = this.root;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!(part in current)) {
if (create) {
current[part] = {};
}
else {
return ['', null];
}
}
const next = current[part];
if (typeof next !== 'object' || next === null) {
// E.g. a string, not a map that we can store values in
return ['', null];
}
current = next;
}
return [parts[parts.length - 1], current];
}
getString(key) {
const value = this.get(key);
return String(value || '');
}
getInt(key) {
const value = this.get(key);
const num = Number(value);
return isNaN(num) ? 0 : Math.floor(num);
}
getBool(key) {
const value = this.get(key);
if (typeof value === 'boolean')
return value;
if (typeof value === 'string') {
return value.toLowerCase() === 'true';
}
return Boolean(value);
}
getParams(key) {
const value = this.get(key);
if (value && typeof value === 'object') {
return value;
}
return {};
}
getStringMap(key) {
const value = this.get(key);
if (value && typeof value === 'object') {
return value;
}
return {};
}
getStringMapString(key) {
const value = this.get(key);
if (value && typeof value === 'object') {
const result = {};
for (const [k, v] of Object.entries(value)) {
result[k] = String(v);
}
return result;
}
return {};
}
getStringSlice(key) {
const value = this.get(key);
if (Array.isArray(value)) {
return value.map(v => String(v));
}
return [];
}
get(key) {
if (key === '') {
return this.root;
}
const [finalKey, map] = this.getNestedKeyAndMap(key.toLowerCase(), false);
if (map === null) {
return undefined;
}
return map[finalKey];
}
set(key, value) {
const lowerKey = key.toLowerCase();
if (lowerKey === '') {
if (value && typeof value === 'object') {
// Set the values directly in Root
Object.assign(this.root, this.prepareParams(value));
}
else {
this.root[lowerKey] = value;
}
return;
}
const [finalKey, map] = this.getNestedKeyAndMap(lowerKey, true);
if (map === null) {
return;
}
if (finalKey in map && typeof map[finalKey] === 'object' &&
typeof value === 'object' && value !== null) {
// Merge objects
Object.assign(map[finalKey], value);
}
else {
map[finalKey] = value;
}
}
keys() {
return Object.keys(this.root);
}
merge(key, value) {
const lowerKey = key.toLowerCase();
if (lowerKey === '') {
if (value && typeof value === 'object') {
// Merge into root
Object.assign(this.root, this.prepareParams(value));
}
return;
}
const [finalKey, map] = this.getNestedKeyAndMap(lowerKey, true);
if (map === null) {
return;
}
if (finalKey in map && typeof map[finalKey] === 'object' &&
typeof value === 'object' && value !== null) {
Object.assign(map[finalKey], value);
}
else {
map[finalKey] = value;
}
}
setDefaults(params) {
const prepared = this.prepareParams(params);
for (const [key, value] of Object.entries(prepared)) {
if (!(key in this.root)) {
this.root[key] = value;
}
}
}
setDefaultMergeStrategy() {
// In TypeScript, we don't need complex merge strategies like in Go
// Keep this method for interface compatibility
}
walkParams(walkFn) {
const walk = (obj) => {
if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
if (walkFn(obj)) {
return true;
}
for (const value of Object.values(obj)) {
if (walk(value)) {
return true;
}
}
}
return false;
};
walk(this.root);
}
isSet(key) {
const [finalKey, map] = this.getNestedKeyAndMap(key.toLowerCase(), false);
return map !== null && finalKey in map;
}
}
exports.DefaultConfigProvider = DefaultConfigProvider;
/**
* Create a Provider backed by params
*/
function newFromParams(params) {
return new DefaultConfigProvider(params);
}
/**
* Create a Provider backed by an empty params object
*/
function newDefaultProvider() {
return new DefaultConfigProvider();
}
//# sourceMappingURL=provider.js.map