https://github.com/codemix/type-registry
Provides a container which can hold different kinds of Type.
https://github.com/codemix/type-registry
Last synced: 3 months ago
JSON representation
Provides a container which can hold different kinds of Type.
- Host: GitHub
- URL: https://github.com/codemix/type-registry
- Owner: codemix
- License: mit
- Created: 2016-02-11T19:50:53.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-26T18:34:29.000Z (almost 10 years ago)
- Last Synced: 2025-02-21T06:35:04.701Z (about 1 year ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# type-registry
A simple container used to declare different kinds of Type.
[](https://travis-ci.org/codemix/type-registry)
## What?
Yeah. This doesn't make much sense in isolation, it's part of a larger (soon to be released) project centering around typed objects.
## Installation
Install via [npm](https://npmjs.org/package/type-registry).
```sh
npm install type-registry
```
## Usage
```js
import TypeRegistry from "type-registry";
const Uint32 = {
id: 1, // Unique id for this type, mandatory
name: 'UInt32', // Unique name for this type, can be a string or symbol, mandatory.
anythingElseYouWant: 'goes here'
};
const Float64 = {
id: 2,
name: 'Float64'
};
const registry = new TypeRegistry();
registry.has(Uint32); // false
registry.add(Uint32);
registry.has(Uint32); // true
let added = false;
registry.on('add', (newType) => {
added = true;
});
added === false;
registry.add(Float64);
added === true;
registry.get('Uint32') === Uint32;
registry.get(1) === Uint32;
registry.get(2) === Float64;
registry.T.Uint32 === Uint32;
registry.T.Float64 === Float64;
registry.I[1] === Uint32;
registry.I[2] === Float64;
registry.isValidType({}); // false
registry.isValidType({id: 123}); // false
registry.isValidType({id: 123, name: 'User'}); // true
function User {};
registry.isValidType(User); // false
User.id = 456;
registry.isValidType(User); // true
console.log('Total number of items in the registry:', registry.size);
for (const type of registry) {
console.log(type);
}
```
## License
Published by [codemix](http://codemix.com/) under a permissive MIT License, see [LICENSE.md](./LICENSE.md).