UNPKG

@imjxsx/localdb

Version:

A lightweight MongoDB-style BSON database for Node.js

3 lines (2 loc) 1.87 kB
import{isObject as _,isArray as f,isString as e,isNumber as a,isBigInt as y,isRegExp as t,isBuffer as l,isDate as g,isMap as m,isSet as I,isURL as w,isBoolean as d,isTypedArray as R}from"./index.js";function u(n){if(n==null)return n;if(f(n))return n.map(r=>u(r));if(_(n))if("__type"in n&&n.__type){if(n.__type==="BigInt"&&"int"in n&&e(n.int))return BigInt(n.int);if(n.__type==="RegExp"&&"source"in n&&e(n.source))return new RegExp(n.source,"flags"in n&&e(n.flags)?n.flags:void 0);if(n.__type==="Buffer"&&"base64"in n&&e(n.base64))return Buffer.from(n.base64,"base64");if(n.__type==="Date"&&"iso"in n&&e(n.iso))return new Date(n.iso);if(n.__type==="Map"&&"entries"in n&&f(n.entries))return new Map(n.entries.map(([r,i])=>[r,u(i)]));if(n.__type==="Set"&&"values"in n&&f(n.values))return new Set(n.values.map(r=>u(r)));if(n.__type==="URL"&&"href"in n&&e(n.href))return new URL(n.href);if(n.__type==="Infinity")return 1/0;if(n.__type==="-Infinity")return-1/0;if(n.__type==="NaN")return NaN}else{const r={};for(const[i,p]of Object.entries(n))r[i]=u(p);return r}return n}function s(n){if(n==null||e(n)||d(n))return n;if(a(n))return isNaN(n)?{__type:"NaN"}:n===1/0?{__type:"Infinity"}:n===-1/0?{__type:"-Infinity"}:n;if(y(n))return{__type:"BigInt",int:n.toString()};if(t(n))return{__type:"RegExp",source:n.source,flags:n.flags};if(l(n))return{__type:"Buffer",base64:n.toString("base64")};if(g(n))return{__type:"Date",iso:n.toISOString()};if(m(n))return{__type:"Map",entries:Array.from(n.entries()).map(([r,i])=>[r,s(i)])};if(I(n))return{__type:"Set",values:Array.from(n.values()).map(r=>s(r))};if(w(n))return{__type:"URL",href:n.href};if(f(n))return n.map(r=>s(r));if(_(n)&&!R(n)){const r={};for(const[i,p]of Object.entries(n))r[i]=s(p);return r}return{__type:"Unsupported",name:n.constructor?.name||typeof n}}export{s as replacer,u as reviver}; //# sourceMappingURL=json.js.map