Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

structurae

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

structurae - npm Package Compare versions

Comparing version 4.0.0-pre.4 to 4.0.0-pre.5

1

index.d.ts

@@ -29,2 +29,3 @@ export { AdjacencyListMixin } from "./adjacency-list";

export { VectorView } from "./vector-view";
export type { ViewConstructor, ViewInstance } from "./view-types";
export { View } from "./view";

@@ -0,1 +1,2 @@

import { Constructor } from "./utility-types";
import type { ComplexView, ViewConstructor, ViewInstance, ViewLayout } from "./view-types";

@@ -11,2 +12,3 @@ export declare class MapView<T extends object> extends DataView implements ComplexView<T> {

static defaultData?: Uint8Array;
static ObjectConstructor: Constructor<unknown>;
static decode<T>(view: DataView, start?: number): T;

@@ -13,0 +15,0 @@ static encode<T>(value: T, view: DataView, start?: number, length?: number, amend?: boolean): number;

2

map-view.js

@@ -6,3 +6,3 @@ export class MapView extends DataView {

const optionalFields = this.optionalFields;
const object = {};
const object = new this.ObjectConstructor();
for (let i = 0; i < fields.length; i++) {

@@ -9,0 +9,0 @@ const field = fields[i];

@@ -0,1 +1,2 @@

import { Constructor } from "./utility-types";
import type { ComplexView, ViewInstance, ViewLayout } from "./view-types";

@@ -7,3 +8,3 @@ export declare class ObjectView<T extends object> extends DataView implements ComplexView<T> {

static defaultData?: Uint8Array;
static defaultObject?: Function;
static ObjectConstructor: Constructor<unknown>;
static decode<T extends object>(view: DataView, start?: number, _?: number): T;

@@ -10,0 +11,0 @@ static encode<T extends object>(value: T, view: DataView, start?: number, length?: number, amend?: boolean): number;

@@ -5,3 +5,3 @@ export class ObjectView extends DataView {

const fields = this.fields;
const result = this.defaultObject();
const result = new this.ObjectConstructor();
for (let i = 0; i < fields.length; i++) {

@@ -8,0 +8,0 @@ const name = fields[i];

{
"name": "structurae",
"version": "4.0.0-pre.4",
"version": "4.0.0-pre.5",
"description": "Data structures for performance-sensitive modern JavaScript applications.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -57,3 +57,3 @@ # Structurae

```
import {...} from "https://deno.land/x/structurae@4.0.0-pre.2/index.ts"
import {...} from "https://deno.land/x/structurae@4.0.0-pre.5/index.ts"
```

@@ -60,0 +60,0 @@

@@ -0,1 +1,2 @@

import { Constructor } from "./utility-types";
export interface PrimitiveView<T> extends DataView {

@@ -118,3 +119,3 @@ /**

defaultData?: Uint8Array;
defaultObject?: Function;
ObjectConstructor?: Constructor<T>;
new (...args: any[]): Instance;

@@ -164,3 +165,2 @@ /**

export declare type SchemaTypeName = "string" | "number" | "integer" | "boolean" | "object" | "array";
export declare type SchemaType = string | number | boolean | SchemaObject | SchemaArray | undefined;
export declare type SchemaBinaryType = "map" | "vector" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "float32" | "float64" | "bigint64" | "biguint64";

@@ -183,8 +183,3 @@ export interface Schema {

btype?: SchemaBinaryType;
default?: SchemaType;
default?: unknown;
}
export interface SchemaObject {
[key: string]: SchemaType;
}
export interface SchemaArray extends Array<SchemaType> {
}

@@ -1,2 +0,2 @@

import type { ComplexView, ContainerView, PrimitiveView, Schema, SchemaObject, SchemaType, ViewConstructor, ViewFieldLayout, ViewInstance, ViewLayout } from "./view-types";
import type { ComplexView, ContainerView, PrimitiveView, Schema, ViewConstructor, ViewFieldLayout, ViewInstance, ViewLayout } from "./view-types";
import { ObjectView } from "./object-view";

@@ -7,2 +7,3 @@ import { ArrayView } from "./array-view";

import { TypedArrayView } from "./typed-array-view";
import { Constructor } from "./utility-types";
declare type UnknownViewConstructor = ViewConstructor<unknown, PrimitiveView<unknown> | ContainerView<unknown> | ComplexView<unknown>>;

@@ -21,3 +22,3 @@ export declare class View {

static get maxView(): DataView;
static create<T>(schema: Schema): ViewConstructor<T>;
static create<T>(schema: Schema, constructor?: Constructor<T extends object ? T : never>): ViewConstructor<T>;
static view<T>(view: DataView): ViewInstance<T> | undefined;

@@ -29,7 +30,7 @@ static decode<T>(view: DataView): T | undefined;

static getDefaultData<T extends unknown>(layout: ViewLayout<T>, viewLength: number, fields: Array<keyof T>): Uint8Array;
static getDefaultValue(View: ViewConstructor<unknown, unknown>): string;
static getDefaultConstructor<T>(fields: Array<keyof T>, layout: ViewLayout<T>): Constructor<T>;
static getExistingView<T>(schema: Schema): ViewConstructor<T>;
static getFieldLayout<T extends SchemaType>(field: Schema, start: number, required: boolean, name: string): ViewFieldLayout<T>;
static getMapView<T extends SchemaObject>(schema: Schema): ViewConstructor<T, ComplexView<T>>;
static getObjectView<T extends SchemaObject>(schema: Schema): ViewConstructor<T, ComplexView<T>>;
static getFieldLayout<T>(field: Schema, start: number, required: boolean, name: string): ViewFieldLayout<T>;
static getMapView<T extends object>(schema: Schema, constructor?: Constructor<T>): ViewConstructor<T, ComplexView<T>>;
static getObjectView<T extends object>(schema: Schema, constructor?: Constructor<T>): ViewConstructor<T, ComplexView<T>>;
static getSchemaId(schema: Schema): string;

@@ -36,0 +37,0 @@ static getSchemaOrdering(schema: Schema): Array<Schema>;

@@ -17,3 +17,3 @@ import { BooleanView } from "./boolean-view.js";

}
static create(schema) {
static create(schema, constructor) {
const schemas = this.getSchemaOrdering(schema);

@@ -26,4 +26,4 @@ for (let i = schemas.length - 1; i >= 0; i--) {

const View = objectSchema.btype === "map"
? this.getMapView(objectSchema)
: this.getObjectView(objectSchema);
? this.getMapView(objectSchema, constructor)
: this.getObjectView(objectSchema, constructor);
// cache the view by id

@@ -168,23 +168,36 @@ this.Views.set(id, View);

}
static getDefaultValue(View) {
switch (View) {
case Int8View:
case Int16View:
case Uint16View:
case Uint8View:
case Int32View:
return "0";
case Float32View:
case Float64View:
return "0.0";
case BigInt64View:
case BigUint64View:
return "0n";
case BooleanView:
return "false";
case StringView:
return "''";
default:
return "null";
static getDefaultConstructor(fields, layout) {
const content = [];
for (const field of fields) {
const View = layout[field].View;
let value = "";
switch (View) {
case Int8View:
case Int16View:
case Uint16View:
case Uint8View:
case Int32View:
value = "0";
break;
case Float32View:
case Float64View:
value = "0.0";
break;
case BigInt64View:
case BigUint64View:
value = "0n";
break;
case BooleanView:
value = "false";
break;
case StringView:
value = "''";
break;
default:
value = "null";
}
content.push(`${field}:${value}`);
}
return new Function("return {" + content.join(",") +
"}");
}

@@ -226,3 +239,3 @@ static getExistingView(schema) {

}
static getMapView(schema) {
static getMapView(schema, constructor) {
var _a;

@@ -249,2 +262,4 @@ const required = schema.required || [];

const defaultData = this.getDefaultData(layout, optionalOffset, required);
const ObjectConstructor = constructor ||
this.getDefaultConstructor(required, layout);
return _a = class extends this.MapClass {

@@ -294,5 +309,11 @@ },

}),
Object.defineProperty(_a, "ObjectConstructor", {
enumerable: true,
configurable: true,
writable: true,
value: ObjectConstructor
}),
_a;
}
static getObjectView(schema) {
static getObjectView(schema, constructor) {
var _a;

@@ -310,6 +331,4 @@ const fields = Object.keys(schema.properties);

const defaultData = this.getDefaultData(layout, lastOffset, fields);
const defaultObject = new Function("return {" +
fields.map((key) => `${key}:${this.getDefaultValue(layout[key].View)}`)
.join(",") +
"}");
const ObjectConstructor = constructor ||
this.getDefaultConstructor(fields, layout);
return _a = class extends this.ObjectClass {

@@ -341,7 +360,7 @@ },

}),
Object.defineProperty(_a, "defaultObject", {
Object.defineProperty(_a, "ObjectConstructor", {
enumerable: true,
configurable: true,
writable: true,
value: defaultObject
value: ObjectConstructor
}),

@@ -348,0 +367,0 @@ _a;

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc