Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jornatf committed Nov 29, 2023
2 parents cac72ae + c73d04e commit 3dfaa08
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ shuffle([1, 2, 3, 4]) // [3, 1, 4, 2]
Returns a random number between `min` and `max`.

```javascript
randBetween(5,10) // 8
randBetween(5, 10) // 8
```

## Changelog
Expand Down
8 changes: 4 additions & 4 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const keyExists = (key: string, collection: any): boolean => {
* @param arr Array
* @returns Key
*/
export const firstKey = (arr: any[]): string|number|null => {
export const firstKey = (arr: any[]): string | number | null => {
return Array.isArray(arr) && arr.length > 0 ? arr[0] : null
}

Expand All @@ -51,7 +51,7 @@ export const firstKey = (arr: any[]): string|number|null => {
* @param arr Array
* @returns Key
*/
export const lastKey = (arr: any[]): string|number|null => {
export const lastKey = (arr: any[]): string | number | null => {
return Array.isArray(arr) && arr.length > 0 ? arr[arr.length - 1] : null
}

Expand Down Expand Up @@ -99,8 +99,8 @@ export const isEmpty = (arr: any[]): boolean => {
export const shuffle = (arr: any[]) => {
const shuffled = [...arr]
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]
const j = Math.floor(Math.random() * (i + 1))
;[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]
}
return shuffled
}
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import {
shuffle,
} from './array'

import {
randBetween,
} from './number'
import { randBetween } from './number'

export {
toCamel,
Expand All @@ -47,5 +45,5 @@ export {
containsAll,
isEmpty,
shuffle,
randBetween
randBetween,
}
2 changes: 1 addition & 1 deletion src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/
export const randBetween = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min)) + min
}
}
13 changes: 8 additions & 5 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import * as crypto from 'node:crypto'
* @returns Slugified string
*/
export const slugify = (str: string, sep: string = '_'): string => {
return str
.toLowerCase()
.replace(/ /g, sep)
return str.toLowerCase().replace(/ /g, sep)
}

/**
Expand Down Expand Up @@ -86,7 +84,8 @@ export const limitStr = (str: string, maxLength: number = 20): string => {
*/
export const randomStr = (length: number = 10): string => {
let result = ''
const char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const char =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charLength = char.length
for (let i = 0; i < length; i++) {
result += char.charAt(Math.floor(Math.random() * charLength))
Expand All @@ -103,7 +102,11 @@ export const randomStr = (length: number = 10): string => {
* @param str Target
* @returns New string
*/
export const replaceStr = (search: string, replacement: string, str: string): string => {
export const replaceStr = (
search: string,
replacement: string,
str: string
): string => {
return str.split(search).join(replacement)
}

Expand Down
10 changes: 4 additions & 6 deletions tests/number.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
randBetween
} from '../src/number'
import { randBetween } from '../src/number'

describe('randBetween()', () => {
test('returns "true" if output is a number', () => {
const output = randBetween(0,5)
const output = randBetween(0, 5)
const isNumber = typeof output == 'number'
expect(isNumber).toBe(true)
})

test('returns "true" if the random number is between two intervals', () => {
const output = randBetween(0,5)
const output = randBetween(0, 5)
const validated = output >= 0 && output <= 5
expect(validated).toBe(true)
})
})
})

0 comments on commit 3dfaa08

Please sign in to comment.