+98
-61
@@ -6,61 +6,2 @@ "use strict"; | ||
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| function getMetadataKey(Class) { | ||
| if (!Class) return null; | ||
| return Class[Symbol.metadata] ?? null; | ||
| } | ||
| if (!Symbol.metadata) { | ||
| Reflect.set(Symbol, "metadata", Symbol.for("Symbol.metadata")); | ||
| } | ||
| class PrivateMetadata { | ||
| constructor() { | ||
| __publicField(this, "registry", /* @__PURE__ */ new WeakMap()); | ||
| } | ||
| getFor(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| return this.get(key); | ||
| } | ||
| get(key) { | ||
| return this.registry.get(key) ?? null; | ||
| } | ||
| getOrInit(key, initializer) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| const value = initializer(); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| has(key) { | ||
| return this.registry.has(key); | ||
| } | ||
| set(key, value) { | ||
| this.registry.set(key, value); | ||
| } | ||
| init(key, value) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| } | ||
| function getIsObject(value) { | ||
| return typeof value === "object" && value !== null; | ||
| } | ||
| function getIsNotNull(value) { | ||
| return value !== null; | ||
| } | ||
| const codableClassRegistry = new PrivateMetadata(); | ||
| const codableClassFieldsRegistry = new PrivateMetadata(); | ||
| const externalClassFieldsRegistry = new PrivateMetadata(); | ||
| function getIsCodableClass(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return false; | ||
| return codableClassRegistry.has(key); | ||
| } | ||
| function getCodableClassType(Class) { | ||
| var _a; | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| const codableType2 = (_a = codableClassRegistry.get(key)) == null ? void 0 : _a.codableType; | ||
| if (!codableType2) return null; | ||
| return codableType2; | ||
| } | ||
| const defaultCodableReader = (item, segments) => { | ||
@@ -202,2 +143,8 @@ const [key] = segments; | ||
| })(EntryIndex || {}); | ||
| function getIsObject(value) { | ||
| return typeof value === "object" && value !== null; | ||
| } | ||
| function getIsNotNull(value) { | ||
| return value !== null; | ||
| } | ||
| function resolveThunk(thunk) { | ||
@@ -625,2 +572,61 @@ let result = thunk; | ||
| } | ||
| function getMetadataKey(Class) { | ||
| if (!Class) return null; | ||
| return Class[Symbol.metadata] ?? null; | ||
| } | ||
| if (!Symbol.metadata) { | ||
| Reflect.set(Symbol, "metadata", Symbol.for("Symbol.metadata")); | ||
| } | ||
| class PrivateMetadata { | ||
| constructor() { | ||
| __publicField(this, "registry", /* @__PURE__ */ new WeakMap()); | ||
| } | ||
| getFor(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| return this.get(key); | ||
| } | ||
| get(key) { | ||
| return this.registry.get(key) ?? null; | ||
| } | ||
| getOrInit(key, initializer) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| const value = initializer(); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| has(key) { | ||
| return this.registry.has(key); | ||
| } | ||
| set(key, value) { | ||
| this.registry.set(key, value); | ||
| } | ||
| init(key, value) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| } | ||
| const codableClassRegistry = new PrivateMetadata(); | ||
| const codableClassFieldsRegistry = new PrivateMetadata(); | ||
| const externalClassFieldsRegistry = new PrivateMetadata(); | ||
| function getIsCodableClass(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return false; | ||
| return codableClassRegistry.has(key); | ||
| } | ||
| function getIsCodableClassInstance(instance) { | ||
| if (!getIsObject(instance)) return false; | ||
| const key = getMetadataKey(instance.constructor); | ||
| if (!key) return false; | ||
| return codableClassRegistry.has(key); | ||
| } | ||
| function getCodableClassType(Class) { | ||
| var _a; | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| const codableType2 = (_a = codableClassRegistry.get(key)) == null ? void 0 : _a.codableType; | ||
| if (!codableType2) return null; | ||
| return codableType2; | ||
| } | ||
| class ExternalReference { | ||
@@ -1115,3 +1121,3 @@ constructor(key, isOptional = false) { | ||
| } | ||
| function copy(value) { | ||
| function clone(value) { | ||
| return coder.clone(value); | ||
@@ -1302,12 +1308,43 @@ } | ||
| } | ||
| function combineDecorators(...decorators) { | ||
| const [first, ...rest] = decorators; | ||
| return function(value, context) { | ||
| let result = first(value, context); | ||
| for (const decorator of rest) { | ||
| result = decorator(result, context); | ||
| } | ||
| return result; | ||
| }; | ||
| } | ||
| function external(key, isOptional = false) { | ||
| return function external2(initialValue, context) { | ||
| var _a; | ||
| const externalFieldsMap = externalClassFieldsRegistry.getOrInit(context.metadata, () => /* @__PURE__ */ new Map()); | ||
| if (typeof context.name !== "string") { | ||
| throw new Error("External decorator can only be used on string properties"); | ||
| } | ||
| if ((_a = codableClassFieldsRegistry.get(context.metadata)) == null ? void 0 : _a.has(context.name)) { | ||
| throw new Error("External decorator cannot be used on codable properties"); | ||
| } | ||
| externalFieldsMap.set(context.name, { key, isOptional }); | ||
| }; | ||
| } | ||
| exports.CodableType = CodableType; | ||
| exports.Coder = Coder; | ||
| exports.DEFAULT_CODABLE_TYPE_PRIORITY = DEFAULT_CODABLE_TYPE_PRIORITY; | ||
| exports.ExternalReference = ExternalReference; | ||
| exports.clone = clone; | ||
| exports.codable = codable; | ||
| exports.codableClass = codableClass; | ||
| exports.codableType = codableType; | ||
| exports.copy = copy; | ||
| exports.combineDecorators = combineDecorators; | ||
| exports.createCoder = createCoder; | ||
| exports.decode = decode; | ||
| exports.encode = encode; | ||
| exports.external = external; | ||
| exports.externalReference = externalReference; | ||
| exports.getCodableProperties = getCodableProperties; | ||
| exports.getExternalProperties = getExternalProperties; | ||
| exports.getIsCodableClass = getIsCodableClass; | ||
| exports.getIsCodableClassInstance = getIsCodableClassInstance; | ||
| exports.getIsCodableType = getIsCodableType; | ||
@@ -1314,0 +1351,0 @@ exports.parse = parse; |
+98
-61
| var __defProp = Object.defineProperty; | ||
| var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
| var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
| function getMetadataKey(Class) { | ||
| if (!Class) return null; | ||
| return Class[Symbol.metadata] ?? null; | ||
| } | ||
| if (!Symbol.metadata) { | ||
| Reflect.set(Symbol, "metadata", Symbol.for("Symbol.metadata")); | ||
| } | ||
| class PrivateMetadata { | ||
| constructor() { | ||
| __publicField(this, "registry", /* @__PURE__ */ new WeakMap()); | ||
| } | ||
| getFor(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| return this.get(key); | ||
| } | ||
| get(key) { | ||
| return this.registry.get(key) ?? null; | ||
| } | ||
| getOrInit(key, initializer) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| const value = initializer(); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| has(key) { | ||
| return this.registry.has(key); | ||
| } | ||
| set(key, value) { | ||
| this.registry.set(key, value); | ||
| } | ||
| init(key, value) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| } | ||
| function getIsObject(value) { | ||
| return typeof value === "object" && value !== null; | ||
| } | ||
| function getIsNotNull(value) { | ||
| return value !== null; | ||
| } | ||
| const codableClassRegistry = new PrivateMetadata(); | ||
| const codableClassFieldsRegistry = new PrivateMetadata(); | ||
| const externalClassFieldsRegistry = new PrivateMetadata(); | ||
| function getIsCodableClass(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return false; | ||
| return codableClassRegistry.has(key); | ||
| } | ||
| function getCodableClassType(Class) { | ||
| var _a; | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| const codableType2 = (_a = codableClassRegistry.get(key)) == null ? void 0 : _a.codableType; | ||
| if (!codableType2) return null; | ||
| return codableType2; | ||
| } | ||
| const defaultCodableReader = (item, segments) => { | ||
@@ -199,2 +140,8 @@ const [key] = segments; | ||
| })(EntryIndex || {}); | ||
| function getIsObject(value) { | ||
| return typeof value === "object" && value !== null; | ||
| } | ||
| function getIsNotNull(value) { | ||
| return value !== null; | ||
| } | ||
| function resolveThunk(thunk) { | ||
@@ -622,2 +569,61 @@ let result = thunk; | ||
| } | ||
| function getMetadataKey(Class) { | ||
| if (!Class) return null; | ||
| return Class[Symbol.metadata] ?? null; | ||
| } | ||
| if (!Symbol.metadata) { | ||
| Reflect.set(Symbol, "metadata", Symbol.for("Symbol.metadata")); | ||
| } | ||
| class PrivateMetadata { | ||
| constructor() { | ||
| __publicField(this, "registry", /* @__PURE__ */ new WeakMap()); | ||
| } | ||
| getFor(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| return this.get(key); | ||
| } | ||
| get(key) { | ||
| return this.registry.get(key) ?? null; | ||
| } | ||
| getOrInit(key, initializer) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| const value = initializer(); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| has(key) { | ||
| return this.registry.has(key); | ||
| } | ||
| set(key, value) { | ||
| this.registry.set(key, value); | ||
| } | ||
| init(key, value) { | ||
| if (this.registry.has(key)) return this.registry.get(key); | ||
| this.registry.set(key, value); | ||
| return value; | ||
| } | ||
| } | ||
| const codableClassRegistry = new PrivateMetadata(); | ||
| const codableClassFieldsRegistry = new PrivateMetadata(); | ||
| const externalClassFieldsRegistry = new PrivateMetadata(); | ||
| function getIsCodableClass(Class) { | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return false; | ||
| return codableClassRegistry.has(key); | ||
| } | ||
| function getIsCodableClassInstance(instance) { | ||
| if (!getIsObject(instance)) return false; | ||
| const key = getMetadataKey(instance.constructor); | ||
| if (!key) return false; | ||
| return codableClassRegistry.has(key); | ||
| } | ||
| function getCodableClassType(Class) { | ||
| var _a; | ||
| const key = getMetadataKey(Class); | ||
| if (!key) return null; | ||
| const codableType2 = (_a = codableClassRegistry.get(key)) == null ? void 0 : _a.codableType; | ||
| if (!codableType2) return null; | ||
| return codableType2; | ||
| } | ||
| class ExternalReference { | ||
@@ -1112,3 +1118,3 @@ constructor(key, isOptional = false) { | ||
| } | ||
| function copy(value) { | ||
| function clone(value) { | ||
| return coder.clone(value); | ||
@@ -1299,13 +1305,44 @@ } | ||
| } | ||
| function combineDecorators(...decorators) { | ||
| const [first, ...rest] = decorators; | ||
| return function(value, context) { | ||
| let result = first(value, context); | ||
| for (const decorator of rest) { | ||
| result = decorator(result, context); | ||
| } | ||
| return result; | ||
| }; | ||
| } | ||
| function external(key, isOptional = false) { | ||
| return function external2(initialValue, context) { | ||
| var _a; | ||
| const externalFieldsMap = externalClassFieldsRegistry.getOrInit(context.metadata, () => /* @__PURE__ */ new Map()); | ||
| if (typeof context.name !== "string") { | ||
| throw new Error("External decorator can only be used on string properties"); | ||
| } | ||
| if ((_a = codableClassFieldsRegistry.get(context.metadata)) == null ? void 0 : _a.has(context.name)) { | ||
| throw new Error("External decorator cannot be used on codable properties"); | ||
| } | ||
| externalFieldsMap.set(context.name, { key, isOptional }); | ||
| }; | ||
| } | ||
| export { | ||
| CodableType, | ||
| Coder, | ||
| DEFAULT_CODABLE_TYPE_PRIORITY, | ||
| ExternalReference, | ||
| clone, | ||
| codable, | ||
| codableClass, | ||
| codableType, | ||
| copy, | ||
| combineDecorators, | ||
| createCoder, | ||
| decode, | ||
| encode, | ||
| external, | ||
| externalReference, | ||
| getCodableProperties, | ||
| getExternalProperties, | ||
| getIsCodableClass, | ||
| getIsCodableClassInstance, | ||
| getIsCodableType, | ||
@@ -1312,0 +1349,0 @@ parse, |
@@ -33,3 +33,3 @@ import { CodableType, CodableTypeOptions } from "./CodableType"; | ||
| export declare function parse<T>(value: string): T; | ||
| export declare function copy<T>(value: T): T; | ||
| export declare function clone<T>(value: T): T; | ||
| export {}; |
@@ -7,3 +7,3 @@ import { AnyClass, ClassDecorator, MakeRequired, MemberwiseClass } from "./types"; | ||
| type CodableClassKeysInput<T extends AnyClass> = Array<keyof InstanceType<T>> | Record<keyof InstanceType<T>, string>; | ||
| interface CodableClassOptions<T extends AnyClass> { | ||
| export interface CodableClassOptions<T extends AnyClass> { | ||
| dependencies?: CodableDependencies; | ||
@@ -10,0 +10,0 @@ encode?: ClassEncoder<T>; |
@@ -1,3 +0,7 @@ | ||
| export * from "./codable"; | ||
| export * from "./codableClass"; | ||
| export * from "./memberwise"; | ||
| export { codable } from "./codable"; | ||
| export { type CodableClassOptions, codableClass } from "./codableClass"; | ||
| export { type Memberwise, type MemberwiseExclude } from "./memberwise"; | ||
| export { combineDecorators } from "./utils"; | ||
| export { getIsCodableClass, getIsCodableClassInstance } from "./registry"; | ||
| export { getCodableProperties, getExternalProperties } from "./properties"; | ||
| export { external } from "./external"; |
| import { JSONArray, JSONObject } from "./types"; | ||
| import { Coder } from "./Coder"; | ||
| type UnknownMode = "unchanged" | "null" | "throw"; | ||
| export type UnknownMode = "unchanged" | "null" | "throw"; | ||
| export interface EncodeOptions { | ||
@@ -53,2 +53,1 @@ /** | ||
| } | ||
| export {}; |
@@ -1,6 +0,6 @@ | ||
| export { getIsCodableClass } from "./decorators/registry"; | ||
| export { Coder, encode, decode, parse, stringify, copy, createCoder } from "./Coder"; | ||
| export { codableClass } from "./decorators/codableClass"; | ||
| export { codable } from "./decorators/codable"; | ||
| export { codableType, getIsCodableType, CodableType } from "./CodableType"; | ||
| export type { Memberwise, MemberwiseExclude } from "./decorators"; | ||
| export { Coder, encode, decode, parse, stringify, clone, createCoder } from "./Coder"; | ||
| export { codableType, getIsCodableType, CodableType, type CodableReader, type CodableTypeOptions, DEFAULT_CODABLE_TYPE_PRIORITY, } from "./CodableType"; | ||
| export { externalReference, ExternalReference } from "./ExternalReference"; | ||
| export type { UnknownMode, EncodeOptions } from "./EncodeContext"; | ||
| export type { DecodeOptions } from "./DecodeContext"; | ||
| export * from "./decorators"; |
+1
-1
| { | ||
| "name": "codables", | ||
| "version": "0.1.3", | ||
| "version": "0.1.4", | ||
| "description": "High-performance, type-safe JSON serialization library that extends JSON to support complex JavaScript types including Date, BigInt, Map, Set, RegExp, Symbol, typed arrays, circular references, and custom classes.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
+8
-8
@@ -52,3 +52,3 @@ # Codables | ||
| // bigint: { $$BigInt: "1234567890123456789" }, | ||
| // regex: { $$RegExp: ["hello", "gi"] }, | ||
| // regex: { $$RegExp: "/hello/gi" }, | ||
| // url: { $$URL: "https://example.com/" } | ||
@@ -123,3 +123,3 @@ // } | ||
| | `Map` | `{ $$Map: [["key", "value"]] }` | | ||
| | `RegExp` | `{ $$RegExp: ["hello", "gi"] }` | | ||
| | `RegExp` | `{ $$RegExp: "/hello/gi" }` | | ||
| | `Symbol` | `{ $$Symbol: "test" }` | | ||
@@ -151,3 +151,3 @@ | `URL` | `{ $$URL: "https://example.com/" }` | | ||
| ```typescript | ||
| import { encode, decode, stringify, parse, copy } from "codables"; | ||
| import { encode, decode, stringify, parse, clone } from "codables"; | ||
@@ -162,9 +162,9 @@ // Basic encoding/decoding | ||
| // Deep copy maintaining all types and references equality | ||
| // Deep clone maintaining all types and references equality | ||
| const foo = { foo: "foo" }; | ||
| const original = [foo, foo]; | ||
| const copied = copy(original); | ||
| // copied === original; // false | ||
| // copied[0] === original[0]; // false -> nested copy | ||
| // copied[0] === copied[1]; // true -> reference equality is preserved | ||
| const cloned = clone(original); | ||
| // cloned === original; // false | ||
| // cloned[0] === original[0]; // false -> nested clone | ||
| // cloned[0] === cloned[1]; // true -> reference equality is preserved | ||
| ``` | ||
@@ -171,0 +171,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
316957
2.28%3258
2.42%