CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes45downloads
browser.js564 linesDownload Raw Back to b4a
1const ascii = require('./lib/ascii')2const base64 = require('./lib/base64')3const hex = require('./lib/hex')4const utf8 = require('./lib/utf8')5const utf16le = require('./lib/utf16le')6 7const LE = new Uint8Array(Uint16Array.of(0xff).buffer)[0] === 0xff8 9function codecFor(encoding) {10  switch (encoding) {11    case 'ascii':12      return ascii13    case 'base64':14      return base6415    case 'hex':16      return hex17    case 'utf8':18    case 'utf-8':19    case undefined:20    case null:21      return utf822    case 'ucs2':23    case 'ucs-2':24    case 'utf16le':25    case 'utf-16le':26      return utf16le27    default:28      throw new Error(`Unknown encoding '${encoding}'`)29  }30}31 32function isBuffer(value) {33  return value instanceof Uint8Array34}35 36function isEncoding(encoding) {37  try {38    codecFor(encoding)39    return true40  } catch {41    return false42  }43}44 45function alloc(size, fill, encoding) {46  const buffer = new Uint8Array(size)47  if (fill !== undefined) {48    exports.fill(buffer, fill, 0, buffer.byteLength, encoding)49  }50  return buffer51}52 53function allocUnsafe(size) {54  return new Uint8Array(size)55}56 57function allocUnsafeSlow(size) {58  return new Uint8Array(size)59}60 61function byteLength(string, encoding) {62  return codecFor(encoding).byteLength(string)63}64 65function compare(a, b) {66  if (a === b) return 067 68  const len = Math.min(a.byteLength, b.byteLength)69 70  a = new DataView(a.buffer, a.byteOffset, a.byteLength)71  b = new DataView(b.buffer, b.byteOffset, b.byteLength)72 73  let i = 074 75  for (let n = len - (len % 4); i < n; i += 4) {76    const x = a.getUint32(i, LE)77    const y = b.getUint32(i, LE)78    if (x !== y) break79  }80 81  for (; i < len; i++) {82    const x = a.getUint8(i)83    const y = b.getUint8(i)84    if (x < y) return -185    if (x > y) return 186  }87 88  return a.byteLength > b.byteLength ? 1 : a.byteLength < b.byteLength ? -1 : 089}90 91function concat(buffers, length) {92  if (length === undefined) {93    length = buffers.reduce((len, buffer) => len + buffer.byteLength, 0)94  }95 96  const result = new Uint8Array(length)97 98  let offset = 099 100  for (const buffer of buffers) {101    if (offset + buffer.byteLength > result.byteLength) {102      result.set(buffer.subarray(0, result.byteLength - offset), offset)103      return result104    }105 106    result.set(buffer, offset)107    offset += buffer.byteLength108  }109 110  return result111}112 113function copy(114  source,115  target,116  targetStart = 0,117  sourceStart = 0,118  sourceEnd = source.byteLength119) {120  if (targetStart < 0) targetStart = 0121  if (targetStart >= target.byteLength) return 0122 123  const targetLength = target.byteLength - targetStart124 125  if (sourceStart < 0) sourceStart = 0126  if (sourceStart >= source.byteLength) return 0127 128  if (sourceEnd <= sourceStart) return 0129  if (sourceEnd > source.byteLength) sourceEnd = source.byteLength130 131  if (sourceEnd - sourceStart > targetLength) {132    sourceEnd = sourceStart + targetLength133  }134 135  const sourceLength = sourceEnd - sourceStart136 137  if (source === target) {138    target.copyWithin(targetStart, sourceStart, sourceEnd)139  } else {140    if (sourceStart !== 0 || sourceEnd !== source.byteLength) {141      source = source.subarray(sourceStart, sourceEnd)142    }143 144    target.set(source, targetStart)145  }146 147  return sourceLength148}149 150function equals(a, b) {151  if (a === b) return true152  if (a.byteLength !== b.byteLength) return false153 154  return compare(a, b) === 0155}156 157function fill(158  buffer,159  value,160  offset = 0,161  end = buffer.byteLength,162  encoding = 'utf8'163) {164  if (typeof value === 'string') {165    if (typeof offset === 'string') {166      // fill(string, encoding)167      encoding = offset168      offset = 0169      end = buffer.byteLength170    } else if (typeof end === 'string') {171      // fill(string, offset, encoding)172      encoding = end173      end = buffer.byteLength174    }175  } else if (typeof value === 'number') {176    value = value & 0xff177  } else if (typeof value === 'boolean') {178    value = +value179  }180 181  if (offset < 0) offset = 0182  if (offset >= buffer.byteLength) return buffer183 184  if (end <= offset) return buffer185  if (end > buffer.byteLength) end = buffer.byteLength186 187  if (typeof value === 'number') return buffer.fill(value, offset, end)188 189  if (typeof value === 'string') value = exports.from(value, encoding)190 191  const len = value.byteLength192 193  for (let i = 0, n = end - offset; i < n; ++i) {194    buffer[i + offset] = value[i % len]195  }196 197  return buffer198}199 200function from(value, encodingOrOffset, length) {201  // from(string, encoding)202  if (typeof value === 'string') return fromString(value, encodingOrOffset)203 204  // from(array)205  if (Array.isArray(value)) return fromArray(value)206 207  // from(buffer)208  if (ArrayBuffer.isView(value)) return fromBuffer(value)209 210  // from(arrayBuffer[, byteOffset[, length]])211  return fromArrayBuffer(value, encodingOrOffset, length)212}213 214function fromString(string, encoding) {215  const codec = codecFor(encoding)216  const buffer = new Uint8Array(codec.byteLength(string))217  codec.write(buffer, string)218  return buffer219}220 221function fromArray(array) {222  const buffer = new Uint8Array(array.length)223  buffer.set(array)224  return buffer225}226 227function fromBuffer(buffer) {228  const copy = new Uint8Array(buffer.byteLength)229  copy.set(buffer)230  return copy231}232 233function fromArrayBuffer(arrayBuffer, byteOffset, length) {234  return new Uint8Array(arrayBuffer, byteOffset, length)235}236 237function includes(buffer, value, byteOffset, encoding) {238  return indexOf(buffer, value, byteOffset, encoding) !== -1239}240 241function indexOf(buffer, value, byteOffset, encoding) {242  return bidirectionalIndexOf(243    buffer,244    value,245    byteOffset,246    encoding,247    true /* first */248  )249}250 251function lastIndexOf(buffer, value, byteOffset, encoding) {252  return bidirectionalIndexOf(253    buffer,254    value,255    byteOffset,256    encoding,257    false /* last */258  )259}260 261function bidirectionalIndexOf(buffer, value, byteOffset, encoding, first) {262  if (buffer.byteLength === 0) return -1263 264  if (typeof byteOffset === 'string') {265    encoding = byteOffset266    byteOffset = 0267  } else if (byteOffset === undefined) {268    byteOffset = first ? 0 : buffer.length - 1269  } else if (byteOffset < 0) {270    byteOffset += buffer.byteLength271  }272 273  if (byteOffset >= buffer.byteLength) {274    if (first) return -1275    else byteOffset = buffer.byteLength - 1276  } else if (byteOffset < 0) {277    if (first) byteOffset = 0278    else return -1279  }280 281  if (typeof value === 'string') {282    value = from(value, encoding)283  } else if (typeof value === 'number') {284    value = value & 0xff285 286    if (first) {287      return buffer.indexOf(value, byteOffset)288    } else {289      return buffer.lastIndexOf(value, byteOffset)290    }291  }292 293  if (value.byteLength === 0) return -1294 295  if (first) {296    let foundIndex = -1297 298    for (let i = byteOffset; i < buffer.byteLength; i++) {299      if (buffer[i] === value[foundIndex === -1 ? 0 : i - foundIndex]) {300        if (foundIndex === -1) foundIndex = i301        if (i - foundIndex + 1 === value.byteLength) return foundIndex302      } else {303        if (foundIndex !== -1) i -= i - foundIndex304        foundIndex = -1305      }306    }307  } else {308    if (byteOffset + value.byteLength > buffer.byteLength) {309      byteOffset = buffer.byteLength - value.byteLength310    }311 312    for (let i = byteOffset; i >= 0; i--) {313      let found = true314 315      for (let j = 0; j < value.byteLength; j++) {316        if (buffer[i + j] !== value[j]) {317          found = false318          break319        }320      }321 322      if (found) return i323    }324  }325 326  return -1327}328 329function swap(buffer, n, m) {330  const i = buffer[n]331  buffer[n] = buffer[m]332  buffer[m] = i333}334 335function swap16(buffer) {336  const len = buffer.byteLength337 338  if (len % 2 !== 0)339    throw new RangeError('Buffer size must be a multiple of 16-bits')340 341  for (let i = 0; i < len; i += 2) swap(buffer, i, i + 1)342 343  return buffer344}345 346function swap32(buffer) {347  const len = buffer.byteLength348 349  if (len % 4 !== 0)350    throw new RangeError('Buffer size must be a multiple of 32-bits')351 352  for (let i = 0; i < len; i += 4) {353    swap(buffer, i, i + 3)354    swap(buffer, i + 1, i + 2)355  }356 357  return buffer358}359 360function swap64(buffer) {361  const len = buffer.byteLength362 363  if (len % 8 !== 0)364    throw new RangeError('Buffer size must be a multiple of 64-bits')365 366  for (let i = 0; i < len; i += 8) {367    swap(buffer, i, i + 7)368    swap(buffer, i + 1, i + 6)369    swap(buffer, i + 2, i + 5)370    swap(buffer, i + 3, i + 4)371  }372 373  return buffer374}375 376function toBuffer(buffer) {377  return buffer378}379 380function toString(381  buffer,382  encoding = 'utf8',383  start = 0,384  end = buffer.byteLength385) {386  // toString(buffer)387  if (arguments.length === 1) return utf8.toString(buffer)388 389  // toString(buffer, encoding)390  if (arguments.length === 2) return codecFor(encoding).toString(buffer)391 392  if (start < 0) start = 0393  if (start >= buffer.byteLength) return ''394 395  if (end <= start) return ''396  if (end > buffer.byteLength) end = buffer.byteLength397 398  if (start !== 0 || end !== buffer.byteLength) {399    buffer = buffer.subarray(start, end)400  }401 402  return codecFor(encoding).toString(buffer)403}404 405function write(buffer, string, offset, length, encoding) {406  // write(buffer, string)407  if (arguments.length === 2) return utf8.write(buffer, string)408 409  if (typeof offset === 'string') {410    // write(buffer, string, encoding)411    encoding = offset412    offset = 0413    length = buffer.byteLength414  } else if (typeof length === 'string') {415    // write(buffer, string, offset, encoding)416    encoding = length417    length = buffer.byteLength - offset418  }419 420  length = Math.min(length, exports.byteLength(string, encoding))421 422  let start = offset423  if (start < 0) start = 0424  if (start >= buffer.byteLength) return 0425 426  let end = offset + length427  if (end <= start) return 0428  if (end > buffer.byteLength) end = buffer.byteLength429 430  if (start !== 0 || end !== buffer.byteLength) {431    buffer = buffer.subarray(start, end)432  }433 434  return codecFor(encoding).write(buffer, string)435}436 437function readDoubleBE(buffer, offset = 0) {438  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)439  return view.getFloat64(offset, false)440}441 442function readDoubleLE(buffer, offset = 0) {443  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)444  return view.getFloat64(offset, true)445}446 447function readFloatBE(buffer, offset = 0) {448  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)449  return view.getFloat32(offset, false)450}451 452function readFloatLE(buffer, offset = 0) {453  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)454  return view.getFloat32(offset, true)455}456 457function readInt32BE(buffer, offset = 0) {458  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)459  return view.getInt32(offset, false)460}461 462function readInt32LE(buffer, offset = 0) {463  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)464  return view.getInt32(offset, true)465}466 467function readUInt32BE(buffer, offset = 0) {468  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)469  return view.getUint32(offset, false)470}471 472function readUInt32LE(buffer, offset = 0) {473  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)474  return view.getUint32(offset, true)475}476 477function writeDoubleBE(buffer, value, offset = 0) {478  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)479  view.setFloat64(offset, value, false)480  return offset + 8481}482 483function writeDoubleLE(buffer, value, offset = 0) {484  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)485  view.setFloat64(offset, value, true)486  return offset + 8487}488 489function writeFloatBE(buffer, value, offset = 0) {490  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)491  view.setFloat32(offset, value, false)492  return offset + 4493}494 495function writeFloatLE(buffer, value, offset = 0) {496  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)497  view.setFloat32(offset, value, true)498  return offset + 4499}500 501function writeInt32BE(buffer, value, offset = 0) {502  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)503  view.setInt32(offset, value, false)504  return offset + 4505}506 507function writeInt32LE(buffer, value, offset = 0) {508  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)509  view.setInt32(offset, value, true)510  return offset + 4511}512 513function writeUInt32BE(buffer, value, offset = 0) {514  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)515  view.setUint32(offset, value, false)516  return offset + 4517}518 519function writeUInt32LE(buffer, value, offset = 0) {520  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)521  view.setUint32(offset, value, true)522  return offset + 4523}524 525module.exports = exports = {526  isBuffer,527  isEncoding,528  alloc,529  allocUnsafe,530  allocUnsafeSlow,531  byteLength,532  compare,533  concat,534  copy,535  equals,536  fill,537  from,538  includes,539  indexOf,540  lastIndexOf,541  swap16,542  swap32,543  swap64,544  toBuffer,545  toString,546  write,547  readDoubleBE,548  readDoubleLE,549  readFloatBE,550  readFloatLE,551  readInt32BE,552  readInt32LE,553  readUInt32BE,554  readUInt32LE,555  writeDoubleBE,556  writeDoubleLE,557  writeFloatBE,558  writeFloatLE,559  writeInt32BE,560  writeInt32LE,561  writeUInt32BE,562  writeUInt32LE563}564 
basant307/AI_Governance_Project · CoolFace