https://github.com/karimsa/gordon
a natural language event-emitter interface
https://github.com/karimsa/gordon
Last synced: 3 months ago
JSON representation
a natural language event-emitter interface
- Host: GitHub
- URL: https://github.com/karimsa/gordon
- Owner: karimsa
- Created: 2015-08-13T18:56:31.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-27T04:18:57.000Z (almost 9 years ago)
- Last Synced: 2025-03-11T21:03:58.043Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 95.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gordon
a natural language event-emitter interface.
[](https://nodei.co/npm/gordon/)
## usage
Gordon's interface is based on the traditional event emitter, but with a few twists to support the ideas
of natural language. Event names work like normal, but [pennyworth](https://github.com/karimsa/pennyworth)
templates can be associated with event names for a more natural experience.When `.emit()` is called with a natural language string, gordon will find the most appropriate registered
event and use its most appropriate template to parse the emitted text. The event will then be fired with
the event data being the compiled template.### initializing a new emitter
```javascript
var Gordon = require('gordon');
var emitter = new Gordon();
```### adding an event listener
For unregistered events, you can use `.when()` to attach an event
handler to a trigger template.```javascript
emitter.when('my name is $subject.', function (data) {
console.log('hello there, %s', data.subject);
});
```### registering an event
To register an event is to associate a certain trigger template or list
of templates with an event. This allows you to hook onto a group of templates
that will provide you with the same data.On every call, you can register a single template or a list of templates. Follow
up calls will not re-allocate the template list but rather append to it.```javascript
emitter.register('introduction', [
'[... hi, hey, hello], $subject.',
'how are you, $subject?'
]);
```### attaching to an event
This works exactly the same as with a regular event emitter.
```javascript
emitter.on('introduction', function (data) {
console.log('hello there, %s.', data.subject);
});
```### emitting an event (using a trigger)
Call emit with a trigger, which is the input data to a pennyworth
template.```javascript
emitter.emit('hey, Gordon.');
```