UNPKG

clwoz-models

Version:
75 lines 2.85 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); var findOrThrow = function (haystack_fn, needle) { var i = haystack_fn(needle); if (i === -1) throw new RangeError(needle + " not found"); return i; }; /** * Finds the index of the first instance of needle in haystack, or throws an exception if not found. */ var findFirstOrThrow = function (haystack, needle) { return findOrThrow(haystack.indexOf.bind(haystack), needle); }; /** * Finds the index of the last instance of needle in haystack, or throws an exception if not found. */ var findLastOrThrow = function (haystack, needle) { return findOrThrow(haystack.lastIndexOf.bind(haystack), needle); }; /** * Parses OBI .dialog files from custom format into structured data. */ var ObiUtils = /** @class */ (function () { function ObiUtils() { } ObiUtils.addToLGMap = function (text, lgMap) { var items = text.split('# '); for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { var item = items_1[_i]; item = item.trim(); if (item.length === 0) { continue; } var lgName = item.substring(0, findFirstOrThrow(item, "-")).trim(); var backticksStart = findFirstOrThrow(item, "```"); var suggestionsStart = findLastOrThrow(item, "["); if (backticksStart > suggestionsStart) { throw new RangeError('``` not found'); } var body = item.substring(backticksStart + 3, suggestionsStart).trim(); var suggestionTag = "[Suggestions="; var suggestionList = item.substring(findLastOrThrow(item, suggestionTag) + suggestionTag.length, findLastOrThrow(item, "]")); var suggestions = suggestionList.length > 0 ? suggestionList.split('|') : []; var lgItem = { lgName: lgName, text: body, suggestions: suggestions, hash: hashText(body) }; lgMap.push(lgItem); } }; return ObiUtils; }()); exports.ObiUtils = ObiUtils; // Calculate a 32 bit FNV-1a hash // Ref.: http://isthe.com/chongo/tech/comp/fnv/ function hashText(text) { // tslint:disable:no-bitwise var l = text.length; var hval = 0x811C9DC5; // seed for (var i = 0; i < l; i = i + 1) { hval ^= text.charCodeAt(i); hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); } // Return 8 digit hex string return ("0000000" + (hval >>> 0).toString(16)).substr(-8); } exports.hashText = hashText; //# sourceMappingURL=ObiUtils.js.map