Skip to content

Instantly share code, notes, and snippets.

@mrampazz
Created September 6, 2020 08:32
Show Gist options
  • Save mrampazz/0f56577d5bb5809c369b9246aee63d5b to your computer and use it in GitHub Desktop.
Save mrampazz/0f56577d5bb5809c369b9246aee63d5b to your computer and use it in GitHub Desktop.
Small JS script to convert steamIDs
const BASE_NUM = 76561197960265728n;
const REGEX_STEAMID64 = /^[0-9]{17}$/;
const REGEX_STEAMID = /^STEAM_[0-5]:[01]:\d+$/;
const REGEX_STEAMID3 = /^\[U:1:[0-9]+\]$/;
const isSteamID64 = (id) => {
if (!id) {
return false;
}
return REGEX_STEAMID64.test(id);
};
const isSteamID = (id) => {
if (!id) {
return false;
}
return REGEX_STEAMID.test(id);
};
const isSteamID3 = (id) => {
if (!id) {
return false;
}
return REGEX_STEAMID3.test(id);
};
const sIDtosID64 = (sID) => {
console.log("Converting steamID to steamID64");
let split = sID.split(":");
let v = BASE_NUM;
let z = BigInt(split[2]);
let y = BigInt(split[1]);
if (z && y) {
return (v + z * 2n + y).toString();
} else {
return false;
}
};
const sID64tosID = (id) => {
console.log("Converting steamID64 to steamID");
let userID = BigInt(id);
let baseID = BASE_NUM;
let y = (userID % 2n).toString();
let z = Math.floor((userID - baseID).toString() / 2).toString();
return `STEAM_0:${y}:${z}`;
};
const sIDtosID3 = (sID) => {
console.log("Converting steamID to steamID3");
let split = sID.split(":");
return `[U:1:${parseInt(split[1]) + parseInt(split[2]) * 2}]`;
};
const sID3tosID = (id) => {
var split = id.split(":");
var last = split[2].substring(0, split[2].length - 1);
return `STEAM_0:${last % 2}:${Math.floor(last / 2)}`;
};
const fromSteamID = (id) => {
return {
steamID64: sIDtosID64(id),
steamID: id,
steamID3: sIDtosID3(id),
};
};
const fromSteamID64 = (id) => {
const steamID = sID64tosID(id);
return {
steamID64: id,
steamID: steamID,
steamID3: sIDtosID3(steamID),
};
};
const fromSteamID3 = (id) => {
const steamID = sID3tosID(id);
return {
steamID64: sIDtosID64(steamID),
steamID: steamID,
steamID3: id,
};
};
export default (id) => {
if (isSteamID(id)) return fromSteamID(id);
if (isSteamID64(id)) return fromSteamID64(id);
if (isSteamID3(id)) return fromSteamID3(id);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment