Utils

fast chunk substr

function chunkSubstr(str, size) {
  const numChunks = Math.ceil(str.length / size)
  const chunks = new Array(numChunks)
 
  for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
    chunks[i] = str.substr(o, size)
  }
 
  return chunks
}

how to use a config file

 
let creds = null;
 
try {
  creds = require("config/config.json");
} catch (e) {
  // ignore
}
 
const dmTarget = creds?.['dmTarget'] ?? "@admin:localhost";
 
const homeserverUrl = creds?.['homeserverUrl'] ?? "http://localhost:8008";
 
const accessToken = creds?.['accessToken'] ?? 'YOUR_TOKEN';
 
process.env["PORT"]

net socket

 
let net = require("node:net")
const port = "8888"
const host = "127.0.0.1"
const client = new net.Socket()
 
client.setKeepAlive(true, 10000)
 
client.connect({ port: port, host: host }, function (args) {
  console.log("Connected")
})
 
client.on("ready", () => {
  console.log("Asking for initial update.")
  emuClient.write("update")
})
 
client.on("data", function (chunk) {
  console.log(`Data received .`)
  let Updated_Emu_Data = chunk.toString()
})
 
client.on("end", function () {
  console.log("Connection ended.")
})
 
client.on("error", function (err) {
    console.error(`There's been an error, `, err)
})
 
start()

simple unique ID

export const getUniqueID = () =>  {
  function s4() {
      return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
  return s4() + s4() + '-' + s4();
}

Misc

bitwise XOR from hex string

function bitwiseXorHexString(pinBlock1, pinBlock2) {
  var result = ''
  for (let index = 0; index < 16; index++) {
    const temp = (parseInt(pinBlock1.charAt(index), 16) ^ parseInt(pinBlock2.charAt(index), 16)).toString(16).toUpperCase()
    result += temp
  }
  return result
}

iterate over htmlCollection

let links = document.getElementsByClassName("nav-link")
for (key of list) {
    console.log(key.id);
}