UNPKG

12.5 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.es.mjs","sources":["../src/lib/constants.ts","../src/lib/object.ts","../src/lib/general.ts","../src/lib/other/debounce.ts","../src/lib/types.ts","../src/lib/other/deepClone.ts","../src/lib/percent.ts","../src/lib/array.ts","../src/lib/chance.ts","../src/lib/number.ts"],"sourcesContent":["export const enum Time {\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = 1000 * 60,\n\tHour = 1000 * 60 * 60,\n\tDay = 1000 * 60 * 60 * 24,\n\tMonth = 1000 * 60 * 60 * 24 * 30,\n\tYear = 1000 * 60 * 60 * 24 * 365\n}\n\nexport const primitiveTypes = ['string', 'bigint', 'number', 'boolean'];\n","export function objectEntries<T extends Record<PropertyKey, unknown>>(obj: T) {\n\treturn Object.entries(obj) as [keyof T, T[keyof T]][];\n}\n\nexport function objectValues<T extends Record<PropertyKey, unknown>>(obj: T) {\n\treturn Object.values(obj) as T[keyof T][];\n}\n\nexport function objectKeys<T extends Record<PropertyKey, unknown>>(obj: T) {\n\treturn Object.keys(obj) as (keyof T)[];\n}\n","export function notEmpty<TValue>(value: TValue | null | undefined): value is TValue {\n\treturn value !== null && value !== undefined;\n}\n\nexport function sleep(ms: number) {\n\treturn new Promise((resolve) => setTimeout(resolve, ms));\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nexport function noOp() {}\n","export const debounce = <F extends (...args: any[]) => any>(fn: F, waitFor: number) => {\n\tlet timeout: ReturnType<typeof setTimeout> | null = null;\n\n\tconst debounced = (...args: Parameters<F>) => {\n\t\tif (timeout !== null) {\n\t\t\tclearTimeout(timeout);\n\t\t\ttimeout = null;\n\t\t}\n\t\ttimeout = setTimeout(() => fn(...args), waitFor);\n\t};\n\n\treturn debounced as (...args: Parameters<F>) => ReturnType<F>;\n};\n","import { primitiveTypes } from './constants';\n\n/**\n * Verify if the input is an object literal (or class).\n * @param input The object to verify\n */\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function isObject(input: unknown): input is Record<PropertyKey, unknown> | object {\n\treturn typeof input === 'object' && input ? input.constructor === Object : false;\n}\n\n/**\n * Check whether a value is a primitive\n * @param input The input to check\n */\nexport function isPrimitive(input: unknown): input is string | bigint | number | boolean {\n\treturn primitiveTypes.includes(typeof input);\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function isFunction(input: unknown): input is Function {\n\treturn typeof input === 'function';\n}\n","import { isObject, isPrimitive } from '../types';\n\n/**\n * Deep clone an object\n * @param source The object to clone\n */\nexport function deepClone<T>(source: T): T {\n\t// Check if it's a primitive (with exception of function and null, which is typeof object)\n\tif (source === null || isPrimitive(source)) return source;\n\tif (Array.isArray(source)) {\n\t\tconst output = ([] as unknown) as T & T extends (infer S)[] ? S[] : never;\n\t\tfor (const value of source) output.push(deepClone(value));\n\t\treturn (output as unknown) as T;\n\t}\n\tif (isObject(source)) {\n\t\tconst output = {} as Record<PropertyKey, unknown>;\n\t\tfor (const [key, value] of Object.entries(source)) output[key] = deepClone(value);\n\t\treturn (output as unknown) as T;\n\t}\n\tif (source instanceof Map) {\n\t\tconst output = (new (source.constructor as MapConstructor)() as unknown) as T & T extends Map<infer K, infer V> ? Map<K, V> : never;\n\t\tfor (const [key, value] of source.entries()) output.set(key, deepClone(value));\n\t\treturn (output as unknown) as T;\n\t}\n\tif (source instanceof Set) {\n\t\tconst output = (new (source.constructor as SetConstructor)() as unknown) as T & T extends Set<infer K> ? Set<K> : never;\n\t\tfor (const value of source.values()) output.add(deepClone(value));\n\t\treturn (output as unknown) as T;\n\t}\n\treturn source;\n}\n","/**\n * Shows what percentage a value is of a total value, for example calculating what percentage of 20 is 5? (25%)\n * @param partialValue The partial value of the total number, that you want to know what its percentage of the total is.\n * @param totalValue The total value, that the partial value is a part of.\n */\nexport function calcWhatPercent(partialValue: number, totalValue: number): number {\n\treturn (100 * partialValue) / totalValue;\n}\n\n/**\n * Calculates what a X% of a total number is, for example calculating what is 20% of 100\n * @param percent The percentage (%) you want to calculate.\n * @param valueToCalc The total number that you want to get the percentage of.\n */\nexport function calcPercentOfNum(percent: number, valueToCalc: number): number {\n\treturn (percent * valueToCalc) / 100;\n}\n\n/**\n * Reduces a number by a percentage of itself.\n * @param value, The number to be reduced.\n * @param percent The percent you want the value to be reduced by.\n */\nexport function reduceNumByPercent(value: number, percent: number): number {\n\tif (percent <= 0) return value;\n\treturn value - value * (percent / 100);\n}\n\n/**\n * Increases a number by a percentage of itself.\n * @param value, The number to be increased.\n * @param percent The percent you want the value to be increased by.\n */\nexport function increaseNumByPercent(value: number, percent: number): number {\n\tif (percent <= 0) return value;\n\treturn value + value * (percent / 100);\n}\n","/**\n * Picks a random item from an array.\n * @param array The array to pick from.\n */\nexport function randArrItem<T>(array: readonly T[]): T {\n\treturn array[Math.floor(Math.random() * array.length)];\n}\n\n/**\n * Splits up an array into chunks\n * @param array The array to chunk up\n * @param chunkSize The size of each individual chunk\n */\nexport function chunk<T>(array: readonly T[], chunkSize: number): T[][] {\n\tif (chunkSize < 1) throw new RangeError('chunkSize must be 1 or greater.');\n\tif (!Number.isInteger(chunkSize)) throw new TypeError('chunkSize must be an integer.');\n\tconst clone: T[] = array.slice();\n\tconst chunks: T[][] = [];\n\twhile (clone.length) chunks.push(clone.splice(0, chunkSize));\n\treturn chunks;\n}\n\n/**\n * Returns a copy of an array with duplicates removed.\n *\n * @param arr The array to copy and remove duplicates from.\n */\nexport function uniqueArr<T>(arr: readonly T[]): T[] {\n\treturn [...new Set(arr)];\n}\n\n/**\n * Returns the sum of an array of numbers.\n *\n * @param arr The array of numbers to sum.\n */\nexport function sumArr(arr: readonly number[]) {\n\treturn arr.reduce((a, b) => a + b, 0);\n}\n\n/**\n * Returns a shuffled copy of an array.\n *\n * @param array The array to shuffle.\n */\nexport function shuffleArr<T>(array: readonly T[]): T[] {\n\tlet copy = [...array];\n\tfor (let i = copy.length - 1; i > 0; i--) {\n\t\tconst j = Math.floor(Math.random() * (i + 1));\n\t\t[copy[i], copy[j]] = [copy[j], copy[i]];\n\t}\n\treturn copy;\n}\n\n/**\n *\n * @param arr The array to partition\n * @param filter The filter by which to partition the array\n */\nexport function partition<T>(arr: T[], filter: (item: T) => boolean): [T[], T[]] {\n\tconst firstArray: T[] = [];\n\tconst secondArray: T[] = [];\n\tfor (const item of arr) {\n\t\t(filter(item) ? firstArray : secondArray).push(item);\n\t}\n\treturn [firstArray, secondArray];\n}\n","/**\n * Rolls a random number inclusively between a min and max.\n *\n * @param min The lower limit of the roll\n * @param max The upper limit of the roll\n */\nexport function randInt(min: number, max: number): number {\n\treturn Math.floor(Math.random() * (max - min + 1) + min);\n}\n\n/**\n * Rolls a random floating point number inclusively between min and max.\n *\n * @param {number} min - min number\n * @param {number} max - max number\n * @return {number} A random float\n */\nexport function randFloat(min: number, max: number): number {\n\treturn Math.random() * (max - min) + min;\n}\n\n/**\n * Rolls a 1 in X chance, returning true on successfull rolls.\n *\n * @param upperLimit The upper limit of the roll\n */\nexport function roll(upperLimit: number): boolean {\n\treturn randInt(1, upperLimit) === 1;\n}\n\n/**\n * Returns true based on a percent chance.\n *\n * @param percent The percent to have a chance of.\n */\nexport function percentChance(percent: number): boolean {\n\treturn randFloat(0, 100) < percent;\n}\n","/**\n * Rounds a number to a given precision.\n *\n * @param value The number to be rounded.\n * @param precision The precision of the rounding.\n */\nexport function round(value: number, precision = 1): number {\n\tconst multiplier = Math.pow(10, precision || 0);\n\treturn Math.round(value * multiplier) / multiplier;\n}\n\nexport function clamp(val: number, min: number, max: number) {\n\treturn Math.min(max, Math.max(min, val));\n}\n\nexport function scaleNumber(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number {\n\treturn ((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;\n}\n"],"names":["primitiveTypes","objectEntries","obj","Object","entries","objectValues","values","objectKeys","keys","notEmpty","value","sleep","ms","Promise","resolve","setTimeout","noOp","debounce","fn","waitFor","timeout","args","clearTimeout","isObject","input","constructor","isPrimitive","includes","isFunction","deepClone","source","Array","isArray","output","push","key","Map","set","Set","add","calcWhatPercent","partialValue","totalValue","calcPercentOfNum","percent","valueToCalc","reduceNumByPercent","increaseNumByPercent","randArrItem","array","Math","floor","random","length","chunk","chunkSize","RangeError","Number","isInteger","TypeError","clone","slice","chunks","splice","uniqueArr","arr","sumArr","reduce","a","b","shuffleArr","copy","i","j","partition","filter","firstArray","secondArray","item","randInt","min","max","randFloat","roll","upperLimit","percentChance","round","precision","multiplier","pow","clamp","val","scaleNumber","num","inMin","inMax","outMin","outMax"],"mappings":"MAUaA,EAAiB,CAAC,SAAU,SAAU,SAAU,oBCV7CC,EAAsDC,GACrE,OAAOC,OAAOC,QAAQF,YAGPG,EAAqDH,GACpE,OAAOC,OAAOG,OAAOJ,YAGNK,EAAmDL,GAClE,OAAOC,OAAOK,KAAKN,YCTJO,EAAiBC,GAChC,OAAOA,MAAAA,WAGQC,EAAMC,GACrB,OAAO,IAAIC,QAASC,GAAYC,WAAWD,EAASF,aAIrCI,WCTHC,EAAW,CAAoCC,EAAOC,KAClE,IAAIC,EAAgD,KAUpD,MARkB,IAAIC,KACL,OAAZD,IACHE,aAAaF,GACbA,EAAU,MAEXA,EAAUL,WAAW,IAAMG,KAAMG,GAAOF,cCD1BI,EAASC,GACxB,QAAwB,iBAAVA,IAAsBA,IAAQA,EAAMC,cAAgBtB,gBAOnDuB,EAAYF,GAC3B,OAAOxB,EAAe2B,gBAAgBH,YAIvBI,EAAWJ,GAC1B,MAAwB,mBAAVA,WCfCK,EAAaC,GAE5B,GAAe,OAAXA,GAAmBJ,EAAYI,GAAS,OAAOA,EACnD,GAAIC,MAAMC,QAAQF,GAAS,CAC1B,MAAMG,EAAU,GAChB,IAAK,MAAMvB,KAASoB,EAAQG,EAAOC,KAAKL,EAAUnB,IAClD,OAAQuB,EAET,GAAIV,EAASO,GAAS,CACrB,MAAMG,EAAS,GACf,IAAK,MAAOE,EAAKzB,KAAUP,OAAOC,QAAQ0B,GAASG,EAAOE,GAAON,EAAUnB,GAC3E,OAAQuB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