UNPKG

utility-kit

Version:

An easy to use library that provides miscellaneous but very useful functions that are commonly used in most javascript projects.

176 lines (168 loc) 8.21 kB
'use strict'; var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __objRest = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop]; } return target; }; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/cache.ts var cacheKey = Symbol(); function memoize(fn) { const cache = /* @__PURE__ */ new Map(); return (...args) => { let current = cache; for (const arg of args) { if (!current.has(arg)) { const nextLevel = typeof arg === "object" && arg !== null ? /* @__PURE__ */ new WeakMap() : /* @__PURE__ */ new Map(); current.set(arg, nextLevel); } current = current.get(arg); } if (current.has(cacheKey)) return current.get(cacheKey); const result = fn(...args); current.set(cacheKey, result); return result; }; } // src/math.ts var minimumNumber = (array) => Math.min(...array); var maximumNumber = (array) => Math.max(...array); // src/random.ts var adjectives = ["Amazing", "Beautiful", "Calm", "Dangerous", "Enormous", "Fantastic", "Gentle", "Happy", "Invincible", "Jolly", "Kind", "Lovely", "Marvelous", "Naughty", "Observant", "Powerful", "Quiet", "Ruthless", "Sleepy", "Tremendous", "Unique", "Violent", "Wonderful", "Young", "Zestful"]; var animals = ["Albatross", "Alligator", "Ant", "Antelope", "Ape", "Baboon", "Barracuda", "Bat", "Bear", "Beaver", "Bee", "Bison", "Boar", "Buffalo", "Butterfly", "Camel", "Cat", "Caterpillar", "Cattle", "Cheetah", "Chicken", "Chimpanzee", "Clam", "Cobra", "Crab", "Crane", "Crocodile", "Crow", "Curlew", "Deer", "Dinosaur", "Dog", "Dolphin", "Donkey", "Dotterel", "Dove", "Dragonfly", "Duck", "Dugong", "Dunlin", "Eagle", "Elephant", "Emu", "Falcon", "Ferret", "Finch", "Fish", "Flamingo", "Fly", "Fox", "Frog", "Gaur", "Gazelle", "Gerbil", "Giraffe", "Gnat", "Gnu", "Goat", "Goldfinch", "Goldfish", "Goose", "Gorilla", "Goshawk", "Grasshopper", "Grouse", "Guanaco", "Gull", "Hamster", "Hare", "Hawk", "Hedgehog", "Heron", "Herring", "Hippopotamus", "Hornet", "Horse", "Human", "Hummingbird", "Hyena", "Ibex", "Ibis", "Jackal", "Jaguar", "Jay", "Jellyfish", "Kangaroo", "Kingfisher", "Koala", "Kookabura", "Kouprey", "Kudu", "Lapwing", "Lark", "Lemur", "Leopard", "Lion", "Llama", "Lobster", "Locust", "Loris", "Louse", "Lyrebird", "Magpie", "Mallard", "Manatee", "Mandrill", "Mantis", "Marten", "Meerkat", "Mink", "Mole", "Mongoose", "Monkey", "Moose", "Mosquito", "Mouse", "Mule", "Narwhal", "Newt", "Nightingale", "Octopus", "Okapi", "Opossum", "Oryx", "Ostrich", "Otter", "Owl", "Oyster", "Panther", "Parrot", "Partridge", "Peafowl", "Pelican", "Penguin", "Pheasant", "Pig", "Pigeon", "Pony", "Porcupine", "Porpoise", "Quail", "Quelea", "Quetzal", "Rabbit", "Raccoon", "Rail", "Ram", "Rat", "Raven", "Red deer", "Red panda", "Reindeer", "Rhinoceros", "Rook", "Salamander", "Salmon", "Sand Dollar", "Sandpiper", "Sardine", "Scorpion", "Seahorse", "Seal", "Shark", "Sheep", "Shrew", "Skunk", "Snail", "Snake", "Sparrow", "Spider", "Spoonbill", "Squid", "Squirrel", "Starling", "Stingray", "Swallow", "Swan", "Termite", "Tiger", "Toad", "Trout", "Turkey", "Turtle", "Viper", "Vulture", "Wallaby", "Walrus", "Wasp", "Weasel", "Whale", "Wildcat", "Wolf", "Wolverine", "Wombat", "Woodcock", "Woodpecker", "Worm", "Wren", "Yak", "Zebra"]; var randomBytes; if (typeof (crypto == null ? void 0 : crypto.getRandomValues) === "function") { randomBytes = (n) => crypto.getRandomValues(new Uint8Array(n)); } var randomNumber = (min = 0, max = Number.MAX_SAFE_INTEGER) => min + Math.floor(random() * (max - min + 1)); function generateOTP(digits = 4) { digits = Math.min(digits, 20); const max = +"9".repeat(digits); let number = randomNumber(0, max).toString(); number = "0".repeat(digits - number.length) + number; return number; } var probability = (p) => !!p && random() <= p; function random(n = 8) { if (!randomBytes) return Math.random(); n = Math.max(1, Math.min(n, 127)); const bytes = randomBytes(n); const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); const value = BigInt("0x" + hex); const maxValue = BigInt(2) ** BigInt(n * 8); return Number(value) / Number(maxValue); } var randomElement = (array) => array[randomNumber(0, array.length - 1)]; var randomAdjective = () => randomElement(adjectives); var randomAnimal = () => randomElement(animals); var randomName = (separator = " ") => `${randomAdjective()}${separator}${randomAnimal()}`; // src/time.ts var wait = (time) => __async(null, null, function* () { return new Promise((resolve) => setTimeout(resolve, time)); }); // src/utility.ts function retry(callback, _a = {}) { var _b = _a, { retries = 3 } = _b, options = __objRest(_b, ["retries"]); const { onSuccess, onError } = options; const result = tryCatch(callback); if (result.success) { onSuccess == null ? void 0 : onSuccess(result.data); return result; } onError == null ? void 0 : onError(result.error); if (retries > 0) return retry(callback, __spreadValues({ retries: retries - 1 }, options)); return result; } function retryAsync(_0) { return __async(this, arguments, function* (callback, _a = {}) { var _b = _a, { retries = 3, initialDelay = 0 } = _b, options = __objRest(_b, ["retries", "initialDelay"]); const { delayIncrement = 0, onSuccess, onError } = options; const result = yield tryCatchAsync(callback); if (result.success) { yield onSuccess == null ? void 0 : onSuccess(result.data); return result; } onError == null ? void 0 : onError(result.error); if (retries > 0) { yield wait(initialDelay); return yield retryAsync(callback, __spreadValues({ retries: retries - 1, initialDelay: initialDelay + delayIncrement }, options)); } return result; }); } function tryCatch(callback) { try { const data = callback(); return { success: true, data, error: null }; } catch (error) { return { success: false, data: null, error }; } } function tryCatchAsync(callback) { return __async(this, null, function* () { try { const data = yield callback(); return { success: true, data, error: null }; } catch (error) { return { success: false, data: null, error }; } }); } exports.generateOTP = generateOTP; exports.maximumNumber = maximumNumber; exports.memoize = memoize; exports.minimumNumber = minimumNumber; exports.probability = probability; exports.random = random; exports.randomAdjective = randomAdjective; exports.randomAnimal = randomAnimal; exports.randomElement = randomElement; exports.randomName = randomName; exports.randomNumber = randomNumber; exports.retry = retry; exports.retryAsync = retryAsync; exports.tryCatch = tryCatch; exports.tryCatchAsync = tryCatchAsync; exports.wait = wait;