MCPcopy Create free account
hub / github.com/microsoft/SandDance / arrayIndexOf

Function arrayIndexOf

docs/tests/v2/es6/js/sanddance.js:79087–79141  ·  view source on GitHub ↗
(arr, val, byteOffset, encoding, dir)

Source from the content-addressed store, hash-verified

79085}
79086
79087function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
79088 var indexSize = 1
79089 var arrLength = arr.length
79090 var valLength = val.length
79091
79092 if (encoding !== undefined) {
79093 encoding = String(encoding).toLowerCase()
79094 if (encoding === 'ucs2' || encoding === 'ucs-2' ||
79095 encoding === 'utf16le' || encoding === 'utf-16le') {
79096 if (arr.length < 2 || val.length < 2) {
79097 return -1
79098 }
79099 indexSize = 2
79100 arrLength /= 2
79101 valLength /= 2
79102 byteOffset /= 2
79103 }
79104 }
79105
79106 function read (buf, i) {
79107 if (indexSize === 1) {
79108 return buf[i]
79109 } else {
79110 return buf.readUInt16BE(i * indexSize)
79111 }
79112 }
79113
79114 var i
79115 if (dir) {
79116 var foundIndex = -1
79117 for (i = byteOffset; i < arrLength; i++) {
79118 if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
79119 if (foundIndex === -1) foundIndex = i
79120 if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
79121 } else {
79122 if (foundIndex !== -1) i -= i - foundIndex
79123 foundIndex = -1
79124 }
79125 }
79126 } else {
79127 if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
79128 for (i = byteOffset; i >= 0; i--) {
79129 var found = true
79130 for (var j = 0; j < valLength; j++) {
79131 if (read(arr, i + j) !== read(val, j)) {
79132 found = false
79133 break
79134 }
79135 }
79136 if (found) return i
79137 }
79138 }
79139
79140 return -1
79141}
79142
79143Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
79144 return this.indexOf(val, byteOffset, encoding) !== -1

Callers 1

bidirectionalIndexOfFunction · 0.70

Calls 1

readFunction · 0.70

Tested by

no test coverage detected