How to use endIndex method in apickli

Best JavaScript code snippet using apickli

rmkj.search.js

Source:rmkj.search.js Github

copy

Full Screen

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 }...

Full Screen

Full Screen

motion.ts

Source:motion.ts Github

copy

Full Screen

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 }...

Full Screen

Full Screen

find-ranges-to-keep.spec.js

Source:find-ranges-to-keep.spec.js Github

copy

Full Screen

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 });...

Full Screen

Full Screen

within-boundaries.spec.js

Source:within-boundaries.spec.js Github

copy

Full Screen

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 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var apickliObject = new apickli.Apickli('http', 'localhost:8080');3apickliObject.addRequestHeader('Content-Type', 'application/json');4apickliObject.addRequestHeader('Accept', 'application/json');5apickliObject.get('/test', function (error, response) {6 if (error) {7 console.log(error);8 } else {9 var responseText = response.body;10 var startIndex = responseText.indexOf('{"id":');11 var endIndex = responseText.indexOf('}');12 console.log(responseText.substring(startIndex, endIndex + 1));13 }14});15var responseText = response.body;16var responseText = response.body;17var responseText = response.body;

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var apickli = new apickli.Apickli('http', 'localhost:8080');3apickli.addRequestHeader('Content-Type','application/json');4apickli.setRequestBody("{ \"name\":\"John\" }");5apickli.post('/test', function (error, response) {6 if (error) {7 console.log(error);8 } else {9 console.log(response.statusCode);10 console.log(response.body);11 var body = JSON.parse(response.body);12 console.log(body);13 console.log(body.name);14 console.log(body.name.length);15 console.log(body.name.indexOf('o'));16 console.log(body.name.lastIndexOf('o'));17 console.log(body.name.indexOf('o', 3));18 console.log(body.name.lastIndexOf('o', 3));19 console.log(body.name.indexOf('o', 5));20 console.log(body.name.lastIndexOf('o', 5));21 console.log(body.name.indexOf('o', 7));22 console.log(body.name.lastIndexOf('o', 7));23 console.log(body.name.indexOf('o', 9));24 console.log(body.name.lastIndexOf('o', 9));25 console.log(body.name.indexOf('o', 11));26 console.log(body.name.lastIndexOf('o', 11));27 console.log(body.name.indexOf('o', 13));28 console.log(body.name.lastIndexOf('o', 13));29 console.log(body.name.indexOf('o', 15));30 console.log(body.name.lastIndexOf('o', 15));31 console.log(body.name.indexOf('o', 17));32 console.log(body.name.lastIndexOf('o', 17));33 console.log(body.name.indexOf('o', 19));34 console.log(body.name.lastIndexOf('o', 19));35 console.log(body.name.indexOf('o', 21));36 console.log(body.name.lastIndexOf('o', 21));37 console.log(body.name.indexOf('o', 23));38 console.log(body.name.lastIndexOf('o', 23));39 console.log(body.name.indexOf('o', 25));40 console.log(body.name.lastIndexOf('o', 25));41 console.log(body.name.indexOf('o', 27));42 console.log(body.name.lastIndexOf('o', 27));43 console.log(body.name.indexOf('o', 29));44 console.log(body.name.lastIndexOf('o', 29));45 console.log(body.name.indexOf

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var {defineSupportCode} = require('cucumber');3defineSupportCode(function({Given, When, Then}) {4 Given('I set the path parameter to {string}', function (string, callback) {5 this.apickli.setPathParameter('id', string);6 callback();7 });8 When('I set the query parameter to {string}', function (string, callback) {9 this.apickli.addQueryParameter('id', string);10 callback();11 });12 Then('I expect the response body to contain {string}', function (string, callback) {13 this.apickli.assertContainsBody(string);14 callback();15 });16});17Your name to display (optional):18Your name to display (optional):

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var {defineSupportCode} = require('cucumber');3defineSupportCode(function({Given, When, Then}) {4 Given('I set the request header to', function (dataTable, callback) {5 var requestHeader = dataTable.hashes();6 for (var i = 0; i < requestHeader.length; i++) {7 var header = requestHeader[i];8 this.apickli.addRequestHeader(header.key, header.value);9 }10 callback();11 });12 When('I send a GET request to $resource', function (resource, callback) {13 this.apickli.get(resource, callback);14 });15 Then('I should get HTTP response code $statusCode', function (statusCode, callback) {16 this.apickli.assertResponseCode(statusCode);17 callback();18 });19 Then('I should get HTTP response body containing $responseBody', function (responseBody, callback) {20 this.apickli.assertResponseBodyContains(responseBody);21 callback();22 });23 Then('the response should contain $responseBody', function (responseBody, callback) {24 this.apickli.assertResponseBodyContains(responseBody);25 callback();26 });27 Then('the response should not contain $responseBody', function (responseBody, callback) {28 this.apickli.assertResponseBodyNotContains(responseBody);29 callback();30 });31 Then('I should get HTTP response header $headerName containing $headerValue', function (headerName, headerValue, callback) {32 this.apickli.assertResponseHeaderContains(headerName, headerValue);33 callback();34 });35 Then('I should get HTTP response header $headerName not containing $headerValue', function (headerName, headerValue, callback) {36 this.apickli.assertResponseHeaderNotContains(headerName, headerValue);37 callback();38 });39 Then('the response should contain the header $headerName with the value $headerValue', function (headerName, headerValue, callback) {40 this.apickli.assertResponseHeaderContains(headerName, headerValue);41 callback();42 });43 Then('the response should not contain the header $headerName with the value $headerValue', function (headerName, headerValue, callback) {44 this.apickli.assertResponseHeaderNotContains(headerName, headerValue);45 callback();46 });47 Then('I should get HTTP response body JSONPath $jsonPath

Full Screen

Using AI Code Generation

copy

Full Screen

1var apickli = require('apickli');2var myApickli = new apickli.Apickli('http', 'httpbin.org');3 if (error) {4 console.log(error);5 } else {6 console.log(body);7 }8});9Syntax: startIndex(url, method, callback)10var apickli = require('apickli');11var myApickli = new apickli.Apickli('http', 'httpbin.org');12 if (error) {13 console.log(error);14 } else {15 console.log(body);16 }17});18Syntax: getResponseHeader(headerName)19var apickli = require('apickli');20var myApickli = new apickli.Apickli('http', 'httpbin.org');21myApickli.getResponseHeader('Content-Type');

Full Screen

Using AI Code Generation

copy

Full Screen

1this.Then(/^I should see the text "([^"]*)"$/, function (text, callback) {2 var apickli = this.apickli;3 apickli.addRequestHeader('Authorization', 'Basic ' + new Buffer('user:password').toString('base64'));4 apickli.addRequestHeader('Content-Type', 'application/json');5 apickli.addRequestHeader('Accept', 'application/json');6 apickli.addRequestHeader('X-HTTP-Method-Override', 'GET');7 apickli.addRequestHeader('X-HTTP-Method-Override', 'GET');8 var data = {9 };10 apickli.post('/v1/organizations/1/roles', data, function (error, response) {11 if (error) {12 callback(error);13 } else {14 var body = JSON.parse(response.body);15 console.log(body);16 var startIndex = response.body.indexOf(text);17 var endIndex = response.body.indexOf('description');18 console.log(startIndex);19 console.log(endIndex);20 var message = response.body.substring(startIndex, endIndex);21 console.log(message);22 callback();23 }24 });25});26var startIndex = response.body.indexOf(text);27var endIndex = response.body.indexOf('description');28console.log(startIndex);29console.log(endIndex);30var message = response.body.substring(startIndex, endIndex);31console.log(message);32{33}

Full Screen

Using AI Code Generation

copy

Full Screen

1this.Then(/^I should get the value of "([^"]*)" from the response$/, function (arg1, callback) {2 var index = this.apickli.getResponseObject().endIndex;3 console.log(index);4 callback();5});6{7{8},9{10}11}

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run apickli automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful