This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement randomHexColor and isHexColor utils
- Loading branch information
Ray Foo
committed
Aug 27, 2022
1 parent
77fcc0f
commit 193bafc
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@busyxiang/utilities-js': patch | ||
--- | ||
|
||
Implement randomHexColor and isHexColor util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { randomHexColor, isHexColor } from './color'; | ||
|
||
describe('isHexColor', () => { | ||
test('it should return true if is valid hex color', () => { | ||
const isValid = isHexColor('#e34155'); | ||
|
||
expect(isValid).toBe(true); | ||
}); | ||
|
||
test('it should return false if is invalid input', () => { | ||
const isValid = isHexColor('color'); | ||
|
||
expect(isValid).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('randomHexColor', () => { | ||
test('it should return a valid hex color', () => { | ||
const regex = /^#[0-9A-F]{6}$/i; | ||
|
||
for (let i = 0; i < 10; i++) { | ||
const color = randomHexColor(); | ||
|
||
expect(regex.test(color)).toBe(true); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const randomHexColor = () => { | ||
const code = (Math.random() * 0xfffff * 1000000).toString(16); | ||
return `#${code.slice(0, 6)}`; | ||
}; | ||
|
||
export const isHexColor = (hexColor: string) => { | ||
const regex = /^#[0-9A-F]{6}$/i; | ||
|
||
return regex.test(hexColor); | ||
}; |