Socket
Socket
Sign inDemoInstall

js-worker-search

Package Overview
Dependencies
3
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.1 to 1.3.0

dist/2ef0afcacb6f46b34be5.worker.js

3

CHANGELOG.md
Changelog
-----
#### 1.3.0
* Added `terminate` method to enable library users to kill the web worker ([@LrsK](https://github.com/LrsK) - [#15](https://github.com/bvaughn/js-worker-search/pull/15))
#### 1.2.1

@@ -5,0 +8,0 @@ Worker `onerror` method properly handles undefined `event.data` for errors during eg `indexDocument`. (This avoids causing a secondary error, as reported in [bvaughn/redux-search/issues/69)](https://github.com/bvaughn/redux-search/issues/69).)

@@ -120,2 +120,6 @@ module.exports =

this.terminate = function () {
_this._search.terminate();
};
// Based on https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

@@ -158,2 +162,7 @@ // But with added check for Node environment

*/
/**
* Stops and retires the worker in the search API. Used for cleanup.
*/
;

@@ -260,2 +269,4 @@

this.terminate = function () {};
this._indexMode = indexMode;

@@ -358,2 +369,10 @@ this._tokenizePattern = tokenizePattern;

/**
* Added to make class adhere to interface. Add cleanup code as needed.
*/
}, {
key: "_expandToken",
/**
* Index strategy based on 'all-substrings-index-strategy.ts' in github.com/bvaughn/js-search/

@@ -363,5 +382,2 @@ *

*/
}, {
key: "_expandToken",
value: function _expandToken(token) {

@@ -663,2 +679,6 @@ switch (this._indexMode) {

this.terminate = function () {
_this._worker.terminate();
};
// Defer worker import until construction to avoid testing error:

@@ -741,2 +761,7 @@ // Error: Cannot find module 'worker!./[workername]'

/**
* Stops and retires the worker. Used for cleanup.
*/
_createClass(SearchWorkerLoader, [{

@@ -1019,3 +1044,3 @@ key: "_updateQueue",

module.exports = function() {
return __webpack_require__(12)("/******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId])\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\texports: {},\n/******/ \t\t\tid: moduleId,\n/******/ \t\t\tloaded: false\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.loaded = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t\"use strict\";\n\t\n\tvar _util = __webpack_require__(1);\n\t\n\t/**\n\t * Search entry point to web worker.\n\t * Builds search index and performs searches on separate thread from the ui.\n\t */\n\t\n\tvar searchUtility = new _util.SearchUtility();\n\t\n\tself.addEventListener(\"message\", function (event) {\n\t var data = event.data;\n\t var method = data.method;\n\t\n\t\n\t switch (method) {\n\t case \"indexDocument\":\n\t var uid = data.uid,\n\t text = data.text;\n\t\n\t\n\t searchUtility.indexDocument(uid, text);\n\t break;\n\t case \"search\":\n\t var callbackId = data.callbackId,\n\t query = data.query;\n\t\n\t\n\t var results = searchUtility.search(query);\n\t\n\t self.postMessage({ callbackId: callbackId, results: results });\n\t break;\n\t case \"setIndexMode\":\n\t var indexMode = data.indexMode;\n\t\n\t\n\t searchUtility.setIndexMode(indexMode);\n\t break;\n\t case \"setTokenizePattern\":\n\t var tokenizePattern = data.tokenizePattern;\n\t\n\t\n\t searchUtility.setTokenizePattern(tokenizePattern);\n\t break;\n\t case \"setCaseSensitive\":\n\t var caseSensitive = data.caseSensitive;\n\t\n\t\n\t searchUtility.setCaseSensitive(caseSensitive);\n\t break;\n\t }\n\t}, false);\n\n/***/ },\n/* 1 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\texports.SearchUtility = exports.INDEX_MODES = undefined;\n\t\n\tvar _SearchUtility = __webpack_require__(2);\n\t\n\tvar _SearchUtility2 = _interopRequireDefault(_SearchUtility);\n\t\n\tvar _constants = __webpack_require__(3);\n\t\n\tfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\t\n\texports.default = _SearchUtility2.default;\n\texports.INDEX_MODES = _constants.INDEX_MODES;\n\texports.SearchUtility = _SearchUtility2.default;\n\n/***/ },\n/* 2 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\t\n\tvar _constants = __webpack_require__(3);\n\t\n\tvar _SearchIndex = __webpack_require__(4);\n\t\n\tvar _SearchIndex2 = _interopRequireDefault(_SearchIndex);\n\t\n\tfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\t\n\tfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\t\n\t/**\n\t * Synchronous client-side full-text search utility.\n\t * Forked from JS search (github.com/bvaughn/js-search).\n\t */\n\tvar SearchUtility = function () {\n\t\n\t /**\n\t * Constructor.\n\t *\n\t * @param indexMode See #setIndexMode\n\t * @param tokenizePattern See #setTokenizePattern\n\t * @param caseSensitive See #setCaseSensitive\n\t */\n\t function SearchUtility() {\n\t var _this = this;\n\t\n\t var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},\n\t _ref$indexMode = _ref.indexMode,\n\t indexMode = _ref$indexMode === undefined ? _constants.INDEX_MODES.ALL_SUBSTRINGS : _ref$indexMode,\n\t _ref$tokenizePattern = _ref.tokenizePattern,\n\t tokenizePattern = _ref$tokenizePattern === undefined ? /\\s+/ : _ref$tokenizePattern,\n\t _ref$caseSensitive = _ref.caseSensitive,\n\t caseSensitive = _ref$caseSensitive === undefined ? false : _ref$caseSensitive;\n\t\n\t _classCallCheck(this, SearchUtility);\n\t\n\t this.indexDocument = function (uid, text) {\n\t _this._uids[uid] = true;\n\t\n\t var fieldTokens = _this._tokenize(_this._sanitize(text));\n\t\n\t fieldTokens.forEach(function (fieldToken) {\n\t var expandedTokens = _this._expandToken(fieldToken);\n\t\n\t expandedTokens.forEach(function (expandedToken) {\n\t _this._searchIndex.indexDocument(expandedToken, uid);\n\t });\n\t });\n\t\n\t return _this;\n\t };\n\t\n\t this.search = function (query) {\n\t if (!query) {\n\t return Object.keys(_this._uids);\n\t } else {\n\t var tokens = _this._tokenize(_this._sanitize(query));\n\t\n\t return _this._searchIndex.search(tokens);\n\t }\n\t };\n\t\n\t this._indexMode = indexMode;\n\t this._tokenizePattern = tokenizePattern;\n\t this._caseSensitive = caseSensitive;\n\t\n\t this._searchIndex = new _SearchIndex2.default();\n\t this._uids = {};\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current index mode.\n\t */\n\t\n\t\n\t _createClass(SearchUtility, [{\n\t key: \"getIndexMode\",\n\t value: function getIndexMode() {\n\t return this._indexMode;\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current tokenize pattern.\n\t */\n\t\n\t }, {\n\t key: \"getTokenizePattern\",\n\t value: function getTokenizePattern() {\n\t return this._tokenizePattern;\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current case-sensitive bit.\n\t */\n\t\n\t }, {\n\t key: \"getCaseSensitive\",\n\t value: function getCaseSensitive() {\n\t return this._caseSensitive;\n\t }\n\t\n\t /**\n\t * Adds or updates a uid in the search index and associates it with the specified text.\n\t * Note that at this time uids can only be added or updated in the index, not removed.\n\t *\n\t * @param uid Uniquely identifies a searchable object\n\t * @param text Text to associate with uid\n\t */\n\t\n\t\n\t /**\n\t * Searches the current index for the specified query text.\n\t * Only uids matching all of the words within the text will be accepted.\n\t * If an empty query string is provided all indexed uids will be returned.\n\t *\n\t * Document searches are case-insensitive (e.g. \"search\" will match \"Search\").\n\t * Document searches use substring matching (e.g. \"na\" and \"me\" will both match \"name\").\n\t *\n\t * @param query Searchable query text\n\t * @return Array of uids\n\t */\n\t\n\t }, {\n\t key: \"setIndexMode\",\n\t\n\t\n\t /**\n\t * Sets a new index mode.\n\t * See util/constants/INDEX_MODES\n\t */\n\t value: function setIndexMode(indexMode) {\n\t if (Object.keys(this._uids).length > 0) {\n\t throw Error(\"indexMode cannot be changed once documents have been indexed\");\n\t }\n\t\n\t this._indexMode = indexMode;\n\t }\n\t\n\t /**\n\t * Sets a new tokenize pattern (regular expression)\n\t */\n\t\n\t }, {\n\t key: \"setTokenizePattern\",\n\t value: function setTokenizePattern(pattern) {\n\t this._tokenizePattern = pattern;\n\t }\n\t\n\t /**\n\t * Sets a new case-sensitive bit\n\t */\n\t\n\t }, {\n\t key: \"setCaseSensitive\",\n\t value: function setCaseSensitive(caseSensitive) {\n\t this._caseSensitive = caseSensitive;\n\t }\n\t\n\t /**\n\t * Index strategy based on 'all-substrings-index-strategy.ts' in github.com/bvaughn/js-search/\n\t *\n\t * @private\n\t */\n\t\n\t }, {\n\t key: \"_expandToken\",\n\t value: function _expandToken(token) {\n\t switch (this._indexMode) {\n\t case _constants.INDEX_MODES.EXACT_WORDS:\n\t return [token];\n\t case _constants.INDEX_MODES.PREFIXES:\n\t return this._expandPrefixTokens(token);\n\t case _constants.INDEX_MODES.ALL_SUBSTRINGS:\n\t default:\n\t return this._expandAllSubstringTokens(token);\n\t }\n\t }\n\t }, {\n\t key: \"_expandAllSubstringTokens\",\n\t value: function _expandAllSubstringTokens(token) {\n\t var expandedTokens = [];\n\t\n\t // String.prototype.charAt() may return surrogate halves instead of whole characters.\n\t // When this happens in the context of a web-worker it can cause Chrome to crash.\n\t // Catching the error is a simple solution for now; in the future I may try to better support non-BMP characters.\n\t // Resources:\n\t // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt\n\t // https://mathiasbynens.be/notes/javascript-unicode\n\t try {\n\t for (var i = 0, length = token.length; i < length; ++i) {\n\t var substring = \"\";\n\t\n\t for (var j = i; j < length; ++j) {\n\t substring += token.charAt(j);\n\t expandedTokens.push(substring);\n\t }\n\t }\n\t } catch (error) {\n\t console.error(\"Unable to parse token \\\"\" + token + \"\\\" \" + error);\n\t }\n\t\n\t return expandedTokens;\n\t }\n\t }, {\n\t key: \"_expandPrefixTokens\",\n\t value: function _expandPrefixTokens(token) {\n\t var expandedTokens = [];\n\t\n\t // String.prototype.charAt() may return surrogate halves instead of whole characters.\n\t // When this happens in the context of a web-worker it can cause Chrome to crash.\n\t // Catching the error is a simple solution for now; in the future I may try to better support non-BMP characters.\n\t // Resources:\n\t // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt\n\t // https://mathiasbynens.be/notes/javascript-unicode\n\t try {\n\t for (var i = 0, length = token.length; i < length; ++i) {\n\t expandedTokens.push(token.substr(0, i + 1));\n\t }\n\t } catch (error) {\n\t console.error(\"Unable to parse token \\\"\" + token + \"\\\" \" + error);\n\t }\n\t\n\t return expandedTokens;\n\t }\n\t\n\t /**\n\t * @private\n\t */\n\t\n\t }, {\n\t key: \"_sanitize\",\n\t value: function _sanitize(string) {\n\t return this._caseSensitive ? string.trim() : string.trim().toLocaleLowerCase();\n\t }\n\t\n\t /**\n\t * @private\n\t */\n\t\n\t }, {\n\t key: \"_tokenize\",\n\t value: function _tokenize(text) {\n\t return text.split(this._tokenizePattern).filter(function (text) {\n\t return text;\n\t }); // Remove empty tokens\n\t }\n\t }]);\n\t\n\t return SearchUtility;\n\t}();\n\t\n\texports.default = SearchUtility;\n\n/***/ },\n/* 3 */\n/***/ function(module, exports) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\tvar INDEX_MODES = exports.INDEX_MODES = {\n\t // Indexes for all substring searches (e.g. the term \"cat\" is indexed as \"c\", \"ca\", \"cat\", \"a\", \"at\", and \"t\").\n\t // Based on 'all-substrings-index-strategy' from js-search;\n\t // github.com/bvaughn/js-search/blob/master/source/index-strategy/all-substrings-index-strategy.ts\n\t ALL_SUBSTRINGS: \"ALL_SUBSTRINGS\",\n\t\n\t // Indexes for exact word matches only.\n\t // Based on 'exact-word-index-strategy' from js-search;\n\t // github.com/bvaughn/js-search/blob/master/source/index-strategy/exact-word-index-strategy.ts\n\t EXACT_WORDS: \"EXACT_WORDS\",\n\t\n\t // Indexes for prefix searches (e.g. the term \"cat\" is indexed as \"c\", \"ca\", and \"cat\" allowing prefix search lookups).\n\t // Based on 'prefix-index-strategy' from js-search;\n\t // github.com/bvaughn/js-search/blob/master/source/index-strategy/prefix-index-strategy.ts\n\t PREFIXES: \"PREFIXES\"\n\t};\n\n/***/ },\n/* 4 */\n/***/ function(module, exports) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\t\n\tfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\t\n\t/**\n\t * Maps search tokens to uids using a trie structure.\n\t */\n\tvar SearchIndex = function () {\n\t function SearchIndex() {\n\t _classCallCheck(this, SearchIndex);\n\t\n\t this.tokenToUidMap = {};\n\t }\n\t\n\t /**\n\t * Maps the specified token to a uid.\n\t *\n\t * @param token Searchable token (e.g. \"road\")\n\t * @param uid Identifies a document within the searchable corpus\n\t */\n\t\n\t\n\t _createClass(SearchIndex, [{\n\t key: \"indexDocument\",\n\t value: function indexDocument(token, uid) {\n\t if (!this.tokenToUidMap[token]) {\n\t this.tokenToUidMap[token] = {};\n\t }\n\t\n\t this.tokenToUidMap[token][uid] = uid;\n\t }\n\t\n\t /**\n\t * Finds uids that have been mapped to the set of tokens specified.\n\t * Only uids that have been mapped to all tokens will be returned.\n\t *\n\t * @param tokens Array of searchable tokens (e.g. [\"long\", \"road\"])\n\t * @return Array of uids that have been associated with the set of search tokens\n\t */\n\t\n\t }, {\n\t key: \"search\",\n\t value: function search(tokens) {\n\t var _this = this;\n\t\n\t var uidMap = {};\n\t var initialized = false;\n\t\n\t tokens.forEach(function (token) {\n\t var currentUidMap = _this.tokenToUidMap[token] || {};\n\t\n\t if (!initialized) {\n\t initialized = true;\n\t\n\t for (var _uid in currentUidMap) {\n\t uidMap[_uid] = currentUidMap[_uid];\n\t }\n\t } else {\n\t for (var _uid2 in uidMap) {\n\t if (!currentUidMap[_uid2]) {\n\t delete uidMap[_uid2];\n\t }\n\t }\n\t }\n\t });\n\t\n\t var uids = [];\n\t for (var _uid3 in uidMap) {\n\t uids.push(uidMap[_uid3]);\n\t }\n\t\n\t return uids;\n\t }\n\t }]);\n\t\n\t return SearchIndex;\n\t}();\n\t\n\texports.default = SearchIndex;\n\n/***/ }\n/******/ ]);\n//# sourceMappingURL=ad4edc03e86dc4df85ea.worker.js.map", __webpack_require__.p + "ad4edc03e86dc4df85ea.worker.js");
return __webpack_require__(12)("/******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId])\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\texports: {},\n/******/ \t\t\tid: moduleId,\n/******/ \t\t\tloaded: false\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.loaded = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t\"use strict\";\n\t\n\tvar _util = __webpack_require__(1);\n\t\n\t/**\n\t * Search entry point to web worker.\n\t * Builds search index and performs searches on separate thread from the ui.\n\t */\n\t\n\tvar searchUtility = new _util.SearchUtility();\n\t\n\tself.addEventListener(\"message\", function (event) {\n\t var data = event.data;\n\t var method = data.method;\n\t\n\t\n\t switch (method) {\n\t case \"indexDocument\":\n\t var uid = data.uid,\n\t text = data.text;\n\t\n\t\n\t searchUtility.indexDocument(uid, text);\n\t break;\n\t case \"search\":\n\t var callbackId = data.callbackId,\n\t query = data.query;\n\t\n\t\n\t var results = searchUtility.search(query);\n\t\n\t self.postMessage({ callbackId: callbackId, results: results });\n\t break;\n\t case \"setIndexMode\":\n\t var indexMode = data.indexMode;\n\t\n\t\n\t searchUtility.setIndexMode(indexMode);\n\t break;\n\t case \"setTokenizePattern\":\n\t var tokenizePattern = data.tokenizePattern;\n\t\n\t\n\t searchUtility.setTokenizePattern(tokenizePattern);\n\t break;\n\t case \"setCaseSensitive\":\n\t var caseSensitive = data.caseSensitive;\n\t\n\t\n\t searchUtility.setCaseSensitive(caseSensitive);\n\t break;\n\t }\n\t}, false);\n\n/***/ },\n/* 1 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\texports.SearchUtility = exports.INDEX_MODES = undefined;\n\t\n\tvar _SearchUtility = __webpack_require__(2);\n\t\n\tvar _SearchUtility2 = _interopRequireDefault(_SearchUtility);\n\t\n\tvar _constants = __webpack_require__(3);\n\t\n\tfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\t\n\texports.default = _SearchUtility2.default;\n\texports.INDEX_MODES = _constants.INDEX_MODES;\n\texports.SearchUtility = _SearchUtility2.default;\n\n/***/ },\n/* 2 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\t\n\tvar _constants = __webpack_require__(3);\n\t\n\tvar _SearchIndex = __webpack_require__(4);\n\t\n\tvar _SearchIndex2 = _interopRequireDefault(_SearchIndex);\n\t\n\tfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\t\n\tfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\t\n\t/**\n\t * Synchronous client-side full-text search utility.\n\t * Forked from JS search (github.com/bvaughn/js-search).\n\t */\n\tvar SearchUtility = function () {\n\t\n\t /**\n\t * Constructor.\n\t *\n\t * @param indexMode See #setIndexMode\n\t * @param tokenizePattern See #setTokenizePattern\n\t * @param caseSensitive See #setCaseSensitive\n\t */\n\t function SearchUtility() {\n\t var _this = this;\n\t\n\t var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},\n\t _ref$indexMode = _ref.indexMode,\n\t indexMode = _ref$indexMode === undefined ? _constants.INDEX_MODES.ALL_SUBSTRINGS : _ref$indexMode,\n\t _ref$tokenizePattern = _ref.tokenizePattern,\n\t tokenizePattern = _ref$tokenizePattern === undefined ? /\\s+/ : _ref$tokenizePattern,\n\t _ref$caseSensitive = _ref.caseSensitive,\n\t caseSensitive = _ref$caseSensitive === undefined ? false : _ref$caseSensitive;\n\t\n\t _classCallCheck(this, SearchUtility);\n\t\n\t this.indexDocument = function (uid, text) {\n\t _this._uids[uid] = true;\n\t\n\t var fieldTokens = _this._tokenize(_this._sanitize(text));\n\t\n\t fieldTokens.forEach(function (fieldToken) {\n\t var expandedTokens = _this._expandToken(fieldToken);\n\t\n\t expandedTokens.forEach(function (expandedToken) {\n\t _this._searchIndex.indexDocument(expandedToken, uid);\n\t });\n\t });\n\t\n\t return _this;\n\t };\n\t\n\t this.search = function (query) {\n\t if (!query) {\n\t return Object.keys(_this._uids);\n\t } else {\n\t var tokens = _this._tokenize(_this._sanitize(query));\n\t\n\t return _this._searchIndex.search(tokens);\n\t }\n\t };\n\t\n\t this.terminate = function () {};\n\t\n\t this._indexMode = indexMode;\n\t this._tokenizePattern = tokenizePattern;\n\t this._caseSensitive = caseSensitive;\n\t\n\t this._searchIndex = new _SearchIndex2.default();\n\t this._uids = {};\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current index mode.\n\t */\n\t\n\t\n\t _createClass(SearchUtility, [{\n\t key: \"getIndexMode\",\n\t value: function getIndexMode() {\n\t return this._indexMode;\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current tokenize pattern.\n\t */\n\t\n\t }, {\n\t key: \"getTokenizePattern\",\n\t value: function getTokenizePattern() {\n\t return this._tokenizePattern;\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current case-sensitive bit.\n\t */\n\t\n\t }, {\n\t key: \"getCaseSensitive\",\n\t value: function getCaseSensitive() {\n\t return this._caseSensitive;\n\t }\n\t\n\t /**\n\t * Adds or updates a uid in the search index and associates it with the specified text.\n\t * Note that at this time uids can only be added or updated in the index, not removed.\n\t *\n\t * @param uid Uniquely identifies a searchable object\n\t * @param text Text to associate with uid\n\t */\n\t\n\t\n\t /**\n\t * Searches the current index for the specified query text.\n\t * Only uids matching all of the words within the text will be accepted.\n\t * If an empty query string is provided all indexed uids will be returned.\n\t *\n\t * Document searches are case-insensitive (e.g. \"search\" will match \"Search\").\n\t * Document searches use substring matching (e.g. \"na\" and \"me\" will both match \"name\").\n\t *\n\t * @param query Searchable query text\n\t * @return Array of uids\n\t */\n\t\n\t }, {\n\t key: \"setIndexMode\",\n\t\n\t\n\t /**\n\t * Sets a new index mode.\n\t * See util/constants/INDEX_MODES\n\t */\n\t value: function setIndexMode(indexMode) {\n\t if (Object.keys(this._uids).length > 0) {\n\t throw Error(\"indexMode cannot be changed once documents have been indexed\");\n\t }\n\t\n\t this._indexMode = indexMode;\n\t }\n\t\n\t /**\n\t * Sets a new tokenize pattern (regular expression)\n\t */\n\t\n\t }, {\n\t key: \"setTokenizePattern\",\n\t value: function setTokenizePattern(pattern) {\n\t this._tokenizePattern = pattern;\n\t }\n\t\n\t /**\n\t * Sets a new case-sensitive bit\n\t */\n\t\n\t }, {\n\t key: \"setCaseSensitive\",\n\t value: function setCaseSensitive(caseSensitive) {\n\t this._caseSensitive = caseSensitive;\n\t }\n\t\n\t /**\n\t * Added to make class adhere to interface. Add cleanup code as needed.\n\t */\n\t\n\t }, {\n\t key: \"_expandToken\",\n\t\n\t\n\t /**\n\t * Index strategy based on 'all-substrings-index-strategy.ts' in github.com/bvaughn/js-search/\n\t *\n\t * @private\n\t */\n\t value: function _expandToken(token) {\n\t switch (this._indexMode) {\n\t case _constants.INDEX_MODES.EXACT_WORDS:\n\t return [token];\n\t case _constants.INDEX_MODES.PREFIXES:\n\t return this._expandPrefixTokens(token);\n\t case _constants.INDEX_MODES.ALL_SUBSTRINGS:\n\t default:\n\t return this._expandAllSubstringTokens(token);\n\t }\n\t }\n\t }, {\n\t key: \"_expandAllSubstringTokens\",\n\t value: function _expandAllSubstringTokens(token) {\n\t var expandedTokens = [];\n\t\n\t // String.prototype.charAt() may return surrogate halves instead of whole characters.\n\t // When this happens in the context of a web-worker it can cause Chrome to crash.\n\t // Catching the error is a simple solution for now; in the future I may try to better support non-BMP characters.\n\t // Resources:\n\t // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt\n\t // https://mathiasbynens.be/notes/javascript-unicode\n\t try {\n\t for (var i = 0, length = token.length; i < length; ++i) {\n\t var substring = \"\";\n\t\n\t for (var j = i; j < length; ++j) {\n\t substring += token.charAt(j);\n\t expandedTokens.push(substring);\n\t }\n\t }\n\t } catch (error) {\n\t console.error(\"Unable to parse token \\\"\" + token + \"\\\" \" + error);\n\t }\n\t\n\t return expandedTokens;\n\t }\n\t }, {\n\t key: \"_expandPrefixTokens\",\n\t value: function _expandPrefixTokens(token) {\n\t var expandedTokens = [];\n\t\n\t // String.prototype.charAt() may return surrogate halves instead of whole characters.\n\t // When this happens in the context of a web-worker it can cause Chrome to crash.\n\t // Catching the error is a simple solution for now; in the future I may try to better support non-BMP characters.\n\t // Resources:\n\t // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt\n\t // https://mathiasbynens.be/notes/javascript-unicode\n\t try {\n\t for (var i = 0, length = token.length; i < length; ++i) {\n\t expandedTokens.push(token.substr(0, i + 1));\n\t }\n\t } catch (error) {\n\t console.error(\"Unable to parse token \\\"\" + token + \"\\\" \" + error);\n\t }\n\t\n\t return expandedTokens;\n\t }\n\t\n\t /**\n\t * @private\n\t */\n\t\n\t }, {\n\t key: \"_sanitize\",\n\t value: function _sanitize(string) {\n\t return this._caseSensitive ? string.trim() : string.trim().toLocaleLowerCase();\n\t }\n\t\n\t /**\n\t * @private\n\t */\n\t\n\t }, {\n\t key: \"_tokenize\",\n\t value: function _tokenize(text) {\n\t return text.split(this._tokenizePattern).filter(function (text) {\n\t return text;\n\t }); // Remove empty tokens\n\t }\n\t }]);\n\t\n\t return SearchUtility;\n\t}();\n\t\n\texports.default = SearchUtility;\n\n/***/ },\n/* 3 */\n/***/ function(module, exports) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\tvar INDEX_MODES = exports.INDEX_MODES = {\n\t // Indexes for all substring searches (e.g. the term \"cat\" is indexed as \"c\", \"ca\", \"cat\", \"a\", \"at\", and \"t\").\n\t // Based on 'all-substrings-index-strategy' from js-search;\n\t // github.com/bvaughn/js-search/blob/master/source/index-strategy/all-substrings-index-strategy.ts\n\t ALL_SUBSTRINGS: \"ALL_SUBSTRINGS\",\n\t\n\t // Indexes for exact word matches only.\n\t // Based on 'exact-word-index-strategy' from js-search;\n\t // github.com/bvaughn/js-search/blob/master/source/index-strategy/exact-word-index-strategy.ts\n\t EXACT_WORDS: \"EXACT_WORDS\",\n\t\n\t // Indexes for prefix searches (e.g. the term \"cat\" is indexed as \"c\", \"ca\", and \"cat\" allowing prefix search lookups).\n\t // Based on 'prefix-index-strategy' from js-search;\n\t // github.com/bvaughn/js-search/blob/master/source/index-strategy/prefix-index-strategy.ts\n\t PREFIXES: \"PREFIXES\"\n\t};\n\n/***/ },\n/* 4 */\n/***/ function(module, exports) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\t\n\tfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\t\n\t/**\n\t * Maps search tokens to uids using a trie structure.\n\t */\n\tvar SearchIndex = function () {\n\t function SearchIndex() {\n\t _classCallCheck(this, SearchIndex);\n\t\n\t this.tokenToUidMap = {};\n\t }\n\t\n\t /**\n\t * Maps the specified token to a uid.\n\t *\n\t * @param token Searchable token (e.g. \"road\")\n\t * @param uid Identifies a document within the searchable corpus\n\t */\n\t\n\t\n\t _createClass(SearchIndex, [{\n\t key: \"indexDocument\",\n\t value: function indexDocument(token, uid) {\n\t if (!this.tokenToUidMap[token]) {\n\t this.tokenToUidMap[token] = {};\n\t }\n\t\n\t this.tokenToUidMap[token][uid] = uid;\n\t }\n\t\n\t /**\n\t * Finds uids that have been mapped to the set of tokens specified.\n\t * Only uids that have been mapped to all tokens will be returned.\n\t *\n\t * @param tokens Array of searchable tokens (e.g. [\"long\", \"road\"])\n\t * @return Array of uids that have been associated with the set of search tokens\n\t */\n\t\n\t }, {\n\t key: \"search\",\n\t value: function search(tokens) {\n\t var _this = this;\n\t\n\t var uidMap = {};\n\t var initialized = false;\n\t\n\t tokens.forEach(function (token) {\n\t var currentUidMap = _this.tokenToUidMap[token] || {};\n\t\n\t if (!initialized) {\n\t initialized = true;\n\t\n\t for (var _uid in currentUidMap) {\n\t uidMap[_uid] = currentUidMap[_uid];\n\t }\n\t } else {\n\t for (var _uid2 in uidMap) {\n\t if (!currentUidMap[_uid2]) {\n\t delete uidMap[_uid2];\n\t }\n\t }\n\t }\n\t });\n\t\n\t var uids = [];\n\t for (var _uid3 in uidMap) {\n\t uids.push(uidMap[_uid3]);\n\t }\n\t\n\t return uids;\n\t }\n\t }]);\n\t\n\t return SearchIndex;\n\t}();\n\t\n\texports.default = SearchIndex;\n\n/***/ }\n/******/ ]);\n//# sourceMappingURL=2ef0afcacb6f46b34be5.worker.js.map", __webpack_require__.p + "2ef0afcacb6f46b34be5.worker.js");
};

@@ -1022,0 +1047,0 @@

14

package.json
{
"name": "js-worker-search",
"version": "1.2.1",
"version": "1.3.0",
"description": "JavaScript client-side search API with web-worker support",

@@ -22,3 +22,3 @@ "author": "Brian Vaughn (brian.david.vaughn@gmail.com)",

"start": "watch 'clear && yarn run lint -s && yarn run test -s' src",
"test": "babel-node node_modules/.bin/tape 'src/**/*.test.js' | tspec"
"test": "jest"
},

@@ -44,3 +44,7 @@ "keywords": [

"global": [
"self"
"describe",
"expect",
"it",
"self",
"test"
]

@@ -52,2 +56,3 @@ },

"babel-eslint": "^7.2.3",
"babel-jest": "^22.1.0",
"babel-loader": "^6.2.0",

@@ -64,6 +69,5 @@ "babel-plugin-syntax-flow": "^6.3.13",

"immutable": "^3.8.1",
"jest": "^22.1.1",
"prettier": "^1.5.3",
"rimraf": "^2.5.0",
"tap-spec": "^4.1.1",
"tape": "^4.4.0",
"watch": "^1.0.2",

@@ -70,0 +74,0 @@ "webpack": "^1.12.9",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc