Skip to content

Commit

Permalink
add function to get random element from an array
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski committed Jun 21, 2024
1 parent 285408d commit 48fadae
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// External exports
export * from "scule"
export * from "@uiw/color-convert"

// Internal exports
export * from "./colors/colors"
Expand Down
2 changes: 1 addition & 1 deletion src/numbers/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

Expand Down
10 changes: 9 additions & 1 deletion src/random/random.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions src/random/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(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.
Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 48fadae

Please sign in to comment.