2.0.0 β’ Published 2 years ago
tydy v2.0.0
π TyDy
π A TypeScript library for runtime type safety and dynamic type management.
π Introduction
TyDy (pronounced /ΛtaΙͺ daΙͺ/) is a TypeScript library that provides robust runtime type safety. It is an essential tool for working with dynamic data structures and APIs where data types can be unpredictable.
π Features
- πΌ Dynamic Type Management: Effortlessly define and manage dynamic types during runtime.
- β Runtime Type Validation: Automatically validates data against your dynamic type definitions.
- π Type Observability: Programmatically respond to type changes.
- π JSON Type Inference: Automatically infer types from complex JSON data structures.
π Visualizing TyDy
flowchart TD
subgraph "π TyDy Library"
subgraph "πΌ Dynamic Type Management"
TypeRegistry[TypeRegistry]
end
subgraph "β
Runtime Type Validation"
TypeValidator[TypeValidator]
end
subgraph "π Type Observability"
TypeObserver[TypeObserver]
end
subgraph "π JSON Type Inference"
JsonTypeInferencer[JsonTypeInferencer]
end
end
TypeRegistry -->|Used by| TypeValidator
TypeRegistry -->|Used by| TypeObserver
TypeRegistry -->|Used by| JsonTypeInferencer
TypeValidator -->|Validates| TypeRegistry
TypeObserver -->|Observes| TypeRegistry
JsonTypeInferencer -->|Infers| TypeRegistry
π¦ Installation
npm install tydy --save
π Usage
π Type Observability with Observer Hooks
import { TypeObserver } from "tydy";
// Initialize a new observer instance
const observer = new TypeObserver();
// Register event handlers
observer.on("typeAdded", (typeName: string) => {
console.log(`Type added: ${typeName}`);
});
observer.on("typeRemoved", (typeName: string) => {
console.log(`Type removed: ${typeName}`);
});
// Operations to add and remove types, which trigger the event handlers
observer.addType("MyDynamicType");
observer.removeType("MyDynamicType");
π JSON Type Inference
import { JsonTypeInferencer, InferableType } from 'tydy';
// Initialize the inferencer
const inferencer = new JsonTypeInferencer();
const result = inferencer.inferNestedTypes({ name: 'Alice', age: 30, address: { street: '123 Elm St' } });
// Check if the result is an object and log its properties
if (result.type === InferableType.Object || result.type === InferableType.NestedObject) {
console.log(result.properties);
}
πΌ Dynamic Type Management
import { TypeRegistry } from "tydy";
const typeSchema = { name: "string", age: "number" };
const MyDynamicType = TypeRegistry.registerType(typeSchema);
β Runtime Type Validation
import { TypeValidator } from "tydy";
// Define a complex dynamic type
const MyDynamicType = {
name: "string",
age: "number",
address: {
street: "string",
zipCode: "number"
}
};
// Create an object to validate
const myObject = {
name: "Alice",
age: 30,
address: {
street: "123 Elm St",
zipCode: 12345
}
};
// Validate the object against the dynamic type
const isValid = TypeValidator.validate(myObject, MyDynamicType);
console.log(isValid);
π Contributing
Contributions are welcomed! For more information on how to contribute, please see our contribution guidelines.
π License
This project is licensed under the MIT License. For complete details, see the LICENSE file.
2.0.0
2 years ago
0.1.25
2 years ago
0.1.24
2 years ago
0.1.22
2 years ago
0.1.21
2 years ago
0.1.20
2 years ago
0.1.19
2 years ago
0.1.17
2 years ago
0.1.16
2 years ago
0.1.15
2 years ago
0.1.13
2 years ago
0.1.12
2 years ago
0.1.11
2 years ago
0.1.8
2 years ago
0.1.4
2 years ago
0.1.3
2 years ago
0.1.2
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago