searcher.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS104: Avoid inline assignments
  8. * DS202: Simplify dynamic range loops
  9. * DS206: Consider reworking classes to avoid initClass
  10. * DS207: Consider shorter variations of null checks
  11. * DS209: Avoid top-level return
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  13. */
  14. //
  15. // Match functions
  16. //
  17. let fuzzyRegexp,
  18. i,
  19. index,
  20. lastIndex,
  21. match,
  22. matcher,
  23. matchIndex,
  24. matchLength,
  25. queryLength,
  26. score,
  27. separators,
  28. value,
  29. valueLength;
  30. const SEPARATOR = ".";
  31. let query =
  32. (queryLength =
  33. value =
  34. valueLength =
  35. matcher = // current match function
  36. fuzzyRegexp = // query fuzzy regexp
  37. index = // position of the query in the string being matched
  38. lastIndex = // last position of the query in the string being matched
  39. match = // regexp match data
  40. matchIndex =
  41. matchLength =
  42. score = // score for the current match
  43. separators = // counter
  44. i =
  45. null); // cursor
  46. function exactMatch() {
  47. index = value.indexOf(query);
  48. if (!(index >= 0)) {
  49. return;
  50. }
  51. lastIndex = value.lastIndexOf(query);
  52. if (index !== lastIndex) {
  53. return Math.max(
  54. scoreExactMatch(),
  55. ((index = lastIndex) && scoreExactMatch()) || 0,
  56. );
  57. } else {
  58. return scoreExactMatch();
  59. }
  60. }
  61. function scoreExactMatch() {
  62. // Remove one point for each unmatched character.
  63. score = 100 - (valueLength - queryLength);
  64. if (index > 0) {
  65. // If the character preceding the query is a dot, assign the same score
  66. // as if the query was found at the beginning of the string, minus one.
  67. if (value.charAt(index - 1) === SEPARATOR) {
  68. score += index - 1;
  69. // Don't match a single-character query unless it's found at the beginning
  70. // of the string or is preceded by a dot.
  71. } else if (queryLength === 1) {
  72. return;
  73. // (1) Remove one point for each unmatched character up to the nearest
  74. // preceding dot or the beginning of the string.
  75. // (2) Remove one point for each unmatched character following the query.
  76. } else {
  77. i = index - 2;
  78. while (i >= 0 && value.charAt(i) !== SEPARATOR) {
  79. i--;
  80. }
  81. score -=
  82. index -
  83. i + // (1)
  84. (valueLength - queryLength - index); // (2)
  85. }
  86. // Remove one point for each dot preceding the query, except for the one
  87. // immediately before the query.
  88. separators = 0;
  89. i = index - 2;
  90. while (i >= 0) {
  91. if (value.charAt(i) === SEPARATOR) {
  92. separators++;
  93. }
  94. i--;
  95. }
  96. score -= separators;
  97. }
  98. // Remove five points for each dot following the query.
  99. separators = 0;
  100. i = valueLength - queryLength - index - 1;
  101. while (i >= 0) {
  102. if (value.charAt(index + queryLength + i) === SEPARATOR) {
  103. separators++;
  104. }
  105. i--;
  106. }
  107. score -= separators * 5;
  108. return Math.max(1, score);
  109. }
  110. function fuzzyMatch() {
  111. if (valueLength <= queryLength || value.indexOf(query) >= 0) {
  112. return;
  113. }
  114. if (!(match = fuzzyRegexp.exec(value))) {
  115. return;
  116. }
  117. matchIndex = match.index;
  118. matchLength = match[0].length;
  119. score = scoreFuzzyMatch();
  120. if (
  121. (match = fuzzyRegexp.exec(
  122. value.slice((i = value.lastIndexOf(SEPARATOR) + 1)),
  123. ))
  124. ) {
  125. matchIndex = i + match.index;
  126. matchLength = match[0].length;
  127. return Math.max(score, scoreFuzzyMatch());
  128. } else {
  129. return score;
  130. }
  131. }
  132. function scoreFuzzyMatch() {
  133. // When the match is at the beginning of the string or preceded by a dot.
  134. if (matchIndex === 0 || value.charAt(matchIndex - 1) === SEPARATOR) {
  135. return Math.max(66, 100 - matchLength);
  136. // When the match is at the end of the string.
  137. } else if (matchIndex + matchLength === valueLength) {
  138. return Math.max(33, 67 - matchLength);
  139. // When the match is in the middle of the string.
  140. } else {
  141. return Math.max(1, 34 - matchLength);
  142. }
  143. }
  144. //
  145. // Searchers
  146. //
  147. (function () {
  148. let CHUNK_SIZE = undefined;
  149. let DEFAULTS = undefined;
  150. let SEPARATORS_REGEXP = undefined;
  151. let EOS_SEPARATORS_REGEXP = undefined;
  152. let INFO_PARANTHESES_REGEXP = undefined;
  153. let EMPTY_PARANTHESES_REGEXP = undefined;
  154. let EVENT_REGEXP = undefined;
  155. let DOT_REGEXP = undefined;
  156. let WHITESPACE_REGEXP = undefined;
  157. let EMPTY_STRING = undefined;
  158. let ELLIPSIS = undefined;
  159. let STRING = undefined;
  160. app.Searcher = class Searcher extends Events {
  161. static initClass() {
  162. CHUNK_SIZE = 20000;
  163. DEFAULTS = {
  164. max_results: app.config.max_results,
  165. fuzzy_min_length: 3,
  166. };
  167. SEPARATORS_REGEXP =
  168. /#|::|:-|->|\$(?=\w)|\-(?=\w)|\:(?=\w)|\ [\/\-&]\ |:\ |\ /g;
  169. EOS_SEPARATORS_REGEXP = /(\w)[\-:]$/;
  170. INFO_PARANTHESES_REGEXP = /\ \(\w+?\)$/;
  171. EMPTY_PARANTHESES_REGEXP = /\(\)/;
  172. EVENT_REGEXP = /\ event$/;
  173. DOT_REGEXP = /\.+/g;
  174. WHITESPACE_REGEXP = /\s/g;
  175. EMPTY_STRING = "";
  176. ELLIPSIS = "...";
  177. STRING = "string";
  178. }
  179. static normalizeString(string) {
  180. return string
  181. .toLowerCase()
  182. .replace(ELLIPSIS, EMPTY_STRING)
  183. .replace(EVENT_REGEXP, EMPTY_STRING)
  184. .replace(INFO_PARANTHESES_REGEXP, EMPTY_STRING)
  185. .replace(SEPARATORS_REGEXP, SEPARATOR)
  186. .replace(DOT_REGEXP, SEPARATOR)
  187. .replace(EMPTY_PARANTHESES_REGEXP, EMPTY_STRING)
  188. .replace(WHITESPACE_REGEXP, EMPTY_STRING);
  189. }
  190. static normalizeQuery(string) {
  191. string = this.normalizeString(string);
  192. return string.replace(EOS_SEPARATORS_REGEXP, "$1.");
  193. }
  194. constructor(options) {
  195. super();
  196. this.options = $.extend({}, DEFAULTS, options || {});
  197. }
  198. find(data, attr, q) {
  199. this.kill();
  200. this.data = data;
  201. this.attr = attr;
  202. this.query = q;
  203. this.setup();
  204. if (this.isValid()) {
  205. this.match();
  206. } else {
  207. this.end();
  208. }
  209. }
  210. setup() {
  211. query = this.query = this.constructor.normalizeQuery(this.query);
  212. queryLength = query.length;
  213. this.dataLength = this.data.length;
  214. this.matchers = [exactMatch];
  215. this.totalResults = 0;
  216. this.setupFuzzy();
  217. }
  218. setupFuzzy() {
  219. if (queryLength >= this.options.fuzzy_min_length) {
  220. fuzzyRegexp = this.queryToFuzzyRegexp(query);
  221. this.matchers.push(fuzzyMatch);
  222. } else {
  223. fuzzyRegexp = null;
  224. }
  225. }
  226. isValid() {
  227. return queryLength > 0 && query !== SEPARATOR;
  228. }
  229. end() {
  230. if (!this.totalResults) {
  231. this.triggerResults([]);
  232. }
  233. this.trigger("end");
  234. this.free();
  235. }
  236. kill() {
  237. if (this.timeout) {
  238. clearTimeout(this.timeout);
  239. this.free();
  240. }
  241. }
  242. free() {
  243. this.data =
  244. this.attr =
  245. this.dataLength =
  246. this.matchers =
  247. this.matcher =
  248. this.query =
  249. this.totalResults =
  250. this.scoreMap =
  251. this.cursor =
  252. this.timeout =
  253. null;
  254. }
  255. match() {
  256. if (!this.foundEnough() && (this.matcher = this.matchers.shift())) {
  257. this.setupMatcher();
  258. this.matchChunks();
  259. } else {
  260. this.end();
  261. }
  262. }
  263. setupMatcher() {
  264. this.cursor = 0;
  265. this.scoreMap = new Array(101);
  266. }
  267. matchChunks() {
  268. this.matchChunk();
  269. if (this.cursor === this.dataLength || this.scoredEnough()) {
  270. this.delay(() => this.match());
  271. this.sendResults();
  272. } else {
  273. this.delay(() => this.matchChunks());
  274. }
  275. }
  276. matchChunk() {
  277. ({ matcher } = this);
  278. for (
  279. let j = 0, end = this.chunkSize(), asc = 0 <= end;
  280. asc ? j < end : j > end;
  281. asc ? j++ : j--
  282. ) {
  283. value = this.data[this.cursor][this.attr];
  284. if (value.split) {
  285. // string
  286. valueLength = value.length;
  287. if ((score = matcher())) {
  288. this.addResult(this.data[this.cursor], score);
  289. }
  290. } else {
  291. // array
  292. score = 0;
  293. for (value of Array.from(this.data[this.cursor][this.attr])) {
  294. valueLength = value.length;
  295. score = Math.max(score, matcher() || 0);
  296. }
  297. if (score > 0) {
  298. this.addResult(this.data[this.cursor], score);
  299. }
  300. }
  301. this.cursor++;
  302. }
  303. }
  304. chunkSize() {
  305. if (this.cursor + CHUNK_SIZE > this.dataLength) {
  306. return this.dataLength % CHUNK_SIZE;
  307. } else {
  308. return CHUNK_SIZE;
  309. }
  310. }
  311. scoredEnough() {
  312. return (
  313. (this.scoreMap[100] != null ? this.scoreMap[100].length : undefined) >=
  314. this.options.max_results
  315. );
  316. }
  317. foundEnough() {
  318. return this.totalResults >= this.options.max_results;
  319. }
  320. addResult(object, score) {
  321. let name;
  322. (
  323. this.scoreMap[(name = Math.round(score))] || (this.scoreMap[name] = [])
  324. ).push(object);
  325. this.totalResults++;
  326. }
  327. getResults() {
  328. const results = [];
  329. for (let j = this.scoreMap.length - 1; j >= 0; j--) {
  330. var objects = this.scoreMap[j];
  331. if (objects) {
  332. results.push.apply(results, objects);
  333. }
  334. }
  335. return results.slice(0, this.options.max_results);
  336. }
  337. sendResults() {
  338. const results = this.getResults();
  339. if (results.length) {
  340. this.triggerResults(results);
  341. }
  342. }
  343. triggerResults(results) {
  344. this.trigger("results", results);
  345. }
  346. delay(fn) {
  347. return (this.timeout = setTimeout(fn, 1));
  348. }
  349. queryToFuzzyRegexp(string) {
  350. const chars = string.split("");
  351. for (i = 0; i < chars.length; i++) {
  352. var char = chars[i];
  353. chars[i] = $.escapeRegexp(char);
  354. }
  355. return new RegExp(chars.join(".*?")); // abc -> /a.*?b.*?c.*?/
  356. }
  357. };
  358. app.Searcher.initClass();
  359. return app.Searcher;
  360. })();
  361. app.SynchronousSearcher = class SynchronousSearcher extends app.Searcher {
  362. match() {
  363. if (this.matcher) {
  364. if (!this.allResults) {
  365. this.allResults = [];
  366. }
  367. this.allResults.push.apply(this.allResults, this.getResults());
  368. }
  369. return super.match(...arguments);
  370. }
  371. free() {
  372. this.allResults = null;
  373. return super.free(...arguments);
  374. }
  375. end() {
  376. this.sendResults(true);
  377. return super.end(...arguments);
  378. }
  379. sendResults(end) {
  380. if (end && (this.allResults != null ? this.allResults.length : undefined)) {
  381. return this.triggerResults(this.allResults);
  382. }
  383. }
  384. delay(fn) {
  385. return fn();
  386. }
  387. };