@botmock-api/utils
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,12 +0,28 @@ | ||
const { createIntentMap, createNodeCollector } = require('../'); | ||
import { enumeratePaths, createIntentMap, createNodeCollector } from '../src'; | ||
test('`createIntentMap` returns a map', () => { | ||
expect(createIntentMap()).toEqual(new Map()); | ||
describe('enumeratePaths', () => { | ||
test('throws if not given root', () => { | ||
expect(() => { | ||
enumeratePaths(); | ||
}).toThrow(); | ||
}); | ||
test('returns a set', () => { | ||
expect( | ||
enumeratePaths([ | ||
{ is_root: true, message_id: 0, next_message_ids: [] } | ||
]) instanceof Set | ||
).toBeTruthy(); | ||
}); | ||
}); | ||
test('`createNodeCollector` returns a function', () => { | ||
expect(() => { | ||
createNodeCollector(); | ||
}).not.toThrow(); | ||
expect(createNodeCollector() instanceof Function).toBeTruthy(); | ||
describe('createIntentMap', () => { | ||
test('returns a map', () => { | ||
expect(createIntentMap() instanceof Map).toBeTruthy(); | ||
}); | ||
}); | ||
describe('createNodeCollector', () => { | ||
test('returns a function', () => { | ||
expect(createNodeCollector() instanceof Function).toBeTruthy(); | ||
}); | ||
}); |
@@ -1,1 +0,1 @@ | ||
export { createIntentMap, createNodeCollector } from './src'; | ||
export { enumeratePaths, createIntentMap, createNodeCollector } from './src'; |
{ | ||
"name": "@botmock-api/utils", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "utilities for handling data from the Botmock API", | ||
@@ -27,4 +27,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@babel/core": "7.4.3", | ||
"@babel/preset-env": "7.4.3", | ||
"babel-jest": "24.7.1", | ||
"jest": "24.7.1" | ||
} | ||
} |
# utils | ||
``` | ||
```console | ||
npm i @botmock-api/utils | ||
@@ -9,16 +9,27 @@ ``` | ||
### API | ||
## API | ||
``` | ||
```js | ||
import * as utils from '@botmock-api/utils'; | ||
``` | ||
#### `utils.createIntentMap(messages: []): Map<messageId: string, intentIds: string[]>` | ||
```js | ||
utils.enumeratePaths({ messages: string[], connections: string[] }): Set<Array<string>> | ||
``` | ||
Function that returns a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) | ||
which contains arrays with the message ids of each possible journey in the provided messages + connections | ||
```js | ||
utils.createIntentMap(messages: []): Map<messageId: string, intentIds: string[]> | ||
``` | ||
Function that returns a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) | ||
which associates message ids with the list of intent ids connected to them | ||
#### `utils.createNodeCollector(map: Map, getter: (id: string) => Object): (next: string[]) => string[]` | ||
```js | ||
utils.createNodeCollector(map: Map, get: (id: string) => ({}): (arr: string[]) => string[] | ||
``` | ||
Creates function that turns `next_messages` into a collection of _all_ reachable | ||
messages that are not connected by an intent |
@@ -0,1 +1,27 @@ | ||
// Return a set representing all possible "journeys" in the project as arrays | ||
export const enumeratePaths = (messages = []) => { | ||
const root = messages.find(m => !!m.is_root); | ||
if (typeof root === 'undefined') { | ||
throw new Error('messages must include a root message'); | ||
} | ||
const s = new Set([root.message_id]); | ||
// Recurse on next_message_ids, beginning with root | ||
(function f(nextIds) { | ||
for (const { message_id } of nextIds) { | ||
// Add the union of the last element in the set and the current message | ||
// id to the set | ||
const lastSetElement = Array.from(s).pop(); | ||
const message = messages.find(m => m.message_id === message_id); | ||
s.add([ | ||
...(typeof lastSetElement === 'string' | ||
? [lastSetElement] | ||
: lastSetElement), | ||
message.message_id | ||
]); | ||
f(message.next_message_ids); | ||
} | ||
})(root.next_message_ids); | ||
return s; | ||
}; | ||
// Return a map associating message id and array of intent ids connected to it | ||
@@ -39,2 +65,3 @@ export const createIntentMap = (messages = []) => { | ||
// Return a function that collects reachable nodes not connected by intents | ||
export const createNodeCollector = (map, getMessage) => | ||
@@ -52,2 +79,1 @@ function f(next, collected = []) { | ||
}; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12517
9
115
35
4