deca-memory-utils
A TypeScript utility library providing memory management tools like UniquePtr for better control over object ownership and lifecycle.
Installation
npm install deca-memory-utils
Features
- UniquePtr: A TypeScript implementation of unique pointer pattern, ensuring single ownership of resources
- Type-safe implementation with full TypeScript support
- Zero dependencies
- Comprehensive test coverage
- Full ESM (ECMAScript Modules) support
Module Support
This package is distributed as an ES Module (ESM). Make sure your project is configured to handle ESM:
{
"type": "module"
}
If you're using TypeScript, ensure your tsconfig.json includes:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node"
}
}
Usage
import { UniquePtr } from 'deca-memory-utils';
const ptr1 = new UniquePtr({ name: "John", data: [1, 2, 3] });
console.log(ptr1.get());
const ptr2 = ptr1.move();
console.log(ptr1.get());
console.log(ptr2.get());
ptr2.reset({ name: "Jane", data: [4, 5, 6] });
const releasedValue = ptr2.release();
console.log(releasedValue);
console.log(ptr2.get());
API Reference
UniquePtr
Constructor
constructor(value: T | null = null): Creates a new UniquePtr instance
Methods
get(): T | null: Access the underlying object
release(): T | null: Release ownership and return the object
reset(newValue: T | null = null): void: Reset with a new value
move(): UniquePtr<T>: Transfer ownership to a new UniquePtr
Project Structure
deca-memory-utils/
├── .gitignore
├── .npmignore
├── package.json
├── tsconfig.json
├── jest.config.cjs
├── README.md
├── LICENSE
├── src/
│ ├── index.ts
│ └── unique-ptr.ts
├── tests/
│ └── unique-ptr.test.ts
└── examples/
└── basic-usage.ts
Development
Prerequisites
- Node.js (v16 or higher recommended)
- npm (v7 or higher)
Setup
git clone https://github.com/yourusername/deca-memory-utils.git
cd deca-memory-utils
npm install
Scripts
npm test
npm run build
npm run lint
Testing
The project uses Jest with ts-jest for testing. The setup includes:
- Full ESM support using Node.js experimental modules
- TypeScript integration via ts-jest
- Test files should use
.test.ts extension
- Tests are located in the
tests directory
To run tests:
npm test
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
Changelog
[1.0.0] - 2024-11-06
Added
- Initial release
- UniquePtr implementation with full TypeScript support
- ESM module configuration
- Basic ownership management functionality:
- get() method for accessing values
- move() method for ownership transfer
- reset() method for value replacement
- release() method for ownership release
- Prevention of accidental copying via toJSON and valueOf
- Comprehensive test suite with Jest
- Example usage code
- Full API documentation
Versioning
This project follows Semantic Versioning. For the versions available, see the tags on this repository.
License
MIT