gab-social/app/javascript/gabsocial/utils/cursor_token_match.js
2020-05-04 14:44:37 -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 < 2 || searchTokens.indexOf(word[0]) === -1) {
return [null, null];
}
word = word.trim().toLowerCase();
if (word.length > 0) {
return [left + 1, word];
}
return [null, null];
};