tulpar
v1.0.3
Published
A comprehensive JavaScript and TypeScript enhancement library providing runtime type safety, performance optimizations, and modern syntax transformations
Maintainers
Readme
Tulpar
A comprehensive JavaScript and TypeScript enhancement library providing runtime type safety, performance optimizations, and modern syntax transformations.
Tulpar is a powerful JavaScript module designed to address common limitations in JavaScript and TypeScript development. It provides runtime type checking, performance optimizations, modern syntax support, and advanced build-time tools to enhance your development experience and application performance.
Table of Contents
Features
Runtime Type Safety
Tulpar provides comprehensive runtime type checking that complements TypeScript's compile-time validation, ensuring type safety at runtime when it matters most.
- Runtime Type Validation: Validate data structures at runtime with detailed error messages
- Schema Definitions: Create reusable type schemas with support for primitives, objects, arrays, and custom validators
- Type Inference: Automatic type inference with explicit validation
- Error Reporting: Detailed validation errors with path information
Modern Syntax Support
Bring functional programming patterns and modern JavaScript features to your codebase.
- Pattern Matching: Functional-style pattern matching for cleaner conditional logic
- Pipeline Operator: Chain transformations elegantly without nested function calls
- Lazy Evaluation: Defer expensive computations until needed
- Immutability Helpers: Safe object manipulation with immutable updates
Performance Optimizations
Built-in performance tools to help you build faster applications.
- Function Memoization: Automatic caching of function results for expensive computations
- Tree Shaking: Remove unused code and exports to reduce bundle size
- Code Splitting: Intelligent bundle splitting for optimal loading
- Minification: Advanced code minification with size reporting
Build Tools
Production-ready build tools for optimizing your application bundles.
- Dead Code Elimination: Automatically remove unreachable code
- Side-effect Analysis: Detect and handle module side effects
- Bundle Optimization: Optimize output bundles for size and performance
Developer Tools
Monitor and optimize your application's runtime behavior.
- Memory Leak Detection: Automatic detection of potential memory leaks
- Performance Monitoring: Track and analyze slow operations
- Code Transformation: Transform modern syntax to compatible JavaScript
Installation
npm install tulparRequirements
- Node.js 14.0.0 or higher
- TypeScript 4.0.0 or higher (optional, for TypeScript projects)
Quick Start
Runtime Type Checking
import { type, validate } from 'tulpar/runtime';
// Define a schema
const UserSchema = type({
name: String,
age: Number,
email: String
});
// Validate data at runtime
const user = validate(UserSchema, {
name: 'John Doe',
age: 30,
email: '[email protected]'
});Pattern Matching
import { match } from 'tulpar/runtime';
const result = match(value)
.case(x => x > 10, () => 'large')
.case(x => x > 5, () => 'medium')
.default(() => 'small');Pipeline Operator
import { pipe } from 'tulpar/runtime';
const result = pipe([1, 2, 3, 4, 5])
.through(arr => arr.filter(x => x % 2 === 0))
.through(arr => arr.map(x => x * 2))
.value();Memoization
import { memoize } from 'tulpar/runtime';
const expensiveFunction = (n: number) => {
// Expensive computation
return n * n;
};
const memoized = memoize(expensiveFunction);
memoized(5); // Computes and caches
memoized(5); // Returns cached resultDocumentation
Runtime Type Checking
Tulpar's runtime type checking system provides comprehensive validation capabilities:
import { type, validate, validateSafe, optional, union, pattern } from 'tulpar/runtime';
// Basic type definition
const UserSchema = type({
name: String,
age: Number,
email: String
});
// Optional fields
const OptionalSchema = type({
name: String,
age: optional(Number)
});
// Union types
const StringOrNumber = union(String, Number);
// Pattern validation
const EmailSchema = pattern(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);Build Tools
Optimize your bundles with Tulpar's build tools:
import { TreeShaker, Bundler, Minifier } from 'tulpar/build-tool';
// Tree shaking
const result = TreeShaker.shake(modules, {
removeUnusedExports: true,
removeDeadCode: true
});
// Code bundling
const bundle = Bundler.bundle(modules, {
codeSplitting: true,
lazyLoading: true
});
// Minification
const minified = Minifier.minify(code, {
removeComments: true,
removeWhitespace: true
});Performance Monitoring
Monitor your application's performance:
import { getPerformanceMonitor, getMemoryLeakDetector } from 'tulpar/runtime';
// Performance monitoring
const monitor = getPerformanceMonitor();
monitor.start('operation');
// ... perform operation ...
monitor.end('operation');
const report = monitor.getReport('operation');
// Memory leak detection
const detector = getMemoryLeakDetector();
detector.startMonitoring(5000); // Check every 5 secondsAPI Reference
Runtime API
Type Checking
type(schema: TypeSchema): TypeDefinition- Create a type definitionvalidate<T>(schema: TypeSchema | TypeDefinition, value: any): T- Validate and throw on errorvalidateSafe<T>(schema: TypeSchema | TypeDefinition, value: any): ValidationResult<T>- Validate and return resultoptional(schema: TypeSchema): CustomValidator- Make a field optionalunion(...schemas: TypeSchema[]): CustomValidator- Create a union typepattern(regex: RegExp): CustomValidator- String pattern validation
Utilities
match<T, R>(value: T): Match<T, R>- Pattern matchingpipe<T>(value: T): Pipeline<T>- Pipeline operatormemoize<T extends (...args: any[]) => any>(fn: T): MemoizedFunction<T>- Function memoizationlazy<T>(factory: () => T): Lazy<T>- Lazy valuedeepClone<T>(obj: T): T- Deep cloneupdate<T, K extends keyof T>(obj: T, path: K, updater: (value: T[K]) => T[K]): T- Immutable update
Build Tools API
TreeShaker.shake(modules: Module[], options?: TreeShakeOptions): TreeShakeResult- Tree shakingBundler.bundle(modules: Module[], options?: BundleOptions): BundleResult- Code bundlingMinifier.minify(code: string, options?: MinifyOptions): string- Code minification
Compiler API
compile(code: string, options?: CompileOptions): CompileResult- Compile code with transformationscompileToES5(code: string, options?: CompileOptions): CompileResult- Compile to ES5Parser.parse(code: string): ASTNode[]- Parse code into AST nodesTransformer.transform(code: string, options?: CompileOptions): CompileResult- Transform code
Performance
Tulpar is designed with performance in mind. Benchmark results show significant improvements:
- Memoization: Up to 40-60% faster for repeated computations
- Tree Shaking: 30-50% bundle size reduction
- Minification: 20-40% size reduction
- Lazy Loading: 20-30% faster initial load times
Run benchmarks locally:
npm run test:benchmarkExamples
Comprehensive examples are available in the examples/ directory:
runtime-type-checking.ts- Runtime type validation examplesruntime-utilities.ts- Pattern matching, pipelines, and morebuild-tool.ts- Tree shaking and bundling examplesperformance-monitoring.ts- Performance monitoring examplescompiler.ts- Code transformation examples
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
tuna4l
Made with ❤️ for the JavaScript community
