MCPcopy Index your code
hub / github.com/microsoft/SandDance / arrayIndexOf

Function arrayIndexOf

docs/app/js/sanddance-app.js:113335–113375  ·  view source on GitHub ↗
(arr, val, byteOffset, encoding, dir)

Source from the content-addressed store, hash-verified

113333 throw new TypeError("val must be string, number or Buffer");
113334}
113335function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
113336 var indexSize = 1;
113337 var arrLength = arr.length;
113338 var valLength = val.length;
113339 if (encoding !== undefined) {
113340 encoding = String(encoding).toLowerCase();
113341 if (encoding === "ucs2" || encoding === "ucs-2" || encoding === "utf16le" || encoding === "utf-16le") {
113342 if (arr.length < 2 || val.length < 2) return -1;
113343 indexSize = 2;
113344 arrLength /= 2;
113345 valLength /= 2;
113346 byteOffset /= 2;
113347 }
113348 }
113349 function read(buf, i) {
113350 if (indexSize === 1) return buf[i];
113351 else return buf.readUInt16BE(i * indexSize);
113352 }
113353 var i1;
113354 if (dir) {
113355 var foundIndex = -1;
113356 for(i1 = byteOffset; i1 < arrLength; i1++)if (read(arr, i1) === read(val, foundIndex === -1 ? 0 : i1 - foundIndex)) {
113357 if (foundIndex === -1) foundIndex = i1;
113358 if (i1 - foundIndex + 1 === valLength) return foundIndex * indexSize;
113359 } else {
113360 if (foundIndex !== -1) i1 -= i1 - foundIndex;
113361 foundIndex = -1;
113362 }
113363 } else {
113364 if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
113365 for(i1 = byteOffset; i1 >= 0; i1--){
113366 var found = true;
113367 for(var j = 0; j < valLength; j++)if (read(arr, i1 + j) !== read(val, j)) {
113368 found = false;
113369 break;
113370 }
113371 if (found) return i1;
113372 }
113373 }
113374 return -1;
113375}
113376Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
113377 return this.indexOf(val, byteOffset, encoding) !== -1;
113378};

Callers 1

bidirectionalIndexOfFunction · 0.70

Calls 1

readFunction · 0.70

Tested by

no test coverage detected