@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
218 lines (209 loc) • 6.61 kB
JavaScript
/*!
* @techmely/utils
* Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
* MIT Licensed
*/
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/number.ts
var number_exports = {};
__export(number_exports, {
CheckBoundary: () => CheckBoundary,
DigitLength: () => DigitLength,
Divide: () => Divide,
EnableBoundaryChecking: () => EnableBoundaryChecking,
Float2Fixed: () => Float2Fixed,
Minus: () => Minus,
Plus: () => Plus,
Round: () => Round,
Strip: () => Strip,
Times: () => Times,
formatLot10Volume: () => formatLot10Volume,
formatNumber: () => formatNumber
});
module.exports = __toCommonJS(number_exports);
// src/convert.ts
function toBoolean(val) {
return val ? val !== "false" : false;
}
// src/env.ts
var import_meta = {};
var _envShim = /* @__PURE__ */ Object.create(null);
var _getEnv = (useShim) => globalThis.process?.env || // Node.js/Bun
import_meta.env || // Vite env
globalThis.Deno?.env.toObject() || // Deno env
globalThis.__env__ || // Custom env
(useShim ? _envShim : globalThis);
var envs = new Proxy(_envShim, {
get(_, prop) {
const env = _getEnv();
return env[prop] ?? _envShim[prop];
},
has(_, prop) {
const env = _getEnv();
return prop in env || prop in _envShim;
},
set(_, prop, value) {
const env = _getEnv(true);
env[prop] = value;
return true;
},
deleteProperty(_, prop) {
if (!prop) {
return false;
}
const env = _getEnv(true);
delete env[prop];
return true;
},
ownKeys() {
const env = _getEnv();
return Object.keys(env);
}
});
var nodeENV = typeof process !== "undefined" && process.env && process.env.NODE_ENV || "";
// src/process.ts
var _process = globalThis.process || /* @__PURE__ */ Object.create(null);
var processShims = {
versions: {}
};
var process2 = new Proxy(_process, {
get(target, prop) {
if (prop === "env") {
return envs;
}
if (prop in target) {
return target[prop];
}
if (prop in processShims) {
return processShims[prop];
}
}
});
var platform = _process.platform || "";
// src/is.ts
var isNumber = (val) => toString.call(val) === "[object Number]";
var isBrowser = typeof window !== "undefined";
var isAndroid = isBrowser ? /(android)/i.test(navigator.userAgent) : false;
var match = isBrowser ? window.matchMedia || window.msMatchMedia : void 0;
var isMobile = isBrowser ? match?.("(pointer:coarse)")?.matches : false;
var isCrawler = isBrowser && (!("onscroll" in window) || /(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent));
var isCI = toBoolean(envs.CI);
var isNodeTest = nodeENV === "test" || toBoolean(envs.TEST);
var isWindows = /^win/i.test(platform);
var isLinux = /^linux/i.test(platform);
var isMacOS = /^darwin/i.test(platform);
// src/number.ts
function formatNumber(num, precision = 0, defaultValue = "-") {
if (!isNumber(num)) {
return defaultValue;
}
return num.toLocaleString("en", {
minimumFractionDigits: precision,
maximumFractionDigits: precision
});
}
function formatLot10Volume(volume, precision = 0, defaultValue = "-") {
if (!isNumber(volume)) {
return defaultValue;
}
return (volume * 10)?.toLocaleString("en", { minimumFractionDigits: precision }).slice(0, -1);
}
function Strip(num, precision = 15) {
return +parseFloat(Number(num).toPrecision(precision));
}
function DigitLength(num) {
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split(".")[1] || "").length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
function Float2Fixed(num) {
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
}
const dLen = DigitLength(num);
const powDLen = 10 ** dLen;
return dLen > 0 ? Strip(Number(num) * powDLen) : Number(num);
}
var _boundaryCheckingState = true;
function CheckBoundary(num) {
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(
`${num} is beyond boundary when transfer to integer, the results may not be accurate`
);
}
}
}
function npCreateOperation(operation) {
return (...nums) => {
const [first, ...others] = nums;
return others.reduce((prev, next) => operation(prev, next), first);
};
}
var Times = npCreateOperation((num1, num2) => {
const num1Changed = Float2Fixed(num1);
const num2Changed = Float2Fixed(num2);
const baseNum = DigitLength(num1) + DigitLength(num2);
const leftValue = num1Changed * num2Changed;
CheckBoundary(leftValue);
const baseNumPow = 10 ** baseNum;
return leftValue / baseNumPow;
});
var Plus = npCreateOperation((num1, num2) => {
const baseNum = 10 ** Math.max(DigitLength(num1), DigitLength(num2));
return (Times(num1, baseNum) + Times(num2, baseNum)) / baseNum;
});
var Minus = npCreateOperation((num1, num2) => {
const baseNum = 10 ** Math.max(DigitLength(num1), DigitLength(num2));
return (Times(num1, baseNum) - Times(num2, baseNum)) / baseNum;
});
var Divide = npCreateOperation((num1, num2) => {
const num1Changed = Float2Fixed(num1);
const num2Changed = Float2Fixed(num2);
CheckBoundary(num1Changed);
CheckBoundary(num2Changed);
return Times(num1Changed / num2Changed, Strip(10 ** (DigitLength(num2) - DigitLength(num1))));
});
function Round(num, decimal) {
const base = 10 ** decimal;
let result = Divide(Math.round(Math.abs(Times(num, base))), base);
if (isNumber(num) && num < 0 && result !== 0) {
result = Times(result, -1);
}
return result;
}
function EnableBoundaryChecking(flag = true) {
_boundaryCheckingState = flag;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CheckBoundary,
DigitLength,
Divide,
EnableBoundaryChecking,
Float2Fixed,
Minus,
Plus,
Round,
Strip,
Times,
formatLot10Volume,
formatNumber
});