@rustable/utils
Essential utilities for object cloning, string manipulation, and value comparison in TypeScript, inspired by Rust's standard library.
📦 Installation
npm install @rustable/utils
yarn add @rustable/utils
pnpm add @rustable/utils
✨ Features
- 🔤 String Utils - Robust string conversion and manipulation
- 🔗 Hash System - Consistent hash values for all types
- 📦 Deep Clone - Comprehensive object cloning system
- 🔍 Deep Equal - Type-safe deep equality comparison
- 📈 Value Management - Safe value manipulation utilities
- 🔗 Pointer System - Mutable reference functionality
- 🛡️ Type Safety - Full TypeScript type system support
- ⚡ Performance - Optimized implementations
- 🔄 Circular Refs - Handles circular references
- 📦 Zero Dependencies - No external runtime dependencies
📚 Key Components
String Manipulation (stringify.ts
)
- Robust string conversion for all JavaScript types
- Handles circular references and complex objects
- Produces deterministic output by sorting object keys
- Comprehensive type conversion rules
import { stringify } from '@rustable/utils';
stringify(42);
stringify('hello');
stringify(null);
stringify(undefined);
const user = {
name: 'John',
info: { age: 30 },
};
stringify(user);
stringify(Symbol('key'));
stringify(42n);
stringify(() => {});
stringify([1, 2, 3]);
stringify(new Map([['a', 1]]));
stringify(new Date(1234567890));
Hashing (hash.ts
)
- Consistent hash values for all JavaScript types
- Special handling for primitives and objects
import { hash } from '@rustable/utils';
hash('hello');
hash(42);
hash(true);
hash(false);
hash(null);
hash(undefined);
const obj = { x: 1, y: 2 };
hash(obj);
Object Cloning (clone.ts
)
- Comprehensive deep cloning system
- Handles circular references
- Supports custom clone methods
- Special handling for built-in types (Date, RegExp, Set, Map)
import { deepClone } from '@rustable/utils';
const num = deepClone(42);
const str = deepClone('hello');
const original = {
date: new Date(),
regex: /test/g,
set: new Set([1, 2, 3]),
map: new Map([
['a', 1],
['b', 2],
]),
nested: { array: [1, 2, 3] },
};
const cloned = deepClone(original);
class Point {
constructor(
public x: number,
public y: number,
) {}
clone() {
return new Point(this.x, this.y);
}
}
const point = new Point(1, 2);
const clonedPoint = deepClone(point);
Equality Comparison (eq.ts
)
- Deep equality comparison using value serialization
- Handles all JavaScript types consistently
- Supports complex objects and nested structures
import { equals } from '@rustable/utils';
equals(42, 42);
equals('hello', 'hello');
equals({ x: 1 }, { x: 1 });
equals([1, 2], [1, 2]);
const obj1 = { data: { points: [1, 2] } };
const obj2 = { data: { points: [1, 2] } };
equals(obj1, obj2);
Value Management (val.ts
)
- Provides utilities for value manipulation and comparison
- Supports primitive and object types
- Implements safe equality comparisons
import { Val } from '@rustable/utils';
const original = { count: 0, data: [1, 2, 3] };
const val = Val(original);
val.count = 1;
val.data.push(4);
console.log(original.count);
console.log(original.data);
const originalRef = val[Val.ptr];
console.log(originalRef === original);
Pointer Management (ptr.ts
)
- Provides mutable reference functionality
- Supports transparent property access
- Includes value replacement utilities
- Type-safe implementation
import { Ptr } from '@rustable/utils';
class Counter {
count = 0;
increment() {
this.count++;
}
}
let counter = new Counter();
const ptr = Ptr({
get: () => counter,
set: (v) => (counter = v),
});
ptr.increment();
console.log(counter.count);
ptr[Ptr.ptr] = new Counter();
📄 License
MIT © illuxiza