staticql
Version:
Type-safe query engine for static content including Markdown, YAML, JSON, and more.
55 lines (54 loc) • 1.13 kB
JavaScript
/**
* Return as array if not array.
*
* @param data
* @returns array
*/
export function asArray(data) {
if (Array.isArray(data))
return data;
return [data];
}
/**
* Map, Set convert to object or array recursive
*
* @param value
* @returns
*/
export function mapSetToObject(value) {
if (value instanceof Map) {
const obj = {};
for (const [k, v] of value.entries()) {
obj[k] = mapSetToObject(v);
}
return obj;
}
else if (value instanceof Set) {
return Array.from(value).map(mapSetToObject);
}
else if (Array.isArray(value)) {
return value.map(mapSetToObject);
}
else if (typeof value === "object" && value !== null) {
const obj = {};
for (const key of Object.keys(value)) {
obj[key] = mapSetToObject(value[key]);
}
return obj;
}
else {
return value;
}
}
/**
* Parse _prefixes.jsonl content.
*
* @param raw
* @returns
*/
export function parsePrefixDict(raw) {
return raw
.split("\n")
.map((x) => x.trim())
.filter(Boolean);
}