silverbullet/plug-api/lib/json.ts

178 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-08-07 19:27:25 +08:00
/**
* Performs a deep comparison of two objects, returning true if they are equal
* @param a first object
* @param b second object
* @returns
*/
2024-02-29 22:25:28 +08:00
export function deepEqual(a: any, b: any): boolean {
if (a === b) {
return true;
}
if (typeof a !== typeof b) {
return false;
}
2024-08-03 17:54:32 +08:00
if (a === null || b === null) {
return false;
}
2024-08-03 17:58:50 +08:00
if (a === undefined || b === undefined) {
return false;
}
2024-02-29 22:25:28 +08:00
if (typeof a === "object") {
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) {
return false;
}
}
return true;
} else {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
for (const key of aKeys) {
if (!deepEqual(a[key], b[key])) {
return false;
}
}
return true;
}
}
return false;
}
2024-08-07 19:27:25 +08:00
/**
* Converts a Date object to a date string in the format YYYY-MM-DD if it just contains a date (and no significant time), or a full ISO string otherwise
* @param d the date to convert
*/
2024-07-06 21:07:40 +08:00
export function cleanStringDate(d: Date): string {
// If no significant time, return a date string only
if (
d.getUTCHours() === 0 && d.getUTCMinutes() === 0 && d.getUTCSeconds() === 0
) {
return d.getFullYear() + "-" +
String(d.getMonth() + 1).padStart(2, "0") + "-" +
String(d.getDate()).padStart(2, "0");
2024-07-06 21:07:40 +08:00
} else {
2024-08-18 19:08:52 +08:00
return d.toISOString();
2024-07-06 21:07:40 +08:00
}
}
2024-08-07 19:27:25 +08:00
/**
* Processes a JSON (typically coming from parse YAML frontmatter) in two ways:
* 1. Expands property names in an object containing a .-separated path
* 2. Converts dates to strings in sensible ways
* @param a
* @returns
*/
2024-07-06 21:07:40 +08:00
export function cleanupJSON(a: any): any {
2024-02-29 22:25:28 +08:00
if (!a) {
return a;
}
if (typeof a !== "object") {
return a;
}
if (Array.isArray(a)) {
2024-07-06 21:07:40 +08:00
return a.map(cleanupJSON);
}
// If a is a date, convert to a string
if (a instanceof Date) {
return cleanStringDate(a);
2024-02-29 22:25:28 +08:00
}
const expanded: any = {};
for (const key of Object.keys(a)) {
const parts = key.split(".");
let target = expanded;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!target[part]) {
target[part] = {};
}
target = target[part];
}
2024-07-06 21:07:40 +08:00
target[parts[parts.length - 1]] = cleanupJSON(a[key]);
2024-02-29 22:25:28 +08:00
}
return expanded;
}
/**
* Performs a deep merge of two objects, with b taking precedence over a
* @param a
* @param b
* @returns
*/
export function deepObjectMerge(a: any, b: any, reverseArrays = false): any {
2024-02-29 22:25:28 +08:00
if (typeof a !== typeof b) {
return b;
}
2024-08-03 20:09:12 +08:00
if (a === undefined || a === null) {
return b;
}
if (b === undefined || b === null) {
return a;
}
2024-02-29 22:25:28 +08:00
if (typeof a === "object") {
if (Array.isArray(a) && Array.isArray(b)) {
if (reverseArrays) {
return [...b, ...a];
} else {
return [...a, ...b];
}
2024-02-29 22:25:28 +08:00
} else {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
const merged = { ...a };
for (const key of bKeys) {
if (aKeys.includes(key)) {
merged[key] = deepObjectMerge(a[key], b[key], reverseArrays);
2024-02-29 22:25:28 +08:00
} else {
merged[key] = b[key];
}
}
return merged;
}
}
return b;
}
2024-08-22 02:13:40 +08:00
export function deepClone<T>(obj: T, ignoreKeys: string[] = []): T {
// Handle null, undefined, or primitive types (string, number, boolean, symbol, bigint)
if (obj === null || typeof obj !== "object") {
return obj;
}
// Handle Date
if (obj instanceof Date) {
return new Date(obj.getTime()) as any;
}
// Handle Array
if (Array.isArray(obj)) {
const arrClone: any[] = [];
for (let i = 0; i < obj.length; i++) {
arrClone[i] = deepClone(obj[i], ignoreKeys);
}
return arrClone as any;
}
// Handle Object
if (obj instanceof Object) {
const objClone: { [key: string]: any } = {};
for (const key in obj) {
if (ignoreKeys.includes(key)) {
objClone[key] = obj[key];
} else if (Object.prototype.hasOwnProperty.call(obj, key)) {
objClone[key] = deepClone(obj[key], ignoreKeys);
}
}
return objClone as T;
}
throw new Error("Unsupported data type.");
}