|
1 | 1 | import type { IColorResultOptions, Palette } from './types'; |
| 2 | +import { isColor } from './utils'; |
2 | 3 |
|
3 | 4 | export const initalOptions: IColorResultOptions = { |
4 | 5 | mainShade: 500, |
5 | 6 | primaryColor: '#FFBD00', |
6 | 7 | shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900], |
7 | 8 | }; |
8 | 9 |
|
9 | | -export const isColor = (color: string) => { |
10 | | - const reg = /^#([\da-f]{3}){1,2}$|^#([\da-f]{6}){1,2}$|(rgb|hsl)a?\((\s*-?\d+%?\s*,){2}(\s*-?\d+%?\s*,?\s*\)?)(,\s*(0?\.\d+)?|1)?\)/gim; |
11 | | - if (typeof color === 'string' && reg.test(color)) { |
12 | | - return true; |
13 | | - } else { |
14 | | - return false; |
| 10 | +export const checkParam = (palette: Palette): boolean => { |
| 11 | + const { color, name, shade, shades } = palette; |
| 12 | + |
| 13 | + if (!color || typeof color !== 'string' || !isColor(color)) { |
| 14 | + throw new Error(`Invalid color: '${color}'. Please provide a valid color.`); |
| 15 | + } |
| 16 | + |
| 17 | + if (!name || typeof name !== 'string') { |
| 18 | + throw new Error(`Invalid name: '${name}'. Please provide a valid name.`); |
| 19 | + } |
| 20 | + |
| 21 | + if (shade && typeof shade !== 'number') { |
| 22 | + throw new Error(`Invalid shade value: '${shade}'. Shade must be a number.`); |
15 | 23 | } |
16 | | -}; |
17 | 24 |
|
18 | | -export const checkParam = (palette: Palette) => { |
19 | | - if (palette.color && typeof palette.color === 'string' && palette.name && typeof palette.name == 'string') { |
20 | | - if (!isColor(palette.color)) { |
21 | | - throw new Error( |
22 | | - `'${palette.color}' The value you entered is not a color. e.g #ffbd00 or #ffb or rgba(255, 189, 0, 1) or rgb(255, 189, 0) or hsl(44, 100%, 50%)`, |
23 | | - ); |
24 | | - } else if (!palette.shade && palette.shades) { |
25 | | - throw new Error(`If you want to specify the shades, you have to specify the main shade.`); |
26 | | - } else if (palette.shade && typeof palette.shade !== 'number') { |
27 | | - throw new Error(`'${palette.shade}' - type: ${typeof palette.shade} It must be of type number.`); |
28 | | - } else if (palette.shades && !Array.isArray(palette.shades)) { |
29 | | - throw new Error(`Shades are not array.`); |
30 | | - } else if (palette.shades && palette.shades.length <= 2) { |
31 | | - throw new Error(`Shades can consist of at least 3 elements.`); |
32 | | - } else if (palette.shade && palette.shades && !palette.shades.includes(palette.shade)) { |
33 | | - throw new Error(`'${palette.shade}' mainShade are not included in the your shades.`); |
34 | | - } else if (!palette.shades && palette.shade && !initalOptions.shades.includes(palette.shade)) { |
35 | | - throw new Error(`'${palette.shade}' mainShade can only be 50, 100, 200, 300, 400, 500, 600, 700, 800 or 900.`); |
36 | | - } else { |
37 | | - return true; |
| 25 | + if (shades) { |
| 26 | + if (!Array.isArray(shades)) { |
| 27 | + throw new Error(`Invalid shades: '${shades}'. Shades must be an array.`); |
| 28 | + } |
| 29 | + |
| 30 | + if (shades.length < 3) { |
| 31 | + throw new Error('Shades array must contain at least 3 elements.'); |
| 32 | + } |
| 33 | + |
| 34 | + if (shade && !shades.includes(shade)) { |
| 35 | + throw new Error(`Main shade '${shade}' is not included in the shades array.`); |
38 | 36 | } |
39 | 37 | } else { |
40 | | - throw new Error('Make sure the required data is included.'); |
| 38 | + if (shade && ![50, 100, 200, 300, 400, 500, 600, 700, 800, 900].includes(shade)) { |
| 39 | + throw new Error(`Main shade '${shade}' must be one of: 50, 100, 200, 300, 400, 500, 600, 700, 800, 900.`); |
| 40 | + } |
41 | 41 | } |
| 42 | + |
| 43 | + return true; |
42 | 44 | }; |
0 commit comments