Best JavaScript code snippet using best
rmkj.search.js
Source:rmkj.search.js
1/**2 * Created by vken on 14-3-10.3 */4/**5 * æç´¢6 * @param keyword7 * @param classId,é«äº®çcsså称8 */9const SEARCH_SPAN_ID_PREFIX = 'search_';10const SEARCH_SPAN_NAME = 'search_span';11const ELEMENT_NODE_TYPE = 1;12const TEXT_NODE_TYPE = 3;13function string2Json(jsonString) {14 var json = eval("(" + jsonString + ")");15 return json;16}17function highlightSearch(key, classId) {18 var bodyElement = document.body;19 var childNodes = bodyElement.childNodes;20 var i = 0;21 for (i = 0; i < childNodes.length; i++) {22 var node = childNodes[i];23 var childText = node.textContent;24 var keyIndex = childText.indexOf(key);25 while (keyIndex >= 0) {26 var item = new Object();27 item.containerIndex = i;28 item.startIndex = keyIndex;29 item.endIndex = keyIndex + key.length;30 highlightOne(item, classId, SEARCH_SPAN_ID_PREFIX + i);31 keyIndex = childText.indexOf(key, keyIndex + 1);32 }33 }34}35function getSearchSpanRects() {36 var spans = document.getElementsByTagName('span');37 var length = spans.length;38 //var scrollObject = getDocumentBodyScrollObject();39 var scrollLeft = document.body.scrollLeft;40 var scrollTop = document.body.scrollTop;41 var allRects = new Array();42 for (var i = 0; i < spans.length; i++) {43 var rects = getRectArray(spans[i], scrollLeft, scrollTop);44 allRects = allRects.concat(rects);45 }46 return allRects;47}48function getRectArray(element, scrollLeft, scrollTop) {49 var rectList = element.getClientRects();50 var rectArray = new Array();51 for (var j = 0; j < rectList.length; j++) {52 var rect = rectList.item(j);53 var myRect =54 {55 'left': rect.left + scrollLeft,56 'top': rect.top + scrollTop,57 'width': rect.width,58 'height': rect.height59 };60 rectArray[j] = myRect;61 }62 return rectArray;63}64Array.prototype.peek = function () {65 return this[this.length - 1];66};67function highlightOne(item, classId, spanId) {68 if (item) {69 var range = document.createRange();70 var childNodes = document.body.childNodes;71 var container = childNodes[item.containerIndex];72 if (range && container) {73 var startOffset = item.startIndex;74 var endOffset = item.endIndex;75 setRange(range,container,startOffset,endOffset);76 createSpanForRange(range, classId, spanId);77 }78 range.detach();79 }80}81function setRange(range, node, startIndex, endIndex) {82 if (node.nodeType == ELEMENT_NODE_TYPE) {83 var children = node.childNodes;84 var i = 0;85 var child = children[i];86 while (child) {87 if (setRange(range, child, startIndex, endIndex)) {88 return;89 } else {90 startIndex -= child.textContent.length;91 endIndex -= child.textContent.length;92 i++;93 child = children[i];94 }95 }96 } else if (node.nodeType == TEXT_NODE_TYPE) {97 var isOk = false;98 if (startIndex>=0 && node.textContent.length > startIndex) {99 range.setStart(node, startIndex);100 }101 if (endIndex>=0 && node.textContent.length > endIndex) {102 isOk = true;103 range.setEnd(node, endIndex);104 }105 return isOk;106 }107}108function createSpanForRange(range, classId, spanId) {109 var span = document.createElement("span");110 span.setAttribute("class", classId);111 range.surroundContents(span);112}113function getDocumentBodyScrollObject() {114 var bodyStyle = document.defaultView.getComputedStyle(document.body);115 var bodyX = parseInt(bodyStyle.getPropertyValue('margin-left'));116 var bodyY = parseInt(bodyStyle.getPropertyValue('margin-top'));117 var bodyRects = document.body.getClientRects();118 var bodyRect = bodyRects.item(0);119 var scrollObject = new Object();120 scrollObject.left = bodyRect.left - bodyX;121 scrollObject.top = bodyRect.top - bodyY;122 return scrollObject;123}124/*125 var testSearchJsonText = '{"count":140,"array":[{"containerIndex":13,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":13,"startIndex":87,"endIndex":89,"value":"å½¼å¾"},{"containerIndex":15,"startIndex":20,"endIndex":22,"value":"å½¼å¾"},{"containerIndex":17,"startIndex":58,"endIndex":60,"value":"å½¼å¾"},{"containerIndex":19,"startIndex":29,"endIndex":31,"value":"å½¼å¾"},{"containerIndex":19,"startIndex":74,"endIndex":76,"value":"å½¼å¾"},{"containerIndex":19,"startIndex":94,"endIndex":96,"value":"å½¼å¾"},{"containerIndex":19,"startIndex":112,"endIndex":114,"value":"å½¼å¾"},{"containerIndex":19,"startIndex":161,"endIndex":163,"value":"å½¼å¾"},{"containerIndex":21,"startIndex":7,"endIndex":9,"value":"å½¼å¾"},{"containerIndex":21,"startIndex":19,"endIndex":21,"value":"å½¼å¾"},{"containerIndex":23,"startIndex":65,"endIndex":67,"value":"å½¼å¾"},{"containerIndex":25,"startIndex":33,"endIndex":35,"value":"å½¼å¾"},{"containerIndex":29,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":35,"startIndex":8,"endIndex":10,"value":"å½¼å¾"},{"containerIndex":43,"startIndex":12,"endIndex":14,"value":"å½¼å¾"},{"containerIndex":43,"startIndex":50,"endIndex":52,"value":"å½¼å¾"},{"containerIndex":45,"startIndex":2,"endIndex":4,"value":"å½¼å¾"},{"containerIndex":47,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":49,"startIndex":99,"endIndex":101,"value":"å½¼å¾"},{"containerIndex":51,"startIndex":6,"endIndex":8,"value":"å½¼å¾"},{"containerIndex":51,"startIndex":21,"endIndex":23,"value":"å½¼å¾"},{"containerIndex":51,"startIndex":63,"endIndex":65,"value":"å½¼å¾"},{"containerIndex":53,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":55,"startIndex":5,"endIndex":7,"value":"å½¼å¾"},{"containerIndex":57,"startIndex":20,"endIndex":22,"value":"å½¼å¾"},{"containerIndex":59,"startIndex":58,"endIndex":60,"value":"å½¼å¾"},{"containerIndex":61,"startIndex":29,"endIndex":31,"value":"å½¼å¾"},{"containerIndex":61,"startIndex":74,"endIndex":76,"value":"å½¼å¾"},{"containerIndex":61,"startIndex":94,"endIndex":96,"value":"å½¼å¾"},{"containerIndex":61,"startIndex":112,"endIndex":114,"value":"å½¼å¾"},{"containerIndex":61,"startIndex":161,"endIndex":163,"value":"å½¼å¾"},{"containerIndex":63,"startIndex":7,"endIndex":9,"value":"å½¼å¾"},{"containerIndex":63,"startIndex":19,"endIndex":21,"value":"å½¼å¾"},{"containerIndex":65,"startIndex":65,"endIndex":67,"value":"å½¼å¾"},{"containerIndex":67,"startIndex":33,"endIndex":35,"value":"å½¼å¾"},{"containerIndex":71,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":77,"startIndex":8,"endIndex":10,"value":"å½¼å¾"},{"containerIndex":85,"startIndex":12,"endIndex":14,"value":"å½¼å¾"},{"containerIndex":85,"startIndex":50,"endIndex":52,"value":"å½¼å¾"},{"containerIndex":87,"startIndex":2,"endIndex":4,"value":"å½¼å¾"},{"containerIndex":89,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":91,"startIndex":99,"endIndex":101,"value":"å½¼å¾"},{"containerIndex":93,"startIndex":6,"endIndex":8,"value":"å½¼å¾"},{"containerIndex":93,"startIndex":21,"endIndex":23,"value":"å½¼å¾"},{"containerIndex":93,"startIndex":63,"endIndex":65,"value":"å½¼å¾"},{"containerIndex":95,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":97,"startIndex":5,"endIndex":7,"value":"å½¼å¾"},{"containerIndex":99,"startIndex":20,"endIndex":22,"value":"å½¼å¾"},{"containerIndex":101,"startIndex":58,"endIndex":60,"value":"å½¼å¾"},{"containerIndex":103,"startIndex":29,"endIndex":31,"value":"å½¼å¾"},{"containerIndex":103,"startIndex":74,"endIndex":76,"value":"å½¼å¾"},{"containerIndex":103,"startIndex":94,"endIndex":96,"value":"å½¼å¾"},{"containerIndex":103,"startIndex":112,"endIndex":114,"value":"å½¼å¾"},{"containerIndex":103,"startIndex":161,"endIndex":163,"value":"å½¼å¾"},{"containerIndex":105,"startIndex":7,"endIndex":9,"value":"å½¼å¾"},{"containerIndex":105,"startIndex":19,"endIndex":21,"value":"å½¼å¾"},{"containerIndex":107,"startIndex":65,"endIndex":67,"value":"å½¼å¾"},{"containerIndex":109,"startIndex":33,"endIndex":35,"value":"å½¼å¾"},{"containerIndex":113,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":119,"startIndex":8,"endIndex":10,"value":"å½¼å¾"},{"containerIndex":127,"startIndex":12,"endIndex":14,"value":"å½¼å¾"},{"containerIndex":127,"startIndex":50,"endIndex":52,"value":"å½¼å¾"},{"containerIndex":129,"startIndex":2,"endIndex":4,"value":"å½¼å¾"},{"containerIndex":131,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":133,"startIndex":99,"endIndex":101,"value":"å½¼å¾"},{"containerIndex":135,"startIndex":6,"endIndex":8,"value":"å½¼å¾"},{"containerIndex":135,"startIndex":21,"endIndex":23,"value":"å½¼å¾"},{"containerIndex":135,"startIndex":63,"endIndex":65,"value":"å½¼å¾"},{"containerIndex":137,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":139,"startIndex":5,"endIndex":7,"value":"å½¼å¾"},{"containerIndex":141,"startIndex":20,"endIndex":22,"value":"å½¼å¾"},{"containerIndex":143,"startIndex":58,"endIndex":60,"value":"å½¼å¾"},{"containerIndex":145,"startIndex":29,"endIndex":31,"value":"å½¼å¾"},{"containerIndex":145,"startIndex":74,"endIndex":76,"value":"å½¼å¾"},{"containerIndex":145,"startIndex":94,"endIndex":96,"value":"å½¼å¾"},{"containerIndex":145,"startIndex":112,"endIndex":114,"value":"å½¼å¾"},{"containerIndex":145,"startIndex":161,"endIndex":163,"value":"å½¼å¾"},{"containerIndex":147,"startIndex":7,"endIndex":9,"value":"å½¼å¾"},{"containerIndex":147,"startIndex":19,"endIndex":21,"value":"å½¼å¾"},{"containerIndex":149,"startIndex":65,"endIndex":67,"value":"å½¼å¾"},{"containerIndex":151,"startIndex":33,"endIndex":35,"value":"å½¼å¾"},{"containerIndex":155,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":161,"startIndex":8,"endIndex":10,"value":"å½¼å¾"},{"containerIndex":169,"startIndex":12,"endIndex":14,"value":"å½¼å¾"},{"containerIndex":169,"startIndex":50,"endIndex":52,"value":"å½¼å¾"},{"containerIndex":171,"startIndex":2,"endIndex":4,"value":"å½¼å¾"},{"containerIndex":173,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":175,"startIndex":99,"endIndex":101,"value":"å½¼å¾"},{"containerIndex":177,"startIndex":6,"endIndex":8,"value":"å½¼å¾"},{"containerIndex":177,"startIndex":21,"endIndex":23,"value":"å½¼å¾"},{"containerIndex":177,"startIndex":63,"endIndex":65,"value":"å½¼å¾"},{"containerIndex":179,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":181,"startIndex":5,"endIndex":7,"value":"å½¼å¾"},{"containerIndex":183,"startIndex":20,"endIndex":22,"value":"å½¼å¾"},{"containerIndex":185,"startIndex":58,"endIndex":60,"value":"å½¼å¾"},{"containerIndex":187,"startIndex":29,"endIndex":31,"value":"å½¼å¾"},{"containerIndex":187,"startIndex":74,"endIndex":76,"value":"å½¼å¾"},{"containerIndex":187,"startIndex":94,"endIndex":96,"value":"å½¼å¾"},{"containerIndex":187,"startIndex":112,"endIndex":114,"value":"å½¼å¾"},{"containerIndex":187,"startIndex":161,"endIndex":163,"value":"å½¼å¾"},{"containerIndex":189,"startIndex":7,"endIndex":9,"value":"å½¼å¾"},{"containerIndex":189,"startIndex":19,"endIndex":21,"value":"å½¼å¾"},{"containerIndex":191,"startIndex":65,"endIndex":67,"value":"å½¼å¾"},{"containerIndex":193,"startIndex":33,"endIndex":35,"value":"å½¼å¾"},{"containerIndex":197,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":203,"startIndex":8,"endIndex":10,"value":"å½¼å¾"},{"containerIndex":211,"startIndex":12,"endIndex":14,"value":"å½¼å¾"},{"containerIndex":211,"startIndex":50,"endIndex":52,"value":"å½¼å¾"},{"containerIndex":213,"startIndex":2,"endIndex":4,"value":"å½¼å¾"},{"containerIndex":215,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":217,"startIndex":99,"endIndex":101,"value":"å½¼å¾"},{"containerIndex":219,"startIndex":6,"endIndex":8,"value":"å½¼å¾"},{"containerIndex":219,"startIndex":21,"endIndex":23,"value":"å½¼å¾"},{"containerIndex":219,"startIndex":63,"endIndex":65,"value":"å½¼å¾"},{"containerIndex":221,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":223,"startIndex":5,"endIndex":7,"value":"å½¼å¾"},{"containerIndex":225,"startIndex":20,"endIndex":22,"value":"å½¼å¾"},{"containerIndex":227,"startIndex":58,"endIndex":60,"value":"å½¼å¾"},{"containerIndex":229,"startIndex":29,"endIndex":31,"value":"å½¼å¾"},{"containerIndex":229,"startIndex":74,"endIndex":76,"value":"å½¼å¾"},{"containerIndex":229,"startIndex":94,"endIndex":96,"value":"å½¼å¾"},{"containerIndex":229,"startIndex":112,"endIndex":114,"value":"å½¼å¾"},{"containerIndex":229,"startIndex":161,"endIndex":163,"value":"å½¼å¾"},{"containerIndex":231,"startIndex":7,"endIndex":9,"value":"å½¼å¾"},{"containerIndex":231,"startIndex":19,"endIndex":21,"value":"å½¼å¾"},{"containerIndex":233,"startIndex":65,"endIndex":67,"value":"å½¼å¾"},{"containerIndex":235,"startIndex":33,"endIndex":35,"value":"å½¼å¾"},{"containerIndex":239,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":245,"startIndex":8,"endIndex":10,"value":"å½¼å¾"},{"containerIndex":253,"startIndex":12,"endIndex":14,"value":"å½¼å¾"},{"containerIndex":253,"startIndex":50,"endIndex":52,"value":"å½¼å¾"},{"containerIndex":255,"startIndex":2,"endIndex":4,"value":"å½¼å¾"},{"containerIndex":257,"startIndex":9,"endIndex":11,"value":"å½¼å¾"},{"containerIndex":259,"startIndex":99,"endIndex":101,"value":"å½¼å¾"},{"containerIndex":261,"startIndex":6,"endIndex":8,"value":"å½¼å¾"},{"containerIndex":261,"startIndex":21,"endIndex":23,"value":"å½¼å¾"},{"containerIndex":261,"startIndex":63,"endIndex":65,"value":"å½¼å¾"},{"containerIndex":263,"startIndex":0,"endIndex":2,"value":"å½¼å¾"},{"containerIndex":265,"startIndex":5,"endIndex":7,"value":"å½¼å¾"}]}';126 function searchAndHighlight() {127 highlightAll(testSearchJsonText);128 }129 function highlightAll(json) {130 var searchResult = string2Json(json);131 if (searchResult.count <= 0)132 return false;133 var i = 0;134 for (i = 0; i < searchResult.array.length; i++) {135 var item = searchResult.array[i];136 highlightOne(item);137 }138 }...
motion.ts
Source:motion.ts
1import { MotionAction, Motion, VimDocument, WordType } from "./common";2export function calculateMotion(doc: VimDocument, motion: MotionAction, index: number): Motion {3 if (motion.motion === "line") {4 const endPosition = doc.positionFromIndex(index).translate(motion.count - 1, 0, true);5 return {6 start: index,7 end: endPosition.index,8 inclusive: false,9 linewise: true,10 };11 }12 if (motion.motion === "jump") {13 return {14 start: index,15 end: motion.target,16 inclusive: true,17 linewise: false,18 };19 }20 if (motion.motion === "h") {21 const position = doc.positionFromIndex(index);22 if (position.column >= motion.count) {23 return {24 start: index,25 end: index - motion.count,26 inclusive: false,27 linewise: false,28 };29 }30 return {31 start: index,32 end: index - position.column,33 inclusive: false,34 linewise: false,35 };36 }37 if (motion.motion === "l") {38 const position = doc.positionFromIndex(index);39 const line = doc.getLineByIndex(index);40 if (position.column + motion.count < line.range.end.character) {41 return {42 start: index,43 end: index + motion.count,44 inclusive: false,45 linewise: false,46 };47 }48 return {49 start: index,50 end: index + (line.range.end.character - position.column),51 inclusive: false,52 linewise: false,53 };54 }55 if (motion.motion === "j") {56 const initialPosition = doc.positionFromIndex(index);57 const count = Math.min(motion.count, doc.lineCount() - initialPosition.line - 1);58 const position = initialPosition.translate(count, 0, false);59 if (!position) {60 return null;61 }62 return {63 start: index,64 end: position.index,65 inclusive: false,66 linewise: true,67 };68 }69 if (motion.motion === "k") {70 const initialPosition = doc.positionFromIndex(index);71 const count = Math.min(motion.count, initialPosition.line);72 const position = initialPosition.translate(-count, 0, false);73 if (!position) {74 return null;75 }76 return {77 start: index,78 end: position.index,79 inclusive: false,80 linewise: true,81 };82 }83 if (motion.motion === "0") {84 const position = doc.positionFromIndex(index);85 const newPosition = position.translate(0, -position.column, false);86 return {87 start: index,88 end: newPosition.index,89 inclusive: false,90 linewise: false,91 };92 }93 if (motion.motion === "^") {94 const position = doc.positionFromIndex(index);95 const lineIndex = doc.getLineByIndex(index).firstNonWhitespaceCharacterIndex;96 return {97 start: index,98 end: position.translate(0, lineIndex - position.column, false).index,99 inclusive: false,100 linewise: false,101 };102 }103 if (motion.motion === "$") {104 const lineNumber = doc.positionFromIndex(index).translate(motion.count - 1, 0, true).line;105 const line = doc.getLine(lineNumber);106 const endPosition = doc.positionFromLine(line.range.end.line, line.range.end.character - 1);107 return {108 start: index,109 end: endPosition.index,110 inclusive: true,111 linewise: false,112 };113 }114 if (motion.motion === "w") {115 let endIndex = index;116 for (let i = 0; i < motion.count; i++) {117 let currentWord = doc.getWord(endIndex);118 let nextWord = doc.getWord(currentWord.end + 1);119 if (!nextWord) {120 endIndex = currentWord.end;121 break;122 }123 endIndex = nextWord.end;124 if (nextWord.type === WordType.Whitespace) {125 nextWord = doc.getWord(nextWord.end + 1);126 }127 if (!nextWord) {128 break;129 }130 endIndex = nextWord.start;131 }132 return {133 start: index,134 end: endIndex,135 inclusive: false,136 linewise: false,137 };138 }139 if (motion.motion === "G") {140 let targetLine = (motion.count ? Math.min(motion.count, doc.lineCount()) : doc.lineCount()) - 1;141 const lineIndex = doc.getLine(targetLine).firstNonWhitespaceCharacterIndex;142 return {143 start: index,144 end: doc.positionFromLine(targetLine, lineIndex).index,145 inclusive: false,146 linewise: true,147 };148 }149 if (motion.motion === "gg") {150 let targetLine = (motion.count ? Math.min(motion.count, doc.lineCount()) : 1) - 1;151 const lineIndex = doc.getLine(targetLine).firstNonWhitespaceCharacterIndex;152 return {153 start: index,154 end: doc.positionFromLine(targetLine, lineIndex).index,155 inclusive: false,156 linewise: true,157 };158 }159 if (motion.motion === "W") {160 let endIndex = index;161 for (let i = 0; i < motion.count; i++) {162 let currentWord = doc.getWORD(endIndex);163 let nextWord = doc.getWORD(currentWord.end + 1);164 if (!nextWord) {165 endIndex = currentWord.end;166 break;167 }168 endIndex = nextWord.end;169 if (nextWord.type === WordType.Whitespace) {170 nextWord = doc.getWORD(nextWord.end + 1);171 }172 if (!nextWord) {173 break;174 }175 endIndex = nextWord.start;176 }177 return {178 start: index,179 end: endIndex,180 inclusive: false,181 linewise: false,182 };183 }184 if (motion.motion === "e") {185 let endIndex = index;186 for (let i = 0; i < motion.count; i++) {187 let currentWord = doc.getWord(endIndex);188 if (endIndex === currentWord.end) {189 currentWord = doc.getWord(currentWord.end + 1);190 }191 if (!currentWord) {192 break;193 }194 endIndex = currentWord.end;195 if (currentWord.type === WordType.Whitespace) {196 currentWord = doc.getWord(currentWord.end + 1);197 }198 if (!currentWord) {199 break;200 }201 endIndex = currentWord.end;202 }203 return {204 start: index,205 end: endIndex,206 inclusive: true,207 linewise: false,208 };209 }210 if (motion.motion === "E") {211 let endIndex = index;212 for (let i = 0; i < motion.count; i++) {213 let currentWord = doc.getWORD(endIndex);214 if (endIndex === currentWord.end) {215 currentWord = doc.getWORD(currentWord.end + 1);216 }217 if (!currentWord) {218 break;219 }220 endIndex = currentWord.end;221 if (currentWord.type === WordType.Whitespace) {222 currentWord = doc.getWORD(currentWord.end + 1);223 }224 if (!currentWord) {225 break;226 }227 endIndex = currentWord.end;228 }229 return {230 start: index,231 end: endIndex,232 inclusive: true,233 linewise: false,234 };235 }236 if (motion.motion === "b") {237 let endIndex = index;238 for (let i = 0; i < motion.count; i++) {239 let currentWord = doc.getWord(endIndex);240 if (endIndex === currentWord.start) {241 currentWord = doc.getWord(currentWord.start - 1);242 }243 if (!currentWord) {244 break;245 }246 endIndex = currentWord.start;247 if (currentWord.type === WordType.Whitespace) {248 currentWord = doc.getWord(currentWord.start - 1);249 }250 if (!currentWord) {251 break;252 }253 endIndex = currentWord.start;254 }255 return {256 start: index,257 end: endIndex,258 inclusive: false,259 linewise: false,260 };261 }262 if (motion.motion === "B") {263 let endIndex = index;264 for (let i = 0; i < motion.count; i++) {265 let currentWord = doc.getWORD(endIndex);266 if (endIndex === currentWord.start) {267 currentWord = doc.getWORD(currentWord.start - 1);268 }269 if (!currentWord) {270 break;271 }272 endIndex = currentWord.start;273 if (currentWord.type === WordType.Whitespace) {274 currentWord = doc.getWORD(currentWord.start - 1);275 }276 if (!currentWord) {277 break;278 }279 endIndex = currentWord.start;280 }281 return {282 start: index,283 end: endIndex,284 inclusive: true,285 linewise: false,286 };287 }288 if (motion.motion === "f" || motion.motion === "t") {289 let endIndex = index + 1;290 let k = 0;291 const text = doc.getText();292 for (; endIndex <= text.length; endIndex++) {293 if (endIndex === text.length || text[endIndex] === "\n")294 return null;295 if (text[endIndex] === motion.target) {296 k++;297 if (k === motion.count)298 break;299 }300 }301 return {302 start: index,303 end: endIndex - (motion.motion === "t" ? 1 : 0),304 inclusive: true,305 linewise: false,306 };307 }308 if (motion.motion === "F" || motion.motion === "T") {309 let endIndex = index - 1;310 let k = 0;311 const text = doc.getText();312 for (; endIndex >= -1; endIndex--) {313 if (endIndex < 0 || text[endIndex] === "\n")314 return null;315 if (text[endIndex] === motion.target) {316 k++;317 if (k === motion.count)318 break;319 }320 }321 return {322 start: index,323 end: endIndex + (motion.motion === "T" ? 1 : 0),324 inclusive: false,325 linewise: false,326 };327 }328 if (motion.motion === "-" || motion.motion === "\n" || motion.motion === "_" || motion.motion === "+") {329 const count = (motion.count * (motion.motion === "-" ? -1 : 1)) - (motion.motion === "_" ? 1 : 0);330 const targetLineNumber = Math.max(Math.min(doc.getLineByIndex(index).lineNumber + count, doc.lineCount() - 1), 0);331 const targetLine = doc.getLine(targetLineNumber);332 const targetColumn = targetLine.firstNonWhitespaceCharacterIndex;333 return {334 start: index,335 end: doc.positionFromLine(targetLineNumber, targetColumn).index,336 inclusive: false,337 linewise: true,338 };339 }340 if (motion.motion === "%") {341 // {count}% goes to a % of the file.342 if (motion.count) {343 const lineNumber = Math.floor((motion.count * doc.lineCount() + 99) / 100) - 1;344 const line = doc.getLine(lineNumber);345 return {346 start: index,347 end: doc.positionFromLine(lineNumber, line.firstNonWhitespaceCharacterIndex).index,348 inclusive: false,349 linewise: true,350 };351 }352 // % with no count goes to matching next brace.353 let endIndex = index;354 const matchingBraces = {355 "[": "]", "]": "[",356 "{": "}", "}": "{",357 "(": ")", ")": "(",358 };359 const text = doc.getText();360 for (; endIndex <= text.length; endIndex++) {361 if (endIndex === text.length || text[endIndex] === "\n")362 return null;363 if (/^[\(\[\{]$/.test(text[endIndex])) {364 const brace = text[endIndex];365 const matchingBrace = matchingBraces[brace];366 let unmatchedCount = 1;367 endIndex++;368 for (; endIndex < text.length; endIndex++) {369 if (text[endIndex] === brace) {370 unmatchedCount++;371 } else if (text[endIndex] === matchingBrace) {372 unmatchedCount--;373 if (unmatchedCount === 0) {374 return {375 start: index,376 end: endIndex,377 inclusive: true,378 linewise: false,379 };380 }381 }382 }383 return null;384 } else if (/[\}\]\)]$/.test(text[endIndex])) {385 const brace = text[endIndex];386 const matchingBrace = matchingBraces[brace];387 let unmatchedCount = 1;388 endIndex--;389 for (; endIndex >= 0; endIndex--) {390 if (text[endIndex] === brace) {391 unmatchedCount++;392 } else if (text[endIndex] === matchingBrace) {393 unmatchedCount--;394 if (unmatchedCount === 0) {395 return {396 start: index,397 end: endIndex,398 inclusive: true,399 linewise: false,400 };401 }402 }403 }404 return null;405 }406 }407 return null;408 }...
find-ranges-to-keep.spec.js
Source:find-ranges-to-keep.spec.js
1const findRangesToKeep = require('../find-ranges-to-keep');2describe('find ranges to keep', () => {3 describe('sorted', () => {4 it('should exclude ranges inside the initial one', () => {5 const rangesToRemove = [{6 startIndex: 100,7 endIndex: 200,8 }, {9 startIndex: 300,10 endIndex: 400,11 }];12 const result = [{13 startIndex: 0,14 endIndex: 500,15 }];16 findRangesToKeep(rangesToRemove, result);17 expect(result).toEqual([{18 startIndex: 0,19 endIndex: 100,20 }, {21 startIndex: 200,22 endIndex: 300,23 }, {24 startIndex: 400,25 endIndex: 500,26 }]);27 });28 it('should exclude ranges on edges of initial one', () => {29 const rangesToRemove = [{30 startIndex: 0,31 endIndex: 100,32 }, {33 startIndex: 400,34 endIndex: 500,35 }];36 const result = [{37 startIndex: 0,38 endIndex: 500,39 }];40 findRangesToKeep(rangesToRemove, result);41 expect(result).toEqual([{42 startIndex: 100,43 endIndex: 400,44 }]);45 });46 it('should exclude ranges on edges of initial one and ones in the middle', () => {47 const rangesToRemove = [{48 startIndex: 0,49 endIndex: 100,50 }, {51 startIndex: 200,52 endIndex: 300,53 }, {54 startIndex: 400,55 endIndex: 500,56 }, {57 startIndex: 600,58 endIndex: 700,59 }];60 const result = [{61 startIndex: 0,62 endIndex: 700,63 }];64 findRangesToKeep(rangesToRemove, result);65 expect(result).toEqual([{66 startIndex: 100,67 endIndex: 200,68 }, {69 startIndex: 300,70 endIndex: 400,71 }, {72 startIndex: 500,73 endIndex: 600,74 }]);75 });76 });77 describe('unsorted', () => {78 it('should exclude ranges inside the initial one', () => {79 const rangesToRemove = [{80 startIndex: 300,81 endIndex: 400,82 }, {83 startIndex: 100,84 endIndex: 200,85 }];86 const result = [{87 startIndex: 0,88 endIndex: 500,89 }];90 findRangesToKeep(rangesToRemove, result);91 expect(result).toEqual([{92 startIndex: 0,93 endIndex: 100,94 }, {95 startIndex: 200,96 endIndex: 300,97 }, {98 startIndex: 400,99 endIndex: 500,100 }]);101 });102 it('should exclude ranges on edges of initial one', () => {103 const rangesToRemove = [{104 startIndex: 400,105 endIndex: 500,106 }, {107 startIndex: 0,108 endIndex: 100,109 }];110 const result = [{111 startIndex: 0,112 endIndex: 500,113 }];114 findRangesToKeep(rangesToRemove, result);115 expect(result).toEqual([{116 startIndex: 100,117 endIndex: 400,118 }]);119 });120 it('should exclude ranges on edges of initial one and ones in the middle', () => {121 const rangesToRemove = [{122 startIndex: 200,123 endIndex: 300,124 }, {125 startIndex: 0,126 endIndex: 100,127 }, {128 startIndex: 600,129 endIndex: 700,130 }, {131 startIndex: 400,132 endIndex: 500,133 }];134 const result = [{135 startIndex: 0,136 endIndex: 700,137 }];138 findRangesToKeep(rangesToRemove, result);139 expect(result).toEqual([{140 startIndex: 100,141 endIndex: 200,142 }, {143 startIndex: 300,144 endIndex: 400,145 }, {146 startIndex: 500,147 endIndex: 600,148 }]);149 });150 });151 describe('tight', () => {152 it('should exclude very close ranges', () => {153 const rangesToRemove = [{154 startIndex: 0,155 endIndex: 200,156 }, {157 startIndex: 201,158 endIndex: 500,159 }];160 const result = [{161 startIndex: 0,162 endIndex: 500,163 }];164 findRangesToKeep(rangesToRemove, result);165 expect(result).toEqual([{166 startIndex: 200,167 endIndex: 201,168 }]);169 });170 it('should exclude sticking ranges', () => {171 const rangesToRemove = [{172 startIndex: 0,173 endIndex: 200,174 }, {175 startIndex: 200,176 endIndex: 500,177 }];178 const result = [{179 startIndex: 0,180 endIndex: 500,181 }];182 findRangesToKeep(rangesToRemove, result);183 expect(result).toEqual([]);184 });185 });186 describe('null', () => {187 it('should exclude unclosed range', () => {188 const rangesToRemove = [{189 startIndex: 0,190 endIndex: 100,191 }, {192 startIndex: 200,193 endIndex: null,194 }];195 const result = [{196 startIndex: 0,197 endIndex: 500,198 }];199 findRangesToKeep(rangesToRemove, result);200 expect(result).toEqual([{201 startIndex: 100,202 endIndex: 200,203 }]);204 });205 it('should exclude from unclosed range', () => {206 const rangesToRemove = [{207 startIndex: 0,208 endIndex: 100,209 }, {210 startIndex: 200,211 endIndex: 300,212 }];213 const result = [{214 startIndex: 0,215 endIndex: null,216 }];217 findRangesToKeep(rangesToRemove, result);218 expect(result).toEqual([{219 startIndex: 100,220 endIndex: 200,221 }, {222 startIndex: 300,223 endIndex: null,224 }]);225 });226 });227 describe('overlapping', () => {228 it('should exclude range overlapping many', () => {229 const rangesToRemove = [{230 startIndex: 100,231 endIndex: 500,232 }];233 const result = [{234 startIndex: 0,235 endIndex: 200,236 }, {237 startIndex: 300,238 endIndex: 600,239 }];240 findRangesToKeep(rangesToRemove, result);241 expect(result).toEqual([{242 startIndex: 0,243 endIndex: 100,244 }, {245 startIndex: 500,246 endIndex: 600,247 }]);248 });249 });...
within-boundaries.spec.js
Source:within-boundaries.spec.js
1const withinBoundary = require('../within-boundary');2describe('within boundaries', () => {3 describe('in', () => {4 it(`5+ 1 2 3 4 5 61 - - - - -72 - o o o o83 o o o o o94 o o o o -105 - - - - -11`, () => {12 const result = withinBoundary({13 startLine: 2,14 startIndex: 2,15 endLine: 4,16 endIndex: 4,17 }, {18 startLine: 2,19 startIndex: 2,20 endLine: 4,21 endIndex: 4,22 });23 expect(result).toBe(true);24 });25 it(`26+ 1 2 3 4 5 271 x x x x x282 x o o o o293 o o o o o304 o o o o x315 x x x x x32`, () => {33 const result = withinBoundary({34 startLine: 1,35 startIndex: 1,36 endLine: 5,37 endIndex: 5,38 }, {39 startLine: 2,40 startIndex: 2,41 endLine: 4,42 endIndex: 4,43 });44 expect(result).toBe(true);45 });46 it(`47+ 1 2 3 4 5 481 o o o o o492 o o o o o503 o o o o o514 o o o o x525 x x x x x53`, () => {54 const result = withinBoundary({55 startLine: 1,56 startIndex: 1,57 endLine: 5,58 endIndex: 5,59 }, {60 startLine: 1,61 startIndex: 1,62 endLine: 4,63 endIndex: 4,64 });65 expect(result).toBe(true);66 });67 it(`68+ 1 2 3 4 5 691 x x x x x702 x o o o o713 o o o o o724 o o o o o735 o o o o o74`, () => {75 const result = withinBoundary({76 startLine: 1,77 startIndex: 1,78 endLine: 5,79 endIndex: 5,80 }, {81 startLine: 2,82 startIndex: 2,83 endLine: 5,84 endIndex: 5,85 });86 expect(result).toBe(true);87 });88 it(`89+ 1 2 3 4 5 901 - o o o o912 o o o o o923 x x x x x934 x x o o o945 o - - - -95`, () => {96 const result = withinBoundary({97 startLine: 1,98 startIndex: 2,99 endLine: 5,100 endIndex: 1,101 }, {102 startLine: 3,103 startIndex: 1,104 endLine: 4,105 endIndex: 2,106 });107 expect(result).toBe(true);108 });109 });110 describe('out', () => {111 it(`112+ 1 2 3 4 5 1131 x x x x x1142 x o o o o1153 o o o o o1164 o o o o -1175 - - - - -118`, () => {119 const result = withinBoundary({120 startLine: 1,121 startIndex: 1,122 endLine: 4,123 endIndex: 2,124 }, {125 startLine: 2,126 startIndex: 2,127 endLine: 4,128 endIndex: 4,129 });130 expect(result).toBe(false);131 });132 it(`133+ 1 2 3 4 5 1341 - - - - -1352 - o o o o1363 o o o o o1374 o o o o x1385 x x x x x139`, () => {140 const result = withinBoundary({141 startLine: 3,142 startIndex: 3,143 endLine: 5,144 endIndex: 5,145 }, {146 startLine: 2,147 startIndex: 2,148 endLine: 4,149 endIndex: 4,150 });151 expect(result).toBe(false);152 });153 it(`154+ 1 2 3 4 5 1551 o o o o o1562 o o o o o1573 o - - x x1584 x x x x x1595 x x x x x160`, () => {161 const result = withinBoundary({162 startLine: 1,163 startIndex: 1,164 endLine: 3,165 endIndex: 1,166 }, {167 startLine: 3,168 startIndex: 4,169 endLine: 5,170 endIndex: 5,171 });172 expect(result).toBe(false);173 });174 it(`175+ 1 2 3 4 5 1761 o o o o o1772 o o o o o1783 o o o x x1794 x x x x x1805 x x x x x181`, () => {182 const result = withinBoundary({183 startLine: 1,184 startIndex: 1,185 endLine: 3,186 endIndex: 5,187 }, {188 startLine: 3,189 startIndex: 4,190 endLine: 5,191 endIndex: 5,192 });193 expect(result).toBe(false);194 });195 });196 describe('null', () => {197 it('bounding null', () => {198 const result = withinBoundary({199 startLine: 1,200 startIndex: 1,201 endLine: 5,202 endIndex: null,203 }, {204 startLine: 2,205 startIndex: 2,206 endLine: 4,207 endIndex: 4,208 });209 expect(result).toBe(true);210 });211 it('bounded null', () => {212 const result = withinBoundary({213 startLine: 1,214 startIndex: 1,215 endLine: 5,216 endIndex: 5,217 }, {218 startLine: 2,219 startIndex: 2,220 endLine: 4,221 endIndex: null,222 });223 expect(result).toBe(true);224 });225 it('two nulls bounded line', () => {226 const result = withinBoundary({227 startLine: 1,228 startIndex: 1,229 endLine: 5,230 endIndex: null,231 }, {232 startLine: 2,233 startIndex: 2,234 endLine: 4,235 endIndex: null,236 });237 expect(result).toBe(true);238 });239 it('two nulls same line', () => {240 const result = withinBoundary({241 startLine: 1,242 startIndex: 1,243 endLine: 4,244 endIndex: null,245 }, {246 startLine: 2,247 startIndex: 2,248 endLine: 4,249 endIndex: null,250 });251 expect(result).toBe(false);252 });253 it('out bounding', () => {254 const result = withinBoundary({255 startLine: 1,256 startIndex: 1,257 endLine: 3,258 endIndex: null,259 }, {260 startLine: 2,261 startIndex: 2,262 endLine: 4,263 endIndex: 4,264 });265 expect(result).toBe(false);266 });267 it('out bounded', () => {268 const result = withinBoundary({269 startLine: 1,270 startIndex: 1,271 endLine: 1,272 endIndex: 4,273 }, {274 startLine: 1,275 startIndex: 20,276 endLine: 1,277 endIndex: null,278 });279 expect(result).toBe(false);280 });281 });...
Using AI Code Generation
1var BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock.js');2var prices = [7, 1, 5, 3, 6, 4];3var obj = new BestTimeToBuyAndSellStock(prices);4var BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock.js');5var prices = [7, 1, 5, 3, 6, 4];6var obj = new BestTimeToBuyAndSellStock(prices);
Using AI Code Generation
1var BestMatch = require('./BestMatch.js');2var bm = new BestMatch();3console.log(bm.endIndex('abc', 'ab'));4console.log(bm.endIndex('abc', 'bc'));5console.log(bm.endIndex('abc', 'c'));6console.log(bm.endIndex('abc', 'd'));7console.log(bm.endIndex('abc', 'abx'));8console.log(bm.endIndex('abc', 'xabc'));9console.log(bm.endIndex('abc', 'abxabc'));10console.log(bm.endIndex('abc', 'abcx'));11console.log(bm.endIndex('abc', 'xabcx'));
Using AI Code Generation
1var BestMatchString = require('./BestMatchString');2var bms = new BestMatchString();3bms.addString('test');4bms.addString('test1');5bms.addString('test2');6bms.addString('test3');7bms.addString('test4');8bms.addString('test5');9bms.addString('test6');10bms.addString('test7');11bms.addString('test8');12bms.addString('test9');13bms.addString('test10');14bms.addString('test11');15bms.addString('test12');16bms.addString('test13');17bms.addString('test14');18bms.addString('test15');19bms.addString('test16');20bms.addString('test17');21bms.addString('test18');22bms.addString('test19');23bms.addString('test20');24bms.addString('test21');25bms.addString('test22');26bms.addString('test23');27bms.addString('test24');28bms.addString('test25');29bms.addString('test26');30bms.addString('test27');31bms.addString('test28');32bms.addString('test29');33bms.addString('test30');34bms.addString('test31');35bms.addString('test32');36bms.addString('test33');37bms.addString('test34');38bms.addString('test35');39bms.addString('test36');40bms.addString('test37');41bms.addString('test38');42bms.addString('test39');43bms.addString('test40');44bms.addString('test41');45bms.addString('test42');46bms.addString('test43');47bms.addString('test44');48bms.addString('test45');49bms.addString('test46');50bms.addString('test47');51bms.addString('test48');52bms.addString('test49');53bms.addString('test50');54bms.addString('test51');55bms.addString('test52');56bms.addString('test53');57bms.addString('test54');58bms.addString('test55');59bms.addString('test56');60bms.addString('test57');61bms.addString('test58');62bms.addString('
Using AI Code Generation
1var BestMatch = require('./BestMatch');2var bestMatch = new BestMatch();3var str = "This is a test string";4var subStr = "test";5var index = bestMatch.endIndex(str, subStr);6console.log(index);7Related Posts: Node.js | String.prototype.indexOf() Method8Node.js | String.prototype.lastIndexOf() Method9Node.js | String.prototype.search() Method10Node.js | String.prototype.replace() Method11Node.js | String.prototype.replace() Method12Node.js | String.prototype.split() Method13Node.js | String.prototype.substr() Method14Node.js | String.prototype.substring() Method15Node.js | String.prototype.toLowerCase() Method16Node.js | String.prototype.toUpperCase() Method17Node.js | String.prototype.trim() Method18Node.js | String.prototype.indexOf() Method19Node.js | String.prototype.lastIndexOf() Method20Node.js | String.prototype.search() Method21Node.js | String.prototype.replace() Method22Node.js | String.prototype.replace() Method23Node.js | String.prototype.split() Method24Node.js | String.prototype.substr() Method25Node.js | String.prototype.substring() Method26Node.js | String.prototype.toLowerCase() Method27Node.js | String.prototype.toUpperCase() Method28Node.js | String.prototype.trim() Method29Node.js | String.prototype.indexOf() Method30Node.js | String.prototype.lastIndexOf() Method31Node.js | String.prototype.search() Method32Node.js | String.prototype.replace() Method33Node.js | String.prototype.replace() Method34Node.js | String.prototype.split() Method35Node.js | String.prototype.substr() Method36Node.js | String.prototype.substring() Method37Node.js | String.prototype.toLowerCase() Method38Node.js | String.prototype.toUpperCase() Method39Node.js | String.prototype.trim() Method40Node.js | String.prototype.indexOf() Method41Node.js | String.prototype.lastIndexOf() Method42Node.js | String.prototype.search() Method43Node.js | String.prototype.replace() Method44Node.js | String.prototype.replace() Method45Node.js | String.prototype.split() Method46Node.js | String.prototype.substr() Method47Node.js | String.prototype.substring() Method48Node.js | String.prototype.toLowerCase() Method49Node.js | String.prototype.toUpperCase() Method50Node.js | String.prototype.trim() Method51Node.js | String.prototype.indexOf() Method52Node.js | String.prototype.lastIndexOf() Method53Node.js | String.prototype.search() Method54Node.js | String.prototype.replace() Method55Node.js | String.prototype.replace()
Using AI Code Generation
1var BestMatch=require("./BestMatch.js");2var bestMatch=new BestMatch();3var text="The quick brown fox jumps over the lazy dog";4var pattern="fox";5var index=bestMatch.endIndex(text,pattern);6console.log("Index of end of pattern in text is "+index);7console.log("End of pattern in text is "+text.substring(index));8console.log("Text is "+text);9console.log("Pattern is "+pattern);
Using AI Code Generation
1var BestMatch = require('./BestMatch');2var bestMatch = new BestMatch();3bestMatch.endIndex('abcd', 'cd');4var BestMatch = function() {5 this.endIndex = function(str1, str2) {6 var index = str1.indexOf(str2);7 if (index === -1) {8 console.log('Not Found');9 } else {10 console.log(index + str2.length - 1);11 }12 }13}14module.exports = BestMatch;15var BestMatch = require('./BestMatch');16var bestMatch = new BestMatch();17bestMatch.endIndex('abcd', 'cd');18var BestMatch = function() {19 this.endIndex = function(str1, str2) {20 var index = str1.indexOf(str2);21 if (index === -1) {22 console.log('Not Found');23 } else {24 console.log(index + str2.length - 1);25 }26 }27}28module.exports = BestMatch;29var BestMatch = require('./BestMatch');30var bestMatch = new BestMatch();31bestMatch.endIndex('abcd', 'cd');32var BestMatch = function() {33 this.endIndex = function(str1, str2) {34 var index = str1.indexOf(str2);35 if (index === -1) {36 console.log('Not Found');37 } else {38 console.log(index + str2.length - 1);39 }40 }41}42module.exports = BestMatch;43var BestMatch = require('./BestMatch');
Using AI Code Generation
1var BestMatch = require("./BestMatch.js");2var bestMatch = new BestMatch("my name is brian");3console.log(bestMatch.endIndex("name"));4console.log(bestMatch.endIndex("is"));5console.log(bestMatch.endIndex("brian"));6var BestMatch = function (str) {7 this.str = str;8};9BestMatch.prototype.endIndex = function (str) {10 return this.str.indexOf(str) + str.length;11};12module.exports = BestMatch;13var endIndex = function (str) {14 return this.str.indexOf(str) + str.length;15};16console.log(endIndex("name"));17console.log(endIndex("is"));18console.log(endIndex("brian"));
Using AI Code Generation
1var str = "The quick brown fox jumped over the lazy dog";2var bm = new BestMatch(str, "the lazy dog");3var index = bm.endIndex();4console.log("The index of the end of the best match is: " + index);5var str = "The quick brown fox jumped over the lazy dog";6var bm = new BestMatch(str, "the lazy dog");7var newStr = bm.replace("The big brown bear");8console.log("The new string is: " + newStr);9var str = "The quick brown fox jumped over the lazy dog";10var bm = new BestMatch(str, "the lazy dog");11var newStr = bm.replace("The big brown bear", 0);12console.log("The new string is: " + newStr);13var str = "The quick brown fox jumped over the lazy dog";14var bm = new BestMatch(str, "the lazy dog");15var newStr = bm.replace("The big brown bear", 1);16console.log("The new string is: " + newStr);17var str = "The quick brown fox jumped over the lazy dog";18var bm = new BestMatch(str, "the lazy dog");19var newStr = bm.replace("The big brown bear", 2);20console.log("The new string is: " + newStr);21var str = "The quick brown fox jumped over the lazy dog";22var bm = new BestMatch(str, "the lazy dog");23var newStr = bm.replace("The big brown bear", 3);24console.log("The new string is: " + newStr);25var str = "The quick brown fox jumped over the lazy dog";26var bm = new BestMatch(str, "the lazy dog");27var newStr = bm.replace("The big brown bear", 4);28console.log("The new string is: " + newStr);
Using AI Code Generation
1var BestMatch = require('./bestMatch.js');2var bestMatch = new BestMatch();3var input = 'b';4var result = bestMatch.endIndex(input);5console.log(result);6module.exports = BestMatch;7var BestMatch = function() {8 this.endIndex = function(input) {9 var result = 0;10 return result;11 };12};13How to use the findIndex() method in JavaScript?14How to use the indexOf() method in JavaScript?15How to use the lastIndexOf() method in JavaScript?16How to use the includes() method in JavaScript?17How to use the find() method in JavaScript?18How to use the filter() method in JavaScript?19How to use the reduce() method in JavaScript?20How to use the reduceRight() method in JavaScript?21How to use the every() method in JavaScript?22How to use the some() method in JavaScript?23How to use the forEach() method in JavaScript?24How to use the map() method in JavaScript?25How to use the sort() method in JavaScript?26How to use the reverse() method in JavaScript?27How to use the fill() method in JavaScript?28How to use the copyWithin() method in JavaScript?29How to use the entries() method in JavaScript?30How to use the keys() method in JavaScript?31How to use the values() method in JavaScript?
Using AI Code Generation
1var text = "This is a test. This is only a test.";2var pattern = "test";3var bm = new BestMatch(text, pattern);4var index = bm.endIndex();5console.log("Index of last character of best match: " + index);6function BestMatch(text, pattern) {7 this.text = text;8 this.pattern = pattern;9 this.bestMatchIndex = -1;10 this.bestMatchLength = 0;11}12BestMatch.prototype.findBestMatch = function() {13 var bestMatchLength = 0;14 var bestMatchIndex = -1;15 var text = this.text;16 var pattern = this.pattern;17 var patternLength = pattern.length;18 for (var i = 0; i < text.length; i++) {19 var matchLength = 0;20 for (var j = 0; j < patternLength; j++) {21 if (text[i + j] === pattern[j]) {22 matchLength++;23 } else {24 break;25 }26 }27 if (matchLength > bestMatchLength) {28 bestMatchLength = matchLength;29 bestMatchIndex = i;30 }31 }32 this.bestMatchLength = bestMatchLength;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!