gab-social/app/javascript/gabsocial/utils/cursor_token_match.js
2019-08-03 01:51:48 -04:00

26 lines
562 B
JavaScript

'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];
};