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.3.0 to 1.4.0

dist/5cafaba60d6eb1f43c8f.worker.js

3

CHANGELOG.md
Changelog
-----
#### 1.4.0
* Added support for optional OR searches (documents matching only some of the search tokens) ([@ dlebech](https://github.com/ dlebech) - [#19](https://github.com/bvaughn/js-worker-search/pull/19))
#### 1.3.0

@@ -5,0 +8,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))

144

dist/js-worker-search.js

@@ -103,5 +103,6 @@ module.exports =

var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
caseSensitive = _ref.caseSensitive,
indexMode = _ref.indexMode,
tokenizePattern = _ref.tokenizePattern,
caseSensitive = _ref.caseSensitive;
matchAnyToken = _ref.matchAnyToken,
tokenizePattern = _ref.tokenizePattern;

@@ -130,2 +131,3 @@ _classCallCheck(this, SearchApi);

indexMode: indexMode,
matchAnyToken: matchAnyToken,
tokenizePattern: tokenizePattern,

@@ -137,2 +139,3 @@ caseSensitive: caseSensitive

indexMode: indexMode,
matchAnyToken: matchAnyToken,
tokenizePattern: tokenizePattern,

@@ -230,2 +233,3 @@ caseSensitive: caseSensitive

* @param caseSensitive See #setCaseSensitive
* @param matchAnyToken See #setMatchAnyToken
*/

@@ -236,8 +240,10 @@ function SearchUtility() {

var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$caseSensitive = _ref.caseSensitive,
caseSensitive = _ref$caseSensitive === undefined ? false : _ref$caseSensitive,
_ref$indexMode = _ref.indexMode,
indexMode = _ref$indexMode === undefined ? _constants.INDEX_MODES.ALL_SUBSTRINGS : _ref$indexMode,
_ref$matchAnyToken = _ref.matchAnyToken,
matchAnyToken = _ref$matchAnyToken === undefined ? false : _ref$matchAnyToken,
_ref$tokenizePattern = _ref.tokenizePattern,
tokenizePattern = _ref$tokenizePattern === undefined ? /\s+/ : _ref$tokenizePattern,
_ref$caseSensitive = _ref.caseSensitive,
caseSensitive = _ref$caseSensitive === undefined ? false : _ref$caseSensitive;
tokenizePattern = _ref$tokenizePattern === undefined ? /\s+/ : _ref$tokenizePattern;

@@ -268,3 +274,3 @@ _classCallCheck(this, SearchUtility);

return _this._searchIndex.search(tokens);
return _this._searchIndex.search(tokens, _this._matchAnyToken);
}

@@ -275,5 +281,6 @@ };

this._caseSensitive = caseSensitive;
this._indexMode = indexMode;
this._matchAnyToken = matchAnyToken;
this._tokenizePattern = tokenizePattern;
this._caseSensitive = caseSensitive;

@@ -285,3 +292,3 @@ this._searchIndex = new _SearchIndex2.default();

/**
* Returns a constant representing the current index mode.
* Returns a constant representing the current case-sensitive bit.
*/

@@ -291,2 +298,12 @@

_createClass(SearchUtility, [{
key: "getCaseSensitive",
value: function getCaseSensitive() {
return this._caseSensitive;
}
/**
* Returns a constant representing the current index mode.
*/
}, {
key: "getIndexMode",

@@ -298,19 +315,19 @@ value: function getIndexMode() {

/**
* Returns a constant representing the current tokenize pattern.
* Returns a constant representing the current match-any-token bit.
*/
}, {
key: "getTokenizePattern",
value: function getTokenizePattern() {
return this._tokenizePattern;
key: "getMatchAnyToken",
value: function getMatchAnyToken() {
return this._matchAnyToken;
}
/**
* Returns a constant representing the current case-sensitive bit.
* Returns a constant representing the current tokenize pattern.
*/
}, {
key: "getCaseSensitive",
value: function getCaseSensitive() {
return this._caseSensitive;
key: "getTokenizePattern",
value: function getTokenizePattern() {
return this._tokenizePattern;
}

@@ -329,7 +346,8 @@

* Searches the current index for the specified query text.
* Only uids matching all of the words within the text will be accepted.
* Only uids matching all of the words within the text will be accepted,
* unless matchAny is set to true.
* If an empty query string is provided all indexed uids will be returned.
*
* Document searches are case-insensitive (e.g. "search" will match "Search").
* Document searches use substring matching (e.g. "na" and "me" will both match "name").
* Document searches are case-insensitive by default (e.g. "search" will match "Search").
* Document searches use substring matching by default (e.g. "na" and "me" will both match "name").
*

@@ -341,9 +359,19 @@ * @param query Searchable query text

}, {
key: "setIndexMode",
key: "setCaseSensitive",
/**
* Sets a new case-sensitive bit
*/
value: function setCaseSensitive(caseSensitive) {
this._caseSensitive = caseSensitive;
}
/**
* Sets a new index mode.
* See util/constants/INDEX_MODES
*/
}, {
key: "setIndexMode",
value: function setIndexMode(indexMode) {

@@ -358,19 +386,19 @@ if (Object.keys(this._uids).length > 0) {

/**
* Sets a new tokenize pattern (regular expression)
* Sets a new match-any-token bit
*/
}, {
key: "setTokenizePattern",
value: function setTokenizePattern(pattern) {
this._tokenizePattern = pattern;
key: "setMatchAnyToken",
value: function setMatchAnyToken(matchAnyToken) {
this._matchAnyToken = matchAnyToken;
}
/**
* Sets a new case-sensitive bit
* Sets a new tokenize pattern (regular expression)
*/
}, {
key: "setCaseSensitive",
value: function setCaseSensitive(caseSensitive) {
this._caseSensitive = caseSensitive;
key: "setTokenizePattern",
value: function setTokenizePattern(pattern) {
this._tokenizePattern = pattern;
}

@@ -551,2 +579,3 @@

* @param tokens Array of searchable tokens (e.g. ["long", "road"])
* @param matchAnyToken Whether to match any token. Default is false.
* @return Array of uids that have been associated with the set of search tokens

@@ -557,6 +586,7 @@ */

key: "search",
value: function search(tokens) {
value: function search(tokens, matchAnyToken) {
var _this = this;
var uidMap = {};
var uidMatches = {};
var initialized = false;

@@ -572,8 +602,18 @@

uidMap[_uid] = currentUidMap[_uid];
uidMatches[_uid] = 1;
}
} else {
for (var _uid2 in uidMap) {
if (!currentUidMap[_uid2]) {
delete uidMap[_uid2];
// Delete existing matches if using and AND query (the default)
// Otherwise add new results to the matches
if (!matchAnyToken) {
for (var _uid2 in uidMap) {
if (!currentUidMap[_uid2]) {
delete uidMap[_uid2];
}
}
} else {
for (var _uid3 in currentUidMap) {
uidMap[_uid3] = currentUidMap[_uid3];
uidMatches[_uid3] = (uidMatches[_uid3] || 0) + 1;
}
}

@@ -584,6 +624,13 @@ }

var uids = [];
for (var _uid3 in uidMap) {
uids.push(uidMap[_uid3]);
for (var _uid4 in uidMap) {
uids.push(uidMap[_uid4]);
}
// Sort according to most matches, if match any token is set.
if (matchAnyToken) {
uids.sort(function (a, b) {
return uidMatches[b] - uidMatches[a];
});
}
return uids;

@@ -651,5 +698,6 @@ }

var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
caseSensitive = _ref.caseSensitive,
indexMode = _ref.indexMode,
matchAnyToken = _ref.matchAnyToken,
tokenizePattern = _ref.tokenizePattern,
caseSensitive = _ref.caseSensitive,
WorkerClass = _ref.WorkerClass;

@@ -726,2 +774,10 @@

// Override default :caseSensitive bit if a specific one has been requested
if (caseSensitive) {
this._worker.postMessage({
method: "setCaseSensitive",
caseSensitive: caseSensitive
});
}
// Override default :indexMode if a specific one has been requested

@@ -735,2 +791,10 @@ if (indexMode) {

// Override default :matchAnyToken bit if a specific one has been requested
if (matchAnyToken) {
this._worker.postMessage({
method: "setMatchAnyToken",
matchAnyToken: matchAnyToken
});
}
// Override default :tokenizePattern if a specific one has been requested

@@ -743,10 +807,2 @@ if (tokenizePattern) {

}
// Override default :caseSensitive bit if a specific one has been requested
if (caseSensitive) {
this._worker.postMessage({
method: "setCaseSensitive",
caseSensitive: caseSensitive
});
}
}

@@ -1058,3 +1114,3 @@

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.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");
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 \"setCaseSensitive\":\n\t var caseSensitive = data.caseSensitive;\n\t\n\t\n\t searchUtility.setCaseSensitive(caseSensitive);\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 \"setMatchAnyToken\":\n\t var matchAnyToken = data.matchAnyToken;\n\t\n\t\n\t searchUtility.setMatchAnyToken(matchAnyToken);\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 }\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 * @param matchAnyToken See #setMatchAnyToken\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$caseSensitive = _ref.caseSensitive,\n\t caseSensitive = _ref$caseSensitive === undefined ? false : _ref$caseSensitive,\n\t _ref$indexMode = _ref.indexMode,\n\t indexMode = _ref$indexMode === undefined ? _constants.INDEX_MODES.ALL_SUBSTRINGS : _ref$indexMode,\n\t _ref$matchAnyToken = _ref.matchAnyToken,\n\t matchAnyToken = _ref$matchAnyToken === undefined ? false : _ref$matchAnyToken,\n\t _ref$tokenizePattern = _ref.tokenizePattern,\n\t tokenizePattern = _ref$tokenizePattern === undefined ? /\\s+/ : _ref$tokenizePattern;\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, _this._matchAnyToken);\n\t }\n\t };\n\t\n\t this.terminate = function () {};\n\t\n\t this._caseSensitive = caseSensitive;\n\t this._indexMode = indexMode;\n\t this._matchAnyToken = matchAnyToken;\n\t this._tokenizePattern = tokenizePattern;\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 case-sensitive bit.\n\t */\n\t\n\t\n\t _createClass(SearchUtility, [{\n\t key: \"getCaseSensitive\",\n\t value: function getCaseSensitive() {\n\t return this._caseSensitive;\n\t }\n\t\n\t /**\n\t * Returns a constant representing the current index mode.\n\t */\n\t\n\t }, {\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 match-any-token bit.\n\t */\n\t\n\t }, {\n\t key: \"getMatchAnyToken\",\n\t value: function getMatchAnyToken() {\n\t return this._matchAnyToken;\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 * 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 * unless matchAny is set to true.\n\t * If an empty query string is provided all indexed uids will be returned.\n\t *\n\t * Document searches are case-insensitive by default (e.g. \"search\" will match \"Search\").\n\t * Document searches use substring matching by default (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: \"setCaseSensitive\",\n\t\n\t\n\t /**\n\t * Sets a new case-sensitive bit\n\t */\n\t value: function setCaseSensitive(caseSensitive) {\n\t this._caseSensitive = caseSensitive;\n\t }\n\t\n\t /**\n\t * Sets a new index mode.\n\t * See util/constants/INDEX_MODES\n\t */\n\t\n\t }, {\n\t key: \"setIndexMode\",\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 match-any-token bit\n\t */\n\t\n\t }, {\n\t key: \"setMatchAnyToken\",\n\t value: function setMatchAnyToken(matchAnyToken) {\n\t this._matchAnyToken = matchAnyToken;\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 * 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 * @param matchAnyToken Whether to match any token. Default is false.\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, matchAnyToken) {\n\t var _this = this;\n\t\n\t var uidMap = {};\n\t var uidMatches = {};\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 uidMatches[_uid] = 1;\n\t }\n\t } else {\n\t // Delete existing matches if using and AND query (the default)\n\t // Otherwise add new results to the matches\n\t if (!matchAnyToken) {\n\t for (var _uid2 in uidMap) {\n\t if (!currentUidMap[_uid2]) {\n\t delete uidMap[_uid2];\n\t }\n\t }\n\t } else {\n\t for (var _uid3 in currentUidMap) {\n\t uidMap[_uid3] = currentUidMap[_uid3];\n\t uidMatches[_uid3] = (uidMatches[_uid3] || 0) + 1;\n\t }\n\t }\n\t }\n\t });\n\t\n\t var uids = [];\n\t for (var _uid4 in uidMap) {\n\t uids.push(uidMap[_uid4]);\n\t }\n\t\n\t // Sort according to most matches, if match any token is set.\n\t if (matchAnyToken) {\n\t uids.sort(function (a, b) {\n\t return uidMatches[b] - uidMatches[a];\n\t });\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=5cafaba60d6eb1f43c8f.worker.js.map", __webpack_require__.p + "5cafaba60d6eb1f43c8f.worker.js");
};

@@ -1061,0 +1117,0 @@

{
"name": "js-worker-search",
"version": "1.3.0",
"version": "1.4.0",
"description": "JavaScript client-side search API with web-worker support",

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

@@ -50,2 +50,5 @@ js-worker-search

##### `terminate()`
If search is running in a web worker, this will terminate the worker to allow for garbage collection.
Example Usage

@@ -120,2 +123,13 @@ ------

### Partial matches
By default, the search utility only returns documents containing every search token.
It can be configured to return documents containing any search token.
```js
// Change search behavior from AND to OR
const searchApi = new SearchApi({
matchAnyToken: true
})
```
Changelog

@@ -122,0 +136,0 @@ ---------

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