UNPKG

3.64 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2export var omit = function omit(obj) {
3 var clone = _objectSpread({}, obj);
4
5 for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
6 keys[_key - 1] = arguments[_key];
7 }
8
9 keys.forEach(function (key) {
10 delete clone[key];
11 });
12 return clone;
13};
14/**
15 * Cut input selection into 2 part and return text before selection start
16 */
17
18export function getBeforeSelectionText(input) {
19 var selectionStart = input.selectionStart;
20 return input.value.slice(0, selectionStart);
21}
22/**
23 * Find the last match prefix index
24 */
25
26export function getLastMeasureIndex(text) {
27 var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
28 var prefixList = Array.isArray(prefix) ? prefix : [prefix];
29 return prefixList.reduce(function (lastMatch, prefixStr) {
30 var lastIndex = text.lastIndexOf(prefixStr);
31
32 if (lastIndex > lastMatch.location) {
33 return {
34 location: lastIndex,
35 prefix: prefixStr
36 };
37 }
38
39 return lastMatch;
40 }, {
41 location: -1,
42 prefix: ''
43 });
44}
45
46function lower(char) {
47 return (char || '').toLowerCase();
48}
49
50function reduceText(text, targetText, split) {
51 var firstChar = text[0];
52
53 if (!firstChar || firstChar === split) {
54 return text;
55 } // Reuse rest text as it can
56
57
58 var restText = text;
59 var targetTextLen = targetText.length;
60
61 for (var i = 0; i < targetTextLen; i += 1) {
62 if (lower(restText[i]) !== lower(targetText[i])) {
63 restText = restText.slice(i);
64 break;
65 } else if (i === targetTextLen - 1) {
66 restText = restText.slice(targetTextLen);
67 }
68 }
69
70 return restText;
71}
72/**
73 * Paint targetText into current text:
74 * text: little@litest
75 * targetText: light
76 * => little @light test
77 */
78
79
80export function replaceWithMeasure(text, measureConfig) {
81 var measureLocation = measureConfig.measureLocation,
82 prefix = measureConfig.prefix,
83 targetText = measureConfig.targetText,
84 selectionStart = measureConfig.selectionStart,
85 split = measureConfig.split; // Before text will append one space if have other text
86
87 var beforeMeasureText = text.slice(0, measureLocation);
88
89 if (beforeMeasureText[beforeMeasureText.length - split.length] === split) {
90 beforeMeasureText = beforeMeasureText.slice(0, beforeMeasureText.length - split.length);
91 }
92
93 if (beforeMeasureText) {
94 beforeMeasureText = "".concat(beforeMeasureText).concat(split);
95 } // Cut duplicate string with current targetText
96
97
98 var restText = reduceText(text.slice(selectionStart), targetText.slice(selectionStart - measureLocation - prefix.length), split);
99
100 if (restText.slice(0, split.length) === split) {
101 restText = restText.slice(split.length);
102 }
103
104 var connectedStartText = "".concat(beforeMeasureText).concat(prefix).concat(targetText).concat(split);
105 return {
106 text: "".concat(connectedStartText).concat(restText),
107 selectionLocation: connectedStartText.length
108 };
109}
110export function setInputSelection(input, location) {
111 input.setSelectionRange(location, location);
112 /**
113 * Reset caret into view.
114 * Since this function always called by user control, it's safe to focus element.
115 */
116
117 input.blur();
118 input.focus();
119}
120export function validateSearch(text, props) {
121 var split = props.split;
122 return !split || text.indexOf(split) === -1;
123}
124export function filterOption(input, _ref) {
125 var _ref$value = _ref.value,
126 value = _ref$value === void 0 ? '' : _ref$value;
127 var lowerCase = input.toLowerCase();
128 return value.toLowerCase().indexOf(lowerCase) !== -1;
129}
\No newline at end of file