structurae
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -6,3 +6,3 @@ { | ||
"name": "structurae", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"type": "module", | ||
@@ -36,5 +36,2 @@ "description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
}, | ||
"engines": { | ||
"node": ">=14.0.0" | ||
}, | ||
"exports": { | ||
@@ -44,5 +41,8 @@ ".": { | ||
"types": "./types/index.d.ts" | ||
}, | ||
"./*": { | ||
"types": "./types/*.d.ts", | ||
"import": "./esm/*.js" | ||
} | ||
}, | ||
"devDependencies": {} | ||
} | ||
} |
@@ -442,2 +442,63 @@ # Structurae | ||
#### Extending View Types | ||
The view protocol is designed with extensibility in mind. While built-in view | ||
types are ample for most cases, creating a special type can reduce boilerplate | ||
in certain situations. You can check out a full example of creating and using a | ||
custom view type for BitArray in | ||
[examples/bit-array-view](https://github.com/zandaqo/structurae/tree/master/examples/bit-array-view). | ||
To create a new view type, first create a class extending DataView and | ||
implementing one of the view type interfaces, for example `PrimitiveView`: | ||
```ts | ||
export class BitArrayView extends DataView implements PrimitiveView<BitArray> { | ||
... | ||
} | ||
``` | ||
To let TypeScript know about our new type, we use | ||
[module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) | ||
to add our new type name to `ViewSchemaTypeMap` interface: | ||
```ts | ||
declare module "structurae" { | ||
interface ViewSchemaTypeMap { | ||
bitarray: "string"; | ||
} | ||
} | ||
``` | ||
This way, it will be a binary subtype (or `btype`) of JSONSchema type `string`. | ||
And finally, we add the new class to the list of views used by our protocol | ||
instance: | ||
```ts | ||
const protocol = new View(); | ||
protocol.Views.set("bitarray", BitArrayView); | ||
``` | ||
Now we can use the new type in our schemas, for example: | ||
```ts | ||
class UserSettings { | ||
id = 0; | ||
settings = new BitArray(3); | ||
} | ||
const UserSettingsView = protocol.create<UserSettings>({ | ||
$id: "UserSettings", | ||
type: "object", | ||
properties: { | ||
id: { type: "integer" }, | ||
settings: { | ||
type: "string", | ||
btype: "bitarray", | ||
maxLength: 12, | ||
}, | ||
}, | ||
}, UserSettings); | ||
``` | ||
### Bit Structures | ||
@@ -444,0 +505,0 @@ |
@@ -24,3 +24,3 @@ import type { BitFieldConstructor } from "./bit-field-types.js"; | ||
export declare function BitFieldMixin<T extends Record<K, number>, K extends keyof T>(schema: T): BitFieldConstructor<K>; | ||
declare const _BitField: BitFieldConstructor<0 | 2 | 1 | 22 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30, number>; | ||
declare const _BitField: BitFieldConstructor<0 | 1 | 2 | 4 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30, number>; | ||
export { _BitField as BitField }; |
@@ -31,3 +31,3 @@ export { AdjacencyListMixin } from "./adjacency-list.js"; | ||
export { VectorView } from "./vector-view.js"; | ||
export type { ViewConstructor, ViewInstance, ViewSchema, } from "./view-types.js"; | ||
export type { ViewConstructor, ViewInstance, ViewSchema, ViewSchemaTypeMap, } from "./view-types.js"; | ||
export { View } from "./view.js"; |
@@ -6,4 +6,4 @@ import type { IndexedCollection } from "./utility-types.js"; | ||
static masks: Int8Array; | ||
static decoder: TextDecoder; | ||
static encoder: TextEncoder; | ||
static decoder: any; | ||
static encoder: any; | ||
/** | ||
@@ -10,0 +10,0 @@ * The amount of UTF characters in the StringView. |
@@ -186,4 +186,22 @@ import { Constructor } from "./utility-types.js"; | ||
}; | ||
export declare type ViewSchemaPrimitiveType = "string" | "number" | "integer" | "boolean"; | ||
export declare type ViewSchemaNumberType = "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "float32" | "float64" | "bigint64" | "biguint64"; | ||
export declare type ViewSchemaTypeField<T> = [T] extends [number | bigint | undefined] ? "number" | "integer" : [T] extends [string | ArrayBufferLike | undefined] ? "string" : [T] extends [boolean | undefined] ? "boolean" : T extends Array<unknown> ? "array" : T extends object ? "object" : never; | ||
export interface ViewSchemaTypeMap { | ||
int8: "number"; | ||
uint8: "number"; | ||
int16: "number"; | ||
uint16: "number"; | ||
int32: "number"; | ||
uint32: "number"; | ||
float32: "number"; | ||
float64: "number"; | ||
bigint64: "number"; | ||
biguint64: "number"; | ||
dict: "object"; | ||
map: "object"; | ||
vector: "array"; | ||
binary: "string"; | ||
} | ||
declare type ReverseTypeMap = { | ||
[P in keyof ViewSchemaTypeMap as ViewSchemaTypeMap[P]]: P; | ||
}; | ||
export interface ViewSchema<T> { | ||
@@ -205,5 +223,6 @@ $id?: string; | ||
additionalProperties?: ViewSchema<T[keyof T]>; | ||
type: [T] extends [number | bigint | undefined] ? "number" | "integer" : [T] extends [string | ArrayBufferLike | undefined] ? "string" : [T] extends [boolean | undefined] ? "boolean" : T extends Array<unknown> ? "array" : T extends object ? "object" : never; | ||
btype?: T extends number ? ViewSchemaNumberType : T extends ArrayBufferLike ? "binary" : T extends Array<unknown> ? "vector" : T extends object ? "map" | "dict" : never; | ||
type: ViewSchemaTypeField<T>; | ||
btype?: ViewSchemaTypeField<T> extends "number" | "integer" ? ReverseTypeMap["number"] : ViewSchemaTypeField<T> extends keyof ReverseTypeMap ? ReverseTypeMap[ViewSchemaTypeField<T>] : never; | ||
default?: T; | ||
} | ||
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
542609
6499
1040