datasquirel/package-shared/utils/deserialize-query.ts

30 lines
703 B
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import EJSON from "./ejson";
2025-01-01 08:01:39 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Convert Serialized Query back to object
2025-01-01 08:01:39 +00:00
*/
2025-01-10 19:10:28 +00:00
export default function deserializeQuery(
query: string | { [s: string]: any }
): {
[s: string]: any;
} {
2025-01-01 08:01:39 +00:00
/** @type {Object<string,any>} */
2025-01-10 19:10:28 +00:00
let queryObject: { [s: string]: any } =
2025-01-01 08:01:39 +00:00
typeof query == "object" ? query : Object(EJSON.parse(query));
const keys = Object.keys(queryObject);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = queryObject[key];
if (typeof value == "string") {
if (value.match(/^\{|^\[/)) {
queryObject[key] = EJSON.parse(value);
}
}
}
return queryObject;
}