https://github.com/openovate/jsm
JavaScript Modules that Work in Node, and Browser
https://github.com/openovate/jsm
javascript-modules
Last synced: 9 months ago
JSON representation
JavaScript Modules that Work in Node, and Browser
- Host: GitHub
- URL: https://github.com/openovate/jsm
- Owner: Openovate
- License: mit
- Created: 2019-09-11T03:25:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-01T22:32:09.000Z (almost 3 years ago)
- Last Synced: 2025-03-03T22:54:33.682Z (10 months ago)
- Topics: javascript-modules
- Language: TypeScript
- Size: 453 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsm
JavaScript Modules that Work in Node, and Browser
## Install
```bash
$ npm i @openovate/jsm
```
## EventEmitter Usage
```js
const { EventEmitter } = require('@openovate/jsm')
const emitter = new EventEmitter;
emitter.on('trigger something', async x => {
console.log('something triggered', x + 1)
})
emitter.on(/trigger (something)/, async x => {
await Helper.sleep(2000)
console.log('(something) triggered', x + 2)
}, 2)
await emitter.emit('trigger something', 1)
```
## Exception Usage
```js
const { Exception } = require('@openovate/jsm')
throw Exception.for('Something %s is %s', 'good', 'bad')
```
## Registry Usage
```js
const { Registry } = require('@openovate/jsm')
const registry = Registry.load()
registry.set('foo', 'bar', 'zoo')
registry.set('foo', 'zoo', ['foo', 'bar', 'zoo'])
console.log(registry.has('foo', 'bar'))
console.log(registry.has('bar', 'foo'))
console.log(registry.get('foo', 'zoo', 1))
registry.remove('foo', 'bar')
console.log(registry.has('foo', 'bar'))
console.log(registry.has('foo', 'zoo'))
```
## TaskQueue Usage
```js
const { TaskQueue } = require('@openovate/jsm')
const queue = new TaskQueue;
queue.push(async x => {
console.log(x + 1)
})
queue.shift(async x => {
await Helper.sleep(2000)
console.log(x + 2)
})
queue.add(async x => {
console.log(x + 3)
}, 10)
await queue.run(1)
```
## Reflection Usage
```js
class Foo {
foo(x, y, z) {
return x + y + z;
}
}
const foo = new Foo;
const names = reflect(foo.foo).getArgumentNames(); //--> x, y, z
const descriptors1 = reflect(Foo1).getDescriptors(); //--> {foo: {...}}
const descriptors2 = reflect(foo).getDescriptors(); //--> {foo: {...}}
const methods1 = reflect(Foo1).getMethods(); //--> {foo: function}
const methods2 = reflect(foo).getMethods(); //--> {foo: function}
```
## traits Usage
```js
const { traits } = require('@openovate/jsm')
class Bar {
bar() {}
}
class Zoo {
zoo() {}
}
class Foo2 extends traits(Bar, Zoo) {
foo() {}
}
const foo = new Foo2
foo.foo()
foo.bar()
foo.zoo()
```