Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Last active January 2, 2020 23:45
Show Gist options
  • Save cxmeel/c9d371bbd20e7e12762b548f58d6ddad to your computer and use it in GitHub Desktop.
Save cxmeel/c9d371bbd20e7e12762b548f58d6ddad to your computer and use it in GitHub Desktop.
Convert between Roblox units.
class RobloxUnit {
constructor(value = new String) {
if (typeof(value) === 'number') value = `${value}s`;
if (typeof(value) !== 'string') throw new Error('Value must be a string with format [number][unit]; e.g. 20s');
console.info(`Got "${value}"`);
const __ov = value;
value = __ov.replace(/[A-Za-z"']+/gi, '');
let unit = __ov.replace(/[^A-Za-z"']+/gi, '');
if (value === undefined || unit === undefined) {
throw new Error(`No ${!!value ? 'unit of measurement' : 'numerical value'} found`);
}
value = Number(value);
unit = unit.toUpperCase();
const unitMatch = (unit = 'M', units = ['M']) => {
for (let checkUnit of units) {
if (String(checkUnit).trim().toUpperCase() === String(unit).trim().toUpperCase()) {
return true;
}
}
}
if (unitMatch(unit, ['M', 'METERS', 'METRES'])) this.studs = 20 * value;
if (unitMatch(unit, ['KM', 'KILOMETERS', 'KILOMETRES'])) this.studs = 20 * (value * 1000);
if (unitMatch(unit, ['CM', 'CENTIMETERS', 'CENTIMETRES'])) this.studs = (20 / 100) * value;
if (unitMatch(unit, ['MM', 'MILLIMETERS', 'MILLIMETRES'])) this.studs = (20 / 100) * (value / 10);
if (unitMatch(unit, ['V', 'VOX', 'VOXEL', 'VOXELS'])) this.studs = 4 * value;
if (unitMatch(unit, ['S', 'STUD', 'STUDS'])) this.studs = value;
if (unitMatch(unit, ['IN', 'INCH', 'INCHES', '"'])) this.studs = ((20 / 100) * value) * 2.54;
if (unitMatch(unit, ['FT', 'FEET', 'FOOT', "'"])) this.studs = (((20 / 100) * value) * 2.54) * 12;
if (unitMatch(unit, ['MI', 'MILE', 'MILES'])) this.studs = ((((20 / 100) * value) * 2.54) * 12) * 5280;
if (unitMatch(unit, ['YD', 'YARD', 'YARDS'])) this.studs = ((((20 / 100) * value) * 2.54) * 12) * 3;
this.s = this.stud = this.studs;
this.voxels = this.v = this.vox = this.s / 4;
this.meters = this.metres = this.m = this.s / 20;
this.km = this.kilometers = this.kilometres = this.meters / 1000;
this.centimeters = this.centimetres = this.cm = this.m * 100;
this.mm = this.millimeters = this.millimetres = this.cm * 10;
this.in = this.inch = this.inches = this['"'] = this.cm / 2.54;
this.ft = this.foot = this.feet = this["'"] = this.in / 12;
this.mi = this.mile = this.miles = this.ft / 5280;
this.yd = this.yard = this.yards = this.ft / 3;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment