| import { Field } from "./field"; | ||
| import { Structure } from "./structure"; | ||
| export declare enum DictMemberType { | ||
| FIELD = 0, | ||
| DICT = 1 | ||
| } | ||
| export type DictMember = Field | Dictionary; | ||
| export declare class Dictionary { | ||
| private members; | ||
| static create(data: Record<string, any>): Dictionary; | ||
| has(key: string): boolean; | ||
| set(key: string, value: Field | Dictionary): void; | ||
| setRaw(key: string, value: any): void; | ||
| get(key: string): DictMember | undefined; | ||
| getField(key: string): Field | null; | ||
| getDict(key: string): Dictionary | null; | ||
| getStructure(): Structure; | ||
| static fromStructure(struct: Structure): Dictionary; | ||
| } | ||
| //# sourceMappingURL=dictionary.d.ts.map |
| {"version":3,"file":"dictionary.d.ts","sourceRoot":"","sources":["../src/dictionary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,oBAAY,cAAc;IACxB,KAAK,IAAI;IACT,IAAI,IAAI;CACT;AAED,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,UAAU,CAAC;AAE5C,qBAAa,UAAU;IACrB,OAAO,CAAC,OAAO,CAAsC;IAErD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,UAAU;IAUpD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,UAAU;IAK1C,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG;IAwB9B,GAAG,CAAC,GAAG,EAAE,MAAM;IAIf,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI;IAMnC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;IAMvC,YAAY,IAAI,SAAS;IAyBzB,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,SAAS,GAAG,UAAU;CAmBpD"} |
| import { Field } from "./field"; | ||
| import { Structure } from "./structure"; | ||
| export var DictMemberType; | ||
| (function (DictMemberType) { | ||
| DictMemberType[DictMemberType["FIELD"] = 0] = "FIELD"; | ||
| DictMemberType[DictMemberType["DICT"] = 1] = "DICT"; | ||
| })(DictMemberType || (DictMemberType = {})); | ||
| export class Dictionary { | ||
| constructor() { | ||
| this.members = new Map(); | ||
| } | ||
| static create(data) { | ||
| const dict = new Dictionary(); | ||
| for (const [key, value] of Object.entries(data)) { | ||
| dict.setRaw(key, value); | ||
| } | ||
| return dict; | ||
| } | ||
| has(key) { | ||
| return this.members.has(key); | ||
| } | ||
| set(key, value) { | ||
| if (this.has(key)) | ||
| return; | ||
| this.members.set(key, value); | ||
| } | ||
| setRaw(key, value) { | ||
| if (typeof value === "undefined" || value === null) | ||
| return; | ||
| if (Array.isArray(value)) { | ||
| return this.set(key, Field.auto(value)); | ||
| } | ||
| switch (typeof value) { | ||
| case "number": | ||
| return this.set(key, Field.int32(value)); | ||
| case "string": | ||
| return this.set(key, Field.string(value)); | ||
| case "boolean": | ||
| return this.set(key, Field.boolean(value)); | ||
| case "object": | ||
| return this.set(key, Dictionary.create(value)); | ||
| default: { | ||
| throw new Error(`Dictionary does not support values of type: ${typeof value}`); | ||
| } | ||
| } | ||
| } | ||
| get(key) { | ||
| return this.members.get(key); | ||
| } | ||
| getField(key) { | ||
| const member = this.get(key); | ||
| if (!(member instanceof Field)) | ||
| return null; | ||
| return member; | ||
| } | ||
| getDict(key) { | ||
| const member = this.get(key); | ||
| if (!(member instanceof Dictionary)) | ||
| return null; | ||
| return member; | ||
| } | ||
| getStructure() { | ||
| const struct = new Structure(); | ||
| const entries = Array.from(this.members.entries()); | ||
| struct.writeField(Field.int32(entries.length)); | ||
| for (const [key, member] of entries) { | ||
| const memberType = member instanceof Field ? DictMemberType.FIELD : DictMemberType.DICT; | ||
| struct.writeField(Field.int32(memberType)); | ||
| struct.writeField(Field.string(key)); | ||
| if (member instanceof Field) { | ||
| struct.writeField(member); | ||
| } | ||
| else { | ||
| struct.write(member.getStructure().data.buffer); | ||
| } | ||
| } | ||
| struct.rewind(); | ||
| return struct; | ||
| } | ||
| static fromStructure(struct) { | ||
| const dict = new Dictionary(); | ||
| const count = struct.readField().toInt32(); | ||
| for (let i = 0; i < count; i++) { | ||
| const memberType = struct.readField().toInt32(); | ||
| const key = struct.readField().toString(); | ||
| if (memberType === DictMemberType.FIELD) { | ||
| const value = struct.readField(); | ||
| dict.set(key, value); | ||
| } | ||
| else { | ||
| const value = Dictionary.fromStructure(struct); | ||
| dict.set(key, value); | ||
| } | ||
| } | ||
| return dict; | ||
| } | ||
| } | ||
| //# sourceMappingURL=dictionary.js.map |
| {"version":3,"file":"dictionary.js","sourceRoot":"","sources":["../src/dictionary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,CAAN,IAAY,cAGX;AAHD,WAAY,cAAc;IACxB,qDAAS,CAAA;IACT,mDAAQ,CAAA;AACV,CAAC,EAHW,cAAc,KAAd,cAAc,QAGzB;AAID,MAAM,OAAO,UAAU;IAAvB;QACU,YAAO,GAA4B,IAAI,GAAG,EAAE,CAAC;IAyGvD,CAAC;IAvGC,MAAM,CAAC,MAAM,CAAC,IAAyB;QACrC,MAAM,IAAI,GAAG,IAAI,UAAU,EAAE,CAAC;QAE9B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAChD,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC1B,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IA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,CAAC;QAEhB,OAAO,MAAM,CAAC;IAChB,CAAC;IAGD,MAAM,CAAC,aAAa,CAAC,MAAiB;QACpC,MAAM,IAAI,GAAG,IAAI,UAAU,EAAE,CAAC;QAC9B,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,CAAC;QAE3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/B,MAAM,UAAU,GAAmB,MAAM,CAAC,SAAS,EAAE,CAAC,OAAO,EAAoB,CAAC;YAClF,MAAM,GAAG,GAAW,MAAM,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,CAAC;YAElD,IAAI,UAAU,KAAK,cAAc,CAAC,KAAK,EAAE,CAAC;gBACxC,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;gBACjC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACvB,CAAC;iBAAM,CAAC;gBACN,MAAM,KAAK,GAAG,UAAU,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;gBAC/C,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"} |
| export * from './dictionary'; | ||
| export * from './structure'; | ||
| export * from './field'; | ||
| //# sourceMappingURL=index.d.ts.map |
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC"} |
| export * from './dictionary'; | ||
| export * from './structure'; | ||
| export * from './field'; | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC"} |
| import { Field } from "./field"; | ||
| import { Structure } from "./structure"; | ||
| export enum DictMemberType { | ||
| FIELD = 0, | ||
| DICT = 1, | ||
| } | ||
| export type DictMember = Field | Dictionary; | ||
| export class Dictionary { | ||
| private members: Map<string, DictMember> = new Map(); | ||
| static create(data: Record<string, any>): Dictionary { | ||
| const dict = new Dictionary(); | ||
| for (const [key, value] of Object.entries(data)) { | ||
| dict.setRaw(key, value); | ||
| } | ||
| return dict; | ||
| } | ||
| has(key: string): boolean { | ||
| return this.members.has(key); | ||
| } | ||
| set(key: string, value: Field | Dictionary) { | ||
| if (this.has(key)) return; | ||
| this.members.set(key, value); | ||
| } | ||
| setRaw(key: string, value: any) { | ||
| if (typeof value === "undefined" || value === null) return; | ||
| if (Array.isArray(value)) { | ||
| return this.set(key, Field.auto(value)); | ||
| } | ||
| switch (typeof value) { | ||
| case "number": | ||
| return this.set(key, Field.int32(value)); | ||
| case "string": | ||
| return this.set(key, Field.string(value)); | ||
| case "boolean": | ||
| return this.set(key, Field.boolean(value)); | ||
| case "object": | ||
| return this.set(key, Dictionary.create(value)); | ||
| default: { | ||
| throw new Error( | ||
| `Dictionary does not support values of type: ${typeof value}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| get(key: string) { | ||
| return this.members.get(key); | ||
| } | ||
| getField(key: string): Field | null { | ||
| const member = this.get(key); | ||
| if (!(member instanceof Field)) return null; | ||
| return member; | ||
| } | ||
| getDict(key: string): Dictionary | null { | ||
| const member = this.get(key); | ||
| if (!(member instanceof Dictionary)) return null; | ||
| return member; | ||
| } | ||
| getStructure(): Structure { | ||
| const struct = new Structure(); | ||
| const entries = Array.from(this.members.entries()); | ||
| struct.writeField(Field.int32(entries.length)); | ||
| for (const [key, member] of entries) { | ||
| const memberType: DictMemberType = | ||
| member instanceof Field ? DictMemberType.FIELD : DictMemberType.DICT; | ||
| struct.writeField(Field.int32(memberType)); | ||
| struct.writeField(Field.string(key)); | ||
| if (member instanceof Field) { | ||
| struct.writeField(member); | ||
| } else { | ||
| struct.write(member.getStructure().data.buffer); | ||
| } | ||
| } | ||
| struct.rewind(); | ||
| return struct; | ||
| } | ||
| static fromStructure(struct: Structure): Dictionary { | ||
| const dict = new Dictionary(); | ||
| const count = struct.readField().toInt32(); | ||
| for (let i = 0; i < count; i++) { | ||
| const memberType: DictMemberType = struct.readField().toInt32() as DictMemberType; | ||
| const key: string = struct.readField().toString(); | ||
| if (memberType === DictMemberType.FIELD) { | ||
| const value = struct.readField(); | ||
| dict.set(key, value); | ||
| } else { | ||
| const value = Dictionary.fromStructure(struct); | ||
| dict.set(key, value); | ||
| } | ||
| } | ||
| return dict; | ||
| } | ||
| } |
| export * from './dictionary'; | ||
| export * from './structure'; | ||
| export * from './field'; |
| import { it } from "node:test"; | ||
| import { Dictionary } from "./Dictionary"; | ||
| import { Dictionary } from "./dictionary"; | ||
| import assert from "node:assert"; | ||
@@ -4,0 +4,0 @@ it("Can store basic values", () => { |
+1
-1
| { | ||
| "name": "binkv", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
| import { it } from "node:test"; | ||
| import { Dictionary } from "./Dictionary"; | ||
| import { Dictionary } from "./dictionary"; | ||
| import assert from "node:assert"; | ||
@@ -4,0 +4,0 @@ |
| import { Field } from "./field"; | ||
| import { Structure } from "./structure"; | ||
| export enum DictMemberType { | ||
| FIELD = 0, | ||
| DICT = 1, | ||
| } | ||
| export type DictMember = Field | Dictionary; | ||
| export class Dictionary { | ||
| private members: Map<string, DictMember> = new Map(); | ||
| static create(data: Record<string, any>): Dictionary { | ||
| const dict = new Dictionary(); | ||
| for (const [key, value] of Object.entries(data)) { | ||
| dict.setRaw(key, value); | ||
| } | ||
| return dict; | ||
| } | ||
| has(key: string): boolean { | ||
| return this.members.has(key); | ||
| } | ||
| set(key: string, value: Field | Dictionary) { | ||
| if (this.has(key)) return; | ||
| this.members.set(key, value); | ||
| } | ||
| setRaw(key: string, value: any) { | ||
| if (typeof value === "undefined" || value === null) return; | ||
| if (Array.isArray(value)) { | ||
| return this.set(key, Field.auto(value)); | ||
| } | ||
| switch (typeof value) { | ||
| case "number": | ||
| return this.set(key, Field.int32(value)); | ||
| case "string": | ||
| return this.set(key, Field.string(value)); | ||
| case "boolean": | ||
| return this.set(key, Field.boolean(value)); | ||
| case "object": | ||
| return this.set(key, Dictionary.create(value)); | ||
| default: { | ||
| throw new Error( | ||
| `Dictionary does not support values of type: ${typeof value}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| get(key: string) { | ||
| return this.members.get(key); | ||
| } | ||
| getField(key: string): Field | null { | ||
| const member = this.get(key); | ||
| if (!(member instanceof Field)) return null; | ||
| return member; | ||
| } | ||
| getDict(key: string): Dictionary | null { | ||
| const member = this.get(key); | ||
| if (!(member instanceof Dictionary)) return null; | ||
| return member; | ||
| } | ||
| getStructure(): Structure { | ||
| const struct = new Structure(); | ||
| const entries = Array.from(this.members.entries()); | ||
| struct.writeField(Field.int32(entries.length)); | ||
| for (const [key, member] of entries) { | ||
| const memberType: DictMemberType = | ||
| member instanceof Field ? DictMemberType.FIELD : DictMemberType.DICT; | ||
| struct.writeField(Field.int32(memberType)); | ||
| struct.writeField(Field.string(key)); | ||
| if (member instanceof Field) { | ||
| struct.writeField(member); | ||
| } else { | ||
| struct.write(member.getStructure().data.buffer); | ||
| } | ||
| } | ||
| struct.rewind(); | ||
| return struct; | ||
| } | ||
| static fromStructure(struct: Structure): Dictionary { | ||
| const dict = new Dictionary(); | ||
| const count = struct.readField().toInt32(); | ||
| for (let i = 0; i < count; i++) { | ||
| const memberType: DictMemberType = struct.readField().toInt32() as DictMemberType; | ||
| const key: string = struct.readField().toString(); | ||
| if (memberType === DictMemberType.FIELD) { | ||
| const value = struct.readField(); | ||
| dict.set(key, value); | ||
| } else { | ||
| const value = Dictionary.fromStructure(struct); | ||
| dict.set(key, value); | ||
| } | ||
| } | ||
| return dict; | ||
| } | ||
| } |
63665
16.02%42
27.27%1096
12.87%