UNPKG

12.1 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.js","sources":["../src/lib/constants.ts","../src/lib/types.ts","../src/lib/chance.ts","../src/lib/percent.ts","../src/lib/array.ts","../src/lib/number.ts","../src/lib/other/debounce.ts","../src/lib/other/deepClone.ts","../src/lib/general.ts","../src/lib/object.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","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","/**\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 * 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 * 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","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 { 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","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 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"],"names":["primitiveTypes","isObject","input","constructor","Object","isPrimitive","includes","randInt","min","max","Math","floor","random","randFloat","percent","valueToCalc","partialValue","totalValue","array","chunkSize","RangeError","Number","isInteger","TypeError","clone","slice","chunks","length","push","splice","val","fn","waitFor","timeout","args","clearTimeout","setTimeout","deepClone","source","Array","isArray","output","value","key","entries","Map","set","Set","values","add","obj","keys","arr","filter","firstArray","secondArray","item","upperLimit","precision","multiplier","pow","round","num","inMin","inMax","outMin","outMax","copy","i","j","ms","Promise","resolve","reduce","a","b"],"mappings":"0EAUaA,EAAiB,CAAC,SAAU,SAAU,SAAU,oBCH7CC,EAASC,GACxB,QAAwB,iBAAVA,IAAsBA,IAAQA,EAAMC,cAAgBC,gBAOnDC,EAAYH,GAC3B,OAAOF,EAAeM,gBAAgBJ,YCVvBK,EAAQC,EAAaC,GACpC,OAAOC,KAAKC,MAAMD,KAAKE,UAAYH,EAAMD,EAAM,GAAKA,YAUrCK,EAAUL,EAAaC,GACtC,OAAOC,KAAKE,UAAYH,EAAMD,GAAOA,oCCJLM,EAAiBC,GACjD,OAAQD,EAAUC,EAAe,sCAVFC,EAAsBC,GACrD,OAAQ,IAAMD,EAAgBC,0BCONC,EAAqBC,GAC7C,GAAIA,EAAY,EAAG,MAAM,IAAIC,WAAW,mCACxC,IAAKC,OAAOC,UAAUH,GAAY,MAAM,IAAII,UAAU,iCACtD,MAAMC,EAAaN,EAAMO,QACnBC,EAAgB,GACtB,KAAOF,EAAMG,QAAQD,EAAOE,KAAKJ,EAAMK,OAAO,EAAGV,IACjD,OAAOO,0BCRcI,EAAatB,EAAaC,GAC/C,OAAOC,KAAKF,IAAIC,EAAKC,KAAKD,IAAID,EAAKsB,sBCZZ,CAAoCC,EAAOC,KAClE,IAAIC,EAAgD,KAUpD,MARkB,IAAIC,KACL,OAAZD,IACHE,aAAaF,GACbA,EAAU,MAEXA,EAAUG,WAAW,IAAML,KAAMG,GAAOF,gCCF1BK,EAAaC,GAE5B,GAAe,OAAXA,GAAmBjC,EAAYiC,GAAS,OAAOA,EACnD,GAAIC,MAAMC,QAAQF,GAAS,CAC1B,MAAMG,EAAU,GAChB,IAAK,MAAMC,KAASJ,EAAQG,EAAOb,KAAKS,EAAUK,IAClD,OAAQD,EAET,GAAIxC,EAASqC,GAAS,C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