@signaldb/core
Version:
SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in
23 lines (22 loc) • 906 B
JavaScript
//#region src/utils/serializeValue.ts
/**
* Serializes a value into a string representation.
* Handles various types, including strings, numbers, booleans, dates, and objects.
* Falls back to JSON stringification for unsupported types.
* @param value - The value to serialize.
* - Strings are returned as-is.
* - Numbers and booleans are converted to their string representation.
* - Dates are converted to ISO string format.
* - Other values are stringified using `JSON.stringify`.
* @returns A string representation of the value.
*/
function serializeValue(value) {
if (value == null) return null;
if (typeof value === "string") return value;
if (typeof value === "number") return value.toString();
if (typeof value === "boolean") return value.toString();
if (value instanceof Date) return value.toISOString();
return JSON.stringify(value);
}
//#endregion
exports.default = serializeValue;