@botmock-api/utils
Advanced tools
Comparing version 1.4.1 to 2.0.0
@@ -7,2 +7,3 @@ import * as utils from "../src"; | ||
}); | ||
test.todo("map has string keys and array values"); | ||
}); | ||
@@ -18,9 +19,45 @@ | ||
}); | ||
test.todo("returned function returns an array of message ids"); | ||
}); | ||
// describe("topological sorting", () => { | ||
// test("returns an array of messages", () => { | ||
// expect(utils.topoSort([]) instanceof Array).toBe(true); | ||
// }); | ||
// test.todo("returns sorted array"); | ||
// }); | ||
describe("topological sorting", () => { | ||
const FIRST_MESSAGE_ID = "d7f423ca-8147-4b64-8b7b-b1c07cc1b9fc"; | ||
const SECOND_MESSAGE_ID = "cb436662-d82d-4f7e-bce5-6d4ce9ffa86c"; | ||
const messages = [ | ||
{ | ||
message_id: SECOND_MESSAGE_ID, | ||
message_type: "text", | ||
next_message_ids: [], | ||
previous_message_ids: [{ message_id: FIRST_MESSAGE_ID, action: "*" }], | ||
is_root: false, | ||
payload: { | ||
nodeName: "x", | ||
context: [], | ||
text: "👋", | ||
workflow_column_id: "default_column", | ||
assigned_to: "0" | ||
} | ||
}, | ||
{ | ||
message_id: FIRST_MESSAGE_ID, | ||
message_type: "text", | ||
next_message_ids: [ | ||
{ | ||
message_id: SECOND_MESSAGE_ID, | ||
action: "", | ||
conditional: false, | ||
intent: { value: "" } | ||
} | ||
], | ||
previous_message_ids: [], | ||
is_root: true, | ||
payload: {} | ||
} | ||
]; | ||
test("returns sorted array", () => { | ||
const [{ message_id }] = utils.topoSort(messages); | ||
expect(message_id).toBe(FIRST_MESSAGE_ID); | ||
expect(utils.topoSort(messages)).toHaveLength(messages.length); | ||
}); | ||
test.todo("throws error in the case of project that contains cycles"); | ||
}); |
@@ -89,2 +89,19 @@ "use strict"; | ||
*/ | ||
// export const topoSort = (messages: Message[]) => {}; | ||
exports.topoSort = function (messages) { | ||
if (messages === void 0) { messages = []; } | ||
var orderedMessages = messages; | ||
try { | ||
var i = 0; | ||
// insert the message with in-degree 0 to the start of the returned array | ||
while (i !== messages.length) { | ||
var j = orderedMessages.findIndex(function (message) { return !message.previous_message_ids.length; }); | ||
var message = orderedMessages.splice(j, 1)[0]; | ||
orderedMessages.unshift(message); | ||
i += 1; | ||
} | ||
} | ||
catch (_) { | ||
throw "cannot perform sort on project that contains cycles"; | ||
} | ||
return orderedMessages; | ||
}; |
{ | ||
"name": "@botmock-api/utils", | ||
"version": "1.4.1", | ||
"version": "2.0.0", | ||
"description": "utilities for handling data from the Botmock API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -25,7 +25,7 @@ type Utterance = { | ||
payload: { | ||
nodeName: string; | ||
context: any[]; | ||
text: string; | ||
workflow_column_id: string; | ||
assigned_to: string; | ||
nodeName?: string; | ||
context?: any[]; | ||
text?: string; | ||
workflow_column_id?: string; | ||
assigned_to?: string; | ||
}; | ||
@@ -129,2 +129,19 @@ previous_message_ids: { message_id: string; action: string | {} }[]; | ||
*/ | ||
// export const topoSort = (messages: Message[]) => {}; | ||
export const topoSort = (messages: Message[] = []) => { | ||
const orderedMessages: Message[] = messages; | ||
try { | ||
let i: number = 0; | ||
// insert the message with in-degree 0 to the start of the returned array | ||
while (i !== messages.length) { | ||
const j = orderedMessages.findIndex( | ||
message => !message.previous_message_ids.length | ||
); | ||
const [message] = orderedMessages.splice(j, 1); | ||
orderedMessages.unshift(message); | ||
i += 1; | ||
} | ||
} catch (_) { | ||
throw "cannot perform sort on project that contains cycles"; | ||
} | ||
return orderedMessages; | ||
}; |
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
20048
325