@rustable/utils
This is the utilities package of the Rustable project, providing fundamental utilities and type management functionalities. The package implements essential features like type identification, object cloning, hashing, string manipulation, and mutable references.
Installation
npm install @rustable/utils
yarn add @rustable/utils
pnpm add @rustable/utils
Key Components
Type Identification System (type_id.ts
)
- Provides a robust type identification system
- Uses WeakMap for efficient memory management
- Supports generic type parameters
- Ensures type safety using TypeScript's branded type pattern
String Manipulation (stringify.ts
)
- Advanced string manipulation and conversion utilities
- Handles complex object to string conversions
Hashing (hash.ts
)
- Implements hashing functionality for various data types
- Provides consistent hash generation
Object Cloning (clone.ts
)
- Deep cloning utilities for objects
- Handles complex data structures
Equality Comparison (eq.ts
)
- Implements equality comparison functionality
- Supports deep equality checks
Mutable Reference (mut.ts
)
- Provides a mutable reference with getter and setter functions
- Supports generic types for flexible usage
Immutable Reference (ref.ts
)
- Provides an immutable reference to values
- Creates a deep clone of the original value
- Supports independent modifications without affecting the original
Usage
Import the required utilities from the package:
import { typeId, clone, hash, stringify, Mut, Ref } from '@rustable/utils';
Example: Using Type ID
class MyClass {}
const id = typeId(MyClass);
class Container<T> {}
const stringContainerId = typeId(Container, [String]);
Example: Deep Cloning
import { deepCopy } from '@rustable/utils';
const original = { name: 'John', age: 30 };
const clone = deepCopy(original);
const complexObj = {
data: [1, 2, 3],
date: new Date(),
};
complexObj.self = complexObj;
const cloned = deepCopy(complexObj);
Example: Hashing
import { hash } from '@rustable/utils';
const numberHash = hash(42);
const stringHash = hash('Hello World');
const objectHash = hash({ x: 1, y: 2 });
const arrayHash = hash([1, 2, 3]);
Example: Equality Comparison
import { equals } from '@rustable/utils';
console.log(equals(5, 5));
console.log(equals('hello', 'hello'));
const obj1 = { x: 1, y: [1, 2, 3] };
const obj2 = { x: 1, y: [1, 2, 3] };
console.log(equals(obj1, obj2));
Example: Using Mutable Reference
import { Mut } from '@rustable/utils';
let obj = { name: 'Alice', age: 30 };
const mutRef = Mut.of({
get: () => obj,
set: (newValue) => {
obj = newValue;
},
});
console.log(mutRef.name);
mutRef.age = 31;
console.log(obj.age);
mutRef[Mut.ptr] = { name: 'Bob', age: 25 };
console.log(obj);
console.log(mutRef[Mut.ptr]);
Mut.replace(mutRef, { name: 'Charlie', age: 20 });
console.log(obj);
let nested = {
info: {
name: 'Alice',
hobbies: ['reading'],
},
};
const nestedRef = Mut.of({
get: () => nested,
set: (newValue) => {
nested = newValue;
},
});
nestedRef.info.hobbies.push('coding');
console.log(nested.info.hobbies);
Mut.replace(nestedRef, {
info: {
name: 'Bob',
hobbies: ['gaming'],
},
});
console.log(nested);
Example: Using Immutable Reference
import { Ref } from '@rustable/utils';
const obj = { name: 'Alice', age: 30 };
const ref = Ref.of(obj);
ref.name = 'Bob';
console.log(ref.name);
console.log(obj.name);
console.log(ref[Ref.ptr].name);
Ref
The Ref
type provides a way to create immutable references to values. Unlike Mut
, which tracks mutations to the original value, Ref
creates an independent copy that can be modified without affecting the original.
Usage
import { Ref } from '@congeer/utils';
const obj = { name: 'Alice', age: 30 };
const ref = Ref.of(obj);
ref.name = 'Bob';
console.log(ref.name);
console.log(obj.name);
console.log(ref[Ref.ptr].name);
Features
- Deep Cloning: Creates a deep clone of the original value, ensuring complete isolation
- Independent Modifications: The reference can be freely modified without affecting the original
- Original Access: The original value can be accessed through
Ref.ptr
symbol
- Method Support: All methods work on the cloned value, preserving the original
Example with Complex Objects
const arr = [1, 2, 3];
const arrRef = Ref.of(arr);
arrRef.push(4);
console.log([...arrRef]);
console.log(arr);
class User {
constructor(public name: string) {}
setName(name: string) {
this.name = name;
return this;
}
}
const user = new User('Alice');
const userRef = Ref.of(user);
userRef.setName('Bob');
console.log(userRef.name);
console.log(user.name);
When to Use
- When you need to experiment with modifications without affecting the original
- When you want to maintain a separate copy of a value
- When you need to compare modified state with original state
- In scenarios where immutability of the original value is critical
Comparison with Mut
While Mut
tracks and propagates changes to the original value, Ref
provides isolation:
const mut = Mut.of({ value: 1 });
mut.value = 2;
console.log(mut[Mut.ptr].value);
const ref = Ref.of({ value: 1 });
ref.value = 2;
console.log(ref[Ref.ptr].value);
Notes
- All utilities are designed with TypeScript's type system in mind
- The package uses WeakMap for efficient memory management
- Generic type support is available where applicable
- The
Mut
type provides a proxy-based mutable reference that allows direct property access and modification
- The
Ref
type provides an immutable reference to values, creating a deep clone of the original value
License
MIT illuxiza