Added cursor_token_match util file

This commit is contained in:
mgabdev 2019-08-03 01:51:48 -04:00
parent 2044648179
commit 16a9bc6e93

View File

@ -0,0 +1,26 @@
'use strict';
export function textAtCursorMatchesToken(str, caretPosition, searchTokens) {
let word;
let left = str.slice(0, caretPosition).search(/\S+$/);
let right = str.slice(caretPosition).search(/\s/);
if (right < 0) {
word = str.slice(left);
} else {
word = str.slice(left, right + caretPosition);
}
if (!word || word.trim().length < 3 || searchTokens.indexOf(word[0]) === -1) {
return [null, null];
}
word = word.trim().toLowerCase();
if (word.length > 0) {
return [left + 1, word];
}
return [null, null];
};