From 48fadae13e08fbc0bf85ed026d6e013987b838cd Mon Sep 17 00:00:00 2001 From: Piotr Kulpinski Date: Fri, 21 Jun 2024 15:11:47 +0200 Subject: [PATCH] add function to get random element from an array --- bun.lockb | Bin 8318 -> 9772 bytes package.json | 4 ++-- src/index.ts | 1 + src/numbers/numbers.test.ts | 2 +- src/random/random.test.ts | 10 +++++++++- src/random/random.ts | 11 +++++++++++ tsconfig.json | 4 +--- 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/bun.lockb b/bun.lockb index dbb396cc07d74e70663cb7b6797c6bc24db85258..e1445564ded4204083e52bc06ccb03115b820afd 100755 GIT binary patch delta 2082 zcmcgtZBP?O7~Z>FFa%8kByd0k35bf#B>|%#gdlCFDlL+!kP41Wt*B5$1r!upqNNp4 zr;ruvs2#x%#IF{LGD8JFIuym~DD6yV%8X*DA86I;DA)|0*msYdhKR~#Kadpk%q;z}-SGad}C{^ zVRRf@TQ)U(5`~aILN-4jRDcc`D*?L!LjZGXFZwT+f|Dge$Q!W9jb8)$z&MOSXguIM zkU$1#6(ckeFcFX>hy-K-Wq<*ILl7SXcndHPup5x@%?u8vGN)egn%0@B-~6Kd(TZzT zet&2V{j_Jpf&8EIYIRvVqt2M(y;mo8KeX&Ui_54QDi3?pW@;av2X+8&0~?Ebz;40b zA~Q9F=Yf42Zv)$adqigM$23B5u$Bm0=vls#VFyMS!A=qBG`>hp!8WnC0*KijgoF@w zTH@OR#0(%L5DnWwQh~VZo&hIE4g>iJ#B|u*9GF3(gQ1Jv1ig;I)l8DO1c*g=CzBw) z23`j5X}l0xz~qd>nmj9Ip1N&Lx}hg$}zUHRU%&-iSPh z+VYJcd3`7m-~Kb=cCYOJXQfu>xE-R3BZ@cWYeh{j|Gss!^PaZr$oBS-rJGi@jXXOw z(tqN|XVtJF+; zghcLYAGREd&uFk739o9olwB|J`|OJf)46hnz1{M{Bku%+Duld<>4-C#4m{ufd;ZbJ z!MS%v8s9H$Qx@7%4t6Ctn-^YgoRj#XX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}g?jvyqq6X>&B(lnRpc z_{e~Rd61=97QM*q#4JSIY=Y6u%Htu=qNfp1WLcENirFHzm~EoNfgUBZaYW)!rWd*; z=&{U$amyJqJ7>s)0=K86YI|iBoVVJjAMRRf(nIYK*`_F2nx+bl`cq_Qnw&yH4N<++ zI%1go>coswPQe5jgw3%Qs-mq0uYz?!`#?;3_pgF}4Ry*HvJ=nJ#e-~<7KhqG_w(u% zNA{cMnSxN`JZ;Z&*?gI|W->$r$83@<)M>8dd%~`Pxnna0vIaVBQbMRT3op4(b*%i7 zFjG)B?Nv8)cgTg^a8^G diff --git a/package.json b/package.json index 40e4085..380cd91 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@curiousleaf/utils", "description": "A lightweight set of utilities", - "version": "1.0.27", + "version": "1.0.28", "license": "MIT", "type": "module", "author": { @@ -26,7 +26,7 @@ "devDependencies": { "@babel/runtime": "^7.23.8", "@biomejs/biome": "^1.7.1", - "bun-types": "latest", + "@types/bun": "^1.1.2", "prettier": "^3.1.1", "typescript": "^5.3.3" }, diff --git a/src/index.ts b/src/index.ts index 25797fc..847a904 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ // External exports export * from "scule" +export * from "@uiw/color-convert" // Internal exports export * from "./colors/colors" diff --git a/src/numbers/numbers.test.ts b/src/numbers/numbers.test.ts index 651c2d0..4b75588 100644 --- a/src/numbers/numbers.test.ts +++ b/src/numbers/numbers.test.ts @@ -34,7 +34,7 @@ describe("parseNumericValue", () => { }) it("returns the original string if it cannot be parsed", () => { - expect(parseNumericValue("not a number")).toBe(undefined) + expect(parseNumericValue("not a number")).toBeUndefined() }) }) diff --git a/src/random/random.test.ts b/src/random/random.test.ts index 3d2422c..01d72b7 100644 --- a/src/random/random.test.ts +++ b/src/random/random.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "bun:test" -import { getRandomColor, getRandomNumber, getRandomProperty, getRandomString } from "./random" +import { getRandomColor, getRandomElement, getRandomNumber, getRandomProperty, getRandomString } from "./random" describe("getRandomColor", () => { it("returns a string", () => { @@ -43,6 +43,14 @@ describe("getRandomNumber", () => { }) }) +describe("getRandomElement", () => { + it("returns a value from the array", () => { + const array = [1, 2, 3] + const result = getRandomElement(array) + expect([1, 2, 3]).toContain(result) + }) +}) + describe("getRandomProperty", () => { it("returns a value from the object", () => { const obj = { a: 1, b: 2, c: 3 } diff --git a/src/random/random.ts b/src/random/random.ts index 7e3520c..19cc1e7 100644 --- a/src/random/random.ts +++ b/src/random/random.ts @@ -31,6 +31,17 @@ export const getRandomNumber = (min: number, max: number) => { return Math.floor(Math.random() * (max - min + 1)) + min } +/** + * Returns a random element from an array. + * + * @param array - The array to get a random element from. + * @returns A random element from the array. + */ +export const getRandomElement = (array: T[]): T => { + const index = Math.floor(Math.random() * array.length) + return array[index]! +} + /** * Returns a random property value from an object. * @param obj - The object to get a random property value from. diff --git a/tsconfig.json b/tsconfig.json index 31e921e..7b4f9f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -103,10 +103,8 @@ "allowUnreachableCode": true /* Disable error reporting for unreachable code. */, /* Completeness */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */, + "skipLibCheck": true /* Skip type checking all .d.ts files. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - - "types": ["bun-types"] /* Add Bun global */ }, "include": ["src"],