๐Number+ext
๐ง under construction
Last updated
Was this helpful?
๐ง under construction
Last updated
Was this helpful?
Was this helpful?
JS โฉ value โฉ primitive โฉ number โฉ +ext
extend Number with custom methods.
๐ custom
replit โฉ Number extension (2)
// 2023.03.04 - 06:50 (+) Quaternion, n.asQuaternion
// 2023.03.01 - 13:30 (/) isEqualTo -> equal, Number.EPSILON -> 1e-10
// (-) areEqualNumbers()
// (+) .round()
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------
const { Random } = require('./Random.js');
const {
0 (โข) first version
1 (+) .clamp(), .isEqualTo()
2 (/) .isEqualTo() renamed to .equal()
3 (/) Number.EPSILON -> 1e-10
// ----------------------------------
// Number.prototype extension
// ----------------------------------
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// โญ multiple of
Object.defineProperties(Number.prototype, {
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
});
// โญ general function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// CommonJS export
module.exports = {};
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo
// -----------------------------------------------------
// โญ Number+ext
// -----------------------------------------------------
// (integer-related)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .isEqualTo()
//
Object.defineProperties(Number.prototype, {
// ---------------
// integer-related
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .isEqualTo()
isEqualTo: {
value: function(n, {threshold = Number.EPSILON}={}){
return areEqualNumbers(this, n, {threshold});
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// areEqualNumbers(x, y {threshold})
function areEqualNumbers(x, y, {threshold = Number.EPSILON}={}) {
return Math.abs(x - y) < threshold;
}
// โญ export
module.exports = {}; // CommonJS export
// export default {}; // ES module export
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------
// โญ Number+ext
// -----------------------------------------------------
// (constants)
// - deg // degree = PI/180
// -----------------------------------------------------
// (integer-related)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .isEqualTo()
//
Object.defineProperties(Number.prototype, {
// ---------------
// integer-related
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .isEqualTo()
isEqualTo: {
value: function(n, {threshold = Number.EPSILON}={}){
return areEqualNumbers(this, n, {threshold});
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// areEqualNumbers(x, y {threshold})
function areEqualNumbers(x, y, {threshold = Number.EPSILON}={}) {
return Math.abs(x - y) < threshold;
}
// deg
const deg = Math.PI / 180;
// โญ export
module.exports = {deg}; // CommonJS export
// export {deg}; // ES module export
// 2023.03.01 - 13:30 (/) isEqualTo -> equal, Number.EPSILON -> 1e-10
// (-) areEqualNumbers()
// (+) .round()
// 2023.01.20 - 16:45 (+) deg
// 2023.01.20 - 11:47 (+) .clamp(), .isEqualTo (*first recorded)
// -----------------------------------------------------
const {abs} = Math;
const {log} = console;
// deg
const deg = Math.PI / 180;
// โญ Number+ext
// -----------------------------------------------------
// (constants)
// - deg // degree = PI/180
// -----------------------------------------------------
// (integer-related)
// ๐ธ n.isEven
// ๐ธ n.isOdd
// ๐ธ n.binary
// ๐ธ n.octal
// ๐ธ n.hex
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
// ๐น n.toOctal({prefix:, pad: {length:, char:})
// ๐น n.toHex({prefix:, pad: {length:, char:})
// -----------------------------------------------------
// ๐น .clamp()
// ๐น .equal()
//
Object.defineProperties(Number.prototype, {
// ---------------
// integer-related
// ---------------
// ๐ธ n.isEven
isEven: {
get() { return this % 2 === 0 },
},
// ๐ธ n.isOdd
isOdd: {
get() { return Math.abs(this % 2) === 1 },
},
// ๐ธ n.binary
binary: {
get() { return this.toString(2) },
},
// ๐ธ n.octal
octal: {
get() { return this.toString(8) },
},
// ๐ธ n.hex
hex: {
get() { return this.toString(16).toUpperCase() },
},
// ๐น n.toBinary({prefix:, pad: {length:, char:}})
toBinary: {
value: formatFunc('0b', 2),
},
// ๐น n.toOctal({prefix:, pad: {length:, char:}})
toOctal: {
value: formatFunc('0o', 8),
},
// ๐น n.toHex({prefix:, pad: {length:, char:}})
toHex: {
value: formatFunc('0x', 16),
},
// ---------------
// range
// ---------------
// ๐น .clamp(min, max)
clamp: {
value: function(min, max){
if (min > max) [min, max] = [max, min];
return Math.min(max, Math.max(min, this));
},
},
// ๐น .equal()
equal: {
value: function(y, {threshold=1e-10}={}){
return abs(this - y) < threshold;
},
},
// ๐น .round()
round: {
value: function(places){
return +this.toFixed(places);
},
},
});
// โญ helpers
// -----------------------------------------------------
// general format function
function formatFunc(pre, base) {
return function format({
prefix = pre,
pad = { length: 0, char: '0' },
} = {}) {
// โญ `pad` might be overridden, better destructure again
const { length = 0, char = '0' } = pad;
return prefix +
this.toString(base).padStart(length, char).toUpperCase();
}
}
// โญ export
module.exports = {deg}; // CommonJS export
// export {deg}; // ES module export