@javelin/core
Version:
This package is a general catch-all for stuff shared by many Javelin packages, including:
104 lines • 4.86 kB
TypeScript
import { CollatedNode, CollatedNodeSchema, Field, FieldArray, FieldBoolean, FieldData, FieldDynamic, FieldExtract, FieldMap, FieldNumber, FieldObject, FieldOf, FieldPrimitive, FieldSet, FieldString, Model, Schema } from "./model";
/**
* Object used in a schema to declare a numeric field.
* @example
* const Wallet = { money: number }
*/
export declare const number: FieldNumber;
/**
* Object used in a schema to declare a string field.
* @example
* const Book = { title: string }
*/
export declare const string: FieldString;
/**
* Object used in a schema to declare a boolean field.
* @example
* const Controller = { jumping: boolean }
*/
export declare const boolean: FieldBoolean;
/**
* Build a field that represents an array within a schema. The sole parameter
* defines the array's element type, which can be another field or Schema.
* @example <caption>primitive element</caption>
* const Bag = { items: arrayOf(number) }
* @example <caption>complex element</caption>
* const Shape = { vertices: arrayOf(arrayOf(number)) }
*/
export declare function arrayOf<$Value extends Field | Schema>(element: $Value): FieldArray<FieldExtract<$Value>>;
/**
* Build a field that represents an object within a schema. The first parameter
* defines the object's element type, which can be another field or Schema. If
* provided, the second argument will override the default key type of `string`
* with another field derived from `string`.
* @example <caption>primitive element</caption>
* const Stats = { values: objectOf(number) }
* @example <caption>`key` type override (e.g. for `@javelin/pack` encoding)</caption>
* const Stat = { min: number, max: number, value: number }
* const Stats = { values: objectOf(Stat, { ...string, length: 20 }) }
*/
export declare function objectOf<$Value extends Field | Schema>(element: $Value, key?: FieldString): FieldObject<FieldExtract<$Value>>;
/**
* Build a field that represents an Set within a schema. The sole parameter
* defines the Set's element type, which can be another field or Schema.
* @example <caption>primitive element</caption>
* const Buffs = { values: setOf(number) }
* @example <caption>complex element</caption>
* const Body = { colliders: setOf(Collider) }
*/
export declare function setOf<$Value extends Field | Schema>(element: $Value): FieldSet<FieldExtract<$Value>>;
/**
* Build a field that represents an Map within a schema. The first parameter
* defines the Map's key type, which must be a primitive field. The second
* argument defines the Map's value type, which can be a field or schema.
* @example <caption>primitive element</caption>
* const Disabled = { entities: mapOf(number, boolean) }
* @example <caption>complex element</caption>
* const PrivateChat = { messagesByClientId: mapOf(string, arrayOf(ChatMessage)) }
*/
export declare function mapOf<$Key, $Value extends Field | Schema>(key: FieldOf<$Key>, element: $Value): FieldMap<$Key, FieldExtract<$Value>>;
/**
* Build a field that represents an unknown type in a schema. Accepts an
* optional parameter which is a factory function that returns an initial
* value for each field.
* @example <caption>`unknown` type</caption>
* const RigidBody = { value: dynamic() }
* @example <caption>library type</caption>
* const RigidBody = { value: dynamic(() => new Rapier.RigidBody()) }
*/
export declare function dynamic<$Value>(get?: FieldData<$Value>["get"]): FieldDynamic<$Value>;
/**
* Determine if an object is a Javelin model field.
*/
export declare function isField<$Type>(object: object): object is FieldData<$Type>;
/**
* Determine if an object is a primitive Javelin model field.
*/
export declare function isPrimitiveField(object: object): object is FieldPrimitive;
/**
* Determine if a Javelin model node represents a schema.
*/
export declare function isSchema<$Type>(node: CollatedNode<$Type>): node is CollatedNodeSchema<$Type>;
/**
* Determine if a Javelin model node is simple. A node is simple if:
* 1. it is primitive
* 2. it is a schema and each of its fields are primitive
* 3. it is a complex field (e.g. array, map) and its element is primitive
*/
export declare function isSimple(node: CollatedNode): boolean;
declare type ModelConfig = Map<number, Schema | Field>;
/**
* Produce a graph from a model and assign each writable field a unique integer
* id.
*/
export declare function createModel<$Props>(config: ModelConfig): Model<$Props>;
/**
* Create an instance of a schema.
*/
export declare function createSchemaInstance<$Type extends Schema>(schema: $Type, object?: FieldExtract<$Type>): FieldExtract<$Type>;
/**
* Reset an instance of a schema.
*/
export declare function resetSchemaInstance<$Type extends Schema>(object: FieldExtract<$Type>, schema: $Type): FieldExtract<$Type>;
export {};
//# sourceMappingURL=model_helpers.d.ts.map