@mhmdhammoud/meritt-utils 1.6.1 → 1.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ReleaseNotes.md +17 -1
- package/dist/__tests__/elastic-transport.test.d.ts +1 -0
- package/dist/__tests__/elastic-transport.test.js +78 -0
- package/dist/__tests__/logger.test.js +21 -0
- package/dist/lib/elastic-transport.d.ts +5 -1
- package/dist/lib/elastic-transport.js +49 -11
- package/dist/lib/logger.js +11 -6
- package/dist/types/logger.d.ts +2 -1
- package/package.json +9 -3
- package/.github/workflows/npm-publish.yml +0 -84
- package/.github/workflows/push.yml +0 -83
- package/.husky/pre-commit +0 -62
- package/.prettierrc +0 -8
- package/.prettierrc.json +0 -8
- package/ISSUES.md +0 -0
- package/eslint.config.mjs +0 -64
- package/example.env +0 -6
- package/jest.config.js +0 -13
- package/src/__tests__/colorful.test.ts +0 -77
- package/src/__tests__/formatter.test.ts +0 -96
- package/src/__tests__/logger.test.ts +0 -122
- package/src/env.d.ts +0 -9
- package/src/index.ts +0 -1
- package/src/lib/colorful.ts +0 -167
- package/src/lib/cypto.ts +0 -296
- package/src/lib/elastic-transport.ts +0 -307
- package/src/lib/formatter.ts +0 -125
- package/src/lib/imagefull.ts +0 -41
- package/src/lib/index.ts +0 -12
- package/src/lib/logger.ts +0 -590
- package/src/lib/pdf.ts +0 -25
- package/src/lib/trace-store.ts +0 -46
- package/src/types/index.ts +0 -1
- package/src/types/logger.ts +0 -66
- package/src/utilities/axios.ts +0 -4
- package/src/utilities/index.ts +0 -1
- package/tsconfig.json +0 -16
package/example.env
DELETED
package/jest.config.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
preset: 'ts-jest',
|
|
4
|
-
testEnvironment: 'node',
|
|
5
|
-
coverageDirectory: 'coverage',
|
|
6
|
-
coverageProvider: 'v8',
|
|
7
|
-
collectCoverageFrom: ['src/**/*.ts', '!test/**/*.ts'],
|
|
8
|
-
transform: {
|
|
9
|
-
'^.+\\.(ts)$': 'ts-jest',
|
|
10
|
-
},
|
|
11
|
-
transformIgnorePatterns: [],
|
|
12
|
-
testMatch: ['<rootDir>/src/**/__tests__/**/*.test.ts'],
|
|
13
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { Colorful } from '../lib'
|
|
2
|
-
|
|
3
|
-
describe('Colorful Class', () => {
|
|
4
|
-
describe('rgbToHex method', () => {
|
|
5
|
-
it('should convert RGB to HEX', () => {
|
|
6
|
-
const rgbColor = 'rgb(255, 255, 255)'
|
|
7
|
-
const hexColor = Colorful.rgbToHex(rgbColor)
|
|
8
|
-
expect(hexColor).toBe('#ffffff')
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it('should convert RGBA to HEX', () => {
|
|
12
|
-
const rgbaColor = 'rgba(255, 0, 0, 0.5)'
|
|
13
|
-
const hexColor = Colorful.rgbToHex(rgbaColor)
|
|
14
|
-
expect(hexColor).toBe('#ff0000')
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should throw an error for invalid color format', () => {
|
|
18
|
-
const invalidColor = 'invalidColor'
|
|
19
|
-
expect(() => Colorful.rgbToHex(invalidColor)).toThrowError(
|
|
20
|
-
`Invalid color format '${invalidColor}'. Please provide a valid RGB or RGBA color.`
|
|
21
|
-
)
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
describe('hexToRgb method', () => {
|
|
26
|
-
it('should convert HEX to RGB', () => {
|
|
27
|
-
const hexColor = '#ffffff'
|
|
28
|
-
const rgbColor = Colorful.hexToRgb(hexColor)
|
|
29
|
-
expect(rgbColor).toBe('rgb(255, 255, 255)')
|
|
30
|
-
})
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
describe('isLightColor method', () => {
|
|
34
|
-
it('should return true for a light color (HEX format)', () => {
|
|
35
|
-
const lightHexColor = '#f0f0f0'
|
|
36
|
-
const isLight = Colorful.isLightColor(lightHexColor)
|
|
37
|
-
expect(isLight).toBe(true)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should return false for a dark color (HEX format)', () => {
|
|
41
|
-
const darkHexColor = '#333333'
|
|
42
|
-
const isLight = Colorful.isLightColor(darkHexColor)
|
|
43
|
-
expect(isLight).toBe(false)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('should return true for a light color (RGBA format)', () => {
|
|
47
|
-
const lightRgbaColor = 'rgba(255, 255, 255, 0.5)'
|
|
48
|
-
const isLight = Colorful.isLightColor(lightRgbaColor)
|
|
49
|
-
expect(isLight).toBe(true)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('should return false for a dark color (RGBA format)', () => {
|
|
53
|
-
const darkRgbaColor = 'rgba(0, 0, 0, 0.8)'
|
|
54
|
-
const isLight = Colorful.isLightColor(darkRgbaColor)
|
|
55
|
-
expect(isLight).toBe(false)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should return true for a light color (RGB format)', () => {
|
|
59
|
-
const lightRgbColor = 'rgb(200, 220, 255)'
|
|
60
|
-
const isLight = Colorful.isLightColor(lightRgbColor)
|
|
61
|
-
expect(isLight).toBe(true)
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
it('should return false for a dark color (RGB format)', () => {
|
|
65
|
-
const darkRgbColor = 'rgb(10, 20, 30)'
|
|
66
|
-
const isLight = Colorful.isLightColor(darkRgbColor)
|
|
67
|
-
expect(isLight).toBe(false)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('should throw an error for an invalid color format', () => {
|
|
71
|
-
const invalidColor = 'invalid-color'
|
|
72
|
-
expect(() => Colorful.isLightColor(invalidColor)).toThrowError(
|
|
73
|
-
"Invalid color format 'invalid-color'. Please provide a valid HEX, RGB, or RGBA color."
|
|
74
|
-
)
|
|
75
|
-
})
|
|
76
|
-
})
|
|
77
|
-
})
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { Formatter } from '../lib'
|
|
2
|
-
|
|
3
|
-
describe('Formatter', () => {
|
|
4
|
-
describe('toUpperFirst method', () => {
|
|
5
|
-
it('should format string with - and upper first letter', () => {
|
|
6
|
-
const formattedString = Formatter.toUpperFirst('hello world')
|
|
7
|
-
expect(formattedString).toBe('Hello-world')
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it('should format string with spaces and upper first letter of every word', () => {
|
|
11
|
-
const formattedString = Formatter.toUpperFirst('hello_world+test')
|
|
12
|
-
expect(formattedString).toBe('Hello-world-test')
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('should throw an error for non-string input', () => {
|
|
16
|
-
expect(() => Formatter.toUpperFirst(123)).toThrowError(
|
|
17
|
-
'Provide a valid string'
|
|
18
|
-
)
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
describe('camelToKebab method', () => {
|
|
23
|
-
it('should convert camelCase to kebab-case', () => {
|
|
24
|
-
const kebabString = Formatter.camelToKebab('camelCaseString')
|
|
25
|
-
expect(kebabString).toBe('camel-case-string')
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should handle spaces and underscores', () => {
|
|
29
|
-
const kebabString = Formatter.camelToKebab('hello World_with Spaces')
|
|
30
|
-
expect(kebabString).toBe('hello-world_with-spaces')
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should throw an error for non-string input', () => {
|
|
34
|
-
expect(() => Formatter.camelToKebab(123)).toThrowError(
|
|
35
|
-
'Invalid input. Please provide a valid string.'
|
|
36
|
-
)
|
|
37
|
-
})
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
describe('obfuscate method', () => {
|
|
41
|
-
it('should obfuscate email address', () => {
|
|
42
|
-
const obfuscatedEmail = Formatter.obfuscate('johndoe@email.com')
|
|
43
|
-
expect(obfuscatedEmail).toMatch(/^jo.*@.*$/)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('should throw an error for non-string input', () => {
|
|
47
|
-
//@ts-ignore
|
|
48
|
-
expect(() => Formatter.obfuscate(123)).toThrowError(
|
|
49
|
-
'Provide a valid string'
|
|
50
|
-
)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('should throw an error for an invalid email address', () => {
|
|
54
|
-
expect(() => Formatter.obfuscate('invalid-email')).toThrowError(
|
|
55
|
-
'Provide a valid email address'
|
|
56
|
-
)
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
describe('toUpperTitle method', () => {
|
|
61
|
-
it('should capitalize first letter of each word and remove non-alphanumeric characters', () => {
|
|
62
|
-
const upperTitle = Formatter.toUpperTitle('this is34354345a---sentence')
|
|
63
|
-
expect(upperTitle).toBe('This Is A Sentence')
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it('should throw an error for non-string input', () => {
|
|
67
|
-
expect(() => Formatter.toUpperTitle(123)).toThrowError(
|
|
68
|
-
'Provide a valid string'
|
|
69
|
-
)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('should throw an error for empty string', () => {
|
|
73
|
-
expect(() => Formatter.toUpperTitle('')).toThrowError(
|
|
74
|
-
'Provide a valid string'
|
|
75
|
-
)
|
|
76
|
-
})
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
describe('slugify method', () => {
|
|
80
|
-
it('should generate a slug from the given string', () => {
|
|
81
|
-
const slug = Formatter.slugify('Hello World')
|
|
82
|
-
expect(slug).toBe('hello-world')
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
it('should handle special characters and spaces', () => {
|
|
86
|
-
const slug = Formatter.slugify('Special Characters % #$ & Space')
|
|
87
|
-
expect(slug).toBe('special-characters-space')
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
it('should throw an error for non-string input', () => {
|
|
91
|
-
expect(() => Formatter.slugify(123)).toThrowError(
|
|
92
|
-
'Provide a valid string'
|
|
93
|
-
)
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
})
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import Logger, { isValidLogLevel } from '../lib/logger'
|
|
2
|
-
import { pino } from 'pino'
|
|
3
|
-
|
|
4
|
-
jest.mock('pino')
|
|
5
|
-
|
|
6
|
-
const LOGGER_NAME = 'logger-name'
|
|
7
|
-
const PINO_DESTINATION = {}
|
|
8
|
-
const PINO = {
|
|
9
|
-
info: jest.fn(),
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
describe('create logger wrapper', () => {
|
|
13
|
-
test('initially create & configure logger', () => {
|
|
14
|
-
//@ts-ignore
|
|
15
|
-
const mock_pino_destination = jest
|
|
16
|
-
.spyOn(pino, 'destination')
|
|
17
|
-
//@ts-ignore
|
|
18
|
-
.mockReturnValue(PINO_DESTINATION)
|
|
19
|
-
//@ts-ignore
|
|
20
|
-
const mock_pino = pino.mockReturnValue(PINO)
|
|
21
|
-
|
|
22
|
-
/** create logger */
|
|
23
|
-
const logger = new Logger(LOGGER_NAME)
|
|
24
|
-
|
|
25
|
-
expect(logger).toBeDefined()
|
|
26
|
-
expect(logger['_name']).toBe(LOGGER_NAME)
|
|
27
|
-
expect(mock_pino).toHaveBeenCalledTimes(1)
|
|
28
|
-
expect(mock_pino).toHaveBeenCalledWith(
|
|
29
|
-
{
|
|
30
|
-
level: 'info',
|
|
31
|
-
timestamp: expect.any(Function),
|
|
32
|
-
},
|
|
33
|
-
PINO_DESTINATION
|
|
34
|
-
)
|
|
35
|
-
expect(mock_pino_destination).toHaveBeenCalledWith({
|
|
36
|
-
minLength: 1024,
|
|
37
|
-
sync: true,
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
test('create repeatedly - stick to pino singleton', () => {
|
|
41
|
-
//@ts-ignore
|
|
42
|
-
jest.spyOn(pino, 'destination').mockReturnValue(PINO_DESTINATION)
|
|
43
|
-
//@ts-ignore
|
|
44
|
-
const mock_pino = pino.mockReturnValue(PINO)
|
|
45
|
-
new Logger(LOGGER_NAME)
|
|
46
|
-
expect(mock_pino).toHaveBeenCalledTimes(1)
|
|
47
|
-
|
|
48
|
-
/** create additional logger */
|
|
49
|
-
new Logger(LOGGER_NAME)
|
|
50
|
-
/** no new pino created */
|
|
51
|
-
expect(mock_pino).toHaveBeenCalledTimes(1)
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
describe('validate log level', () => {
|
|
55
|
-
test('throw on invalid log level', () => {
|
|
56
|
-
const INVALID_LOG_LEVEL = 'invalid-log-level'
|
|
57
|
-
expect(() => {
|
|
58
|
-
isValidLogLevel(INVALID_LOG_LEVEL)
|
|
59
|
-
}).toThrowError(
|
|
60
|
-
`Invalid log level "${INVALID_LOG_LEVEL}": only error, warn, info, debug, trace are valid.`
|
|
61
|
-
)
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
describe('route and format logs', () => {
|
|
65
|
-
const LOG_EVENT = {
|
|
66
|
-
code: 'code',
|
|
67
|
-
msg: 'message',
|
|
68
|
-
}
|
|
69
|
-
const DETAILS = {
|
|
70
|
-
key0: 'val0',
|
|
71
|
-
key1: 'val1',
|
|
72
|
-
}
|
|
73
|
-
test('log info with structured context (single object in context field)', () => {
|
|
74
|
-
//@ts-ignore
|
|
75
|
-
jest.spyOn(pino, 'destination').mockReturnValue(PINO_DESTINATION)
|
|
76
|
-
//@ts-ignore
|
|
77
|
-
pino.mockReturnValue(PINO)
|
|
78
|
-
const logger = new Logger(LOGGER_NAME)
|
|
79
|
-
|
|
80
|
-
logger.info(LOG_EVENT, DETAILS)
|
|
81
|
-
|
|
82
|
-
expect(PINO.info).toHaveBeenCalledWith(
|
|
83
|
-
expect.objectContaining({
|
|
84
|
-
component: LOGGER_NAME,
|
|
85
|
-
code: LOG_EVENT.code,
|
|
86
|
-
msg: LOG_EVENT.msg,
|
|
87
|
-
context: { key0: 'val0', key1: 'val1' },
|
|
88
|
-
})
|
|
89
|
-
)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
test('remap reserved elastic field names in context', () => {
|
|
93
|
-
//@ts-ignore
|
|
94
|
-
jest.spyOn(pino, 'destination').mockReturnValue(PINO_DESTINATION)
|
|
95
|
-
//@ts-ignore
|
|
96
|
-
pino.mockReturnValue(PINO)
|
|
97
|
-
const logger = new Logger(LOGGER_NAME)
|
|
98
|
-
|
|
99
|
-
logger.info(LOG_EVENT, {
|
|
100
|
-
_id: 'abc123',
|
|
101
|
-
nested: {
|
|
102
|
-
_id: 'nested-1',
|
|
103
|
-
_index: 'bad-index',
|
|
104
|
-
},
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
// Context holds sanitized structure; reserved names remapped recursively
|
|
108
|
-
expect(PINO.info).toHaveBeenCalledWith(
|
|
109
|
-
expect.objectContaining({
|
|
110
|
-
context: expect.objectContaining({
|
|
111
|
-
mongo_id: 'abc123',
|
|
112
|
-
nested: { mongo_id: 'nested-1', es_index: 'bad-index' },
|
|
113
|
-
}),
|
|
114
|
-
})
|
|
115
|
-
)
|
|
116
|
-
expect(PINO.info).not.toHaveBeenCalledWith(
|
|
117
|
-
expect.objectContaining({
|
|
118
|
-
_id: expect.anything(),
|
|
119
|
-
})
|
|
120
|
-
)
|
|
121
|
-
})
|
|
122
|
-
})
|
package/src/env.d.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './lib'
|
package/src/lib/colorful.ts
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Author : Mhmd Hammoud https://github.com/mhmdhammoud
|
|
3
|
-
Date : 2024-01-20
|
|
4
|
-
Description : Color adapter
|
|
5
|
-
Version : 1.0
|
|
6
|
-
*/
|
|
7
|
-
class Colorful {
|
|
8
|
-
/**
|
|
9
|
-
* @remarks
|
|
10
|
-
* Receives a color in RGB format and returns it in HEX format
|
|
11
|
-
* @param color - Color in RGB format
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const color = 'rgb(255, 255, 255)';
|
|
15
|
-
* const hexColor = rgbToHex(color);
|
|
16
|
-
* console.log(hexColor) // #ffffff
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @returns Color in HEX format string
|
|
20
|
-
*/
|
|
21
|
-
rgbToHex = (color: string): string => {
|
|
22
|
-
const componentToHex = (c: any) => {
|
|
23
|
-
const hex = c.toString(16)
|
|
24
|
-
return hex.length == 1 ? '0' + hex : hex
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const rgbaMatch = color.match(
|
|
28
|
-
/rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*((\d+\.)?\d+)\)/
|
|
29
|
-
)
|
|
30
|
-
const rgbMatch = color.match(/rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)/)
|
|
31
|
-
|
|
32
|
-
let redColor, greenColor, blueColor
|
|
33
|
-
|
|
34
|
-
if (rgbaMatch) {
|
|
35
|
-
redColor = parseInt(rgbaMatch[1], 10)
|
|
36
|
-
greenColor = parseInt(rgbaMatch[2], 10)
|
|
37
|
-
blueColor = parseInt(rgbaMatch[3], 10)
|
|
38
|
-
} else if (rgbMatch) {
|
|
39
|
-
redColor = parseInt(rgbMatch[1], 10)
|
|
40
|
-
greenColor = parseInt(rgbMatch[2], 10)
|
|
41
|
-
blueColor = parseInt(rgbMatch[3], 10)
|
|
42
|
-
} else {
|
|
43
|
-
// Both patterns failed to match
|
|
44
|
-
throw new Error(
|
|
45
|
-
`Invalid color format '${color}'. Please provide a valid RGB or RGBA color.`
|
|
46
|
-
)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const hexColor =
|
|
50
|
-
'#' +
|
|
51
|
-
componentToHex(redColor) +
|
|
52
|
-
componentToHex(greenColor) +
|
|
53
|
-
componentToHex(blueColor)
|
|
54
|
-
|
|
55
|
-
return hexColor
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @remarks
|
|
60
|
-
* Receives a color in HEX format and returns it in RGB format
|
|
61
|
-
* @param hexColor - Color in HEX format
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* const hexColor = '#ffffff';
|
|
65
|
-
* const rgbColor = hexToRgb(hexColor);
|
|
66
|
-
* console.log(rgbColor) // 'rgb(255, 255, 255)'
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* @returns Color in RGB format string
|
|
70
|
-
*/
|
|
71
|
-
hexToRgb = (hexColor: string): string => {
|
|
72
|
-
const hex = hexColor.replace(/^#/, '')
|
|
73
|
-
const bigint = parseInt(hex, 16)
|
|
74
|
-
const red = (bigint >> 16) & 255
|
|
75
|
-
const green = (bigint >> 8) & 255
|
|
76
|
-
const blue = bigint & 255
|
|
77
|
-
return `rgb(${red}, ${green}, ${blue})`
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @remarks
|
|
82
|
-
* Checks whether a given HEX color is light or dark
|
|
83
|
-
* @param hexColor - Color in HEX format
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* const hexColor = '#ffffff';
|
|
87
|
-
* const isLight = isLightColor(hexColor);
|
|
88
|
-
* console.log(isLight); // true or false
|
|
89
|
-
* ```
|
|
90
|
-
*
|
|
91
|
-
* @returns True if the color is light, false if it is dark
|
|
92
|
-
*/
|
|
93
|
-
isLightColor = (color: string): boolean => {
|
|
94
|
-
// Remove spaces
|
|
95
|
-
const trimmedColor = color.replace(/\s/g, '')
|
|
96
|
-
|
|
97
|
-
// Check if the color is in RGBA format
|
|
98
|
-
const rgbaMatch = trimmedColor.match(
|
|
99
|
-
/^rgba\((\d+),(\d+),(\d+),(\d+(\.\d+)?)\)$/
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
if (rgbaMatch) {
|
|
103
|
-
// Extract RGB components from RGBA
|
|
104
|
-
const r = parseInt(rgbaMatch[1], 10)
|
|
105
|
-
const g = parseInt(rgbaMatch[2], 10)
|
|
106
|
-
const b = parseInt(rgbaMatch[3], 10)
|
|
107
|
-
|
|
108
|
-
// Calculate the relative luminance without considering alpha
|
|
109
|
-
const luminance = 0.299 * r + 0.587 * g + 0.114 * b
|
|
110
|
-
|
|
111
|
-
// You can adjust the threshold to your preference
|
|
112
|
-
const threshold = 128
|
|
113
|
-
|
|
114
|
-
// Determine whether the color is light or dark
|
|
115
|
-
return luminance > threshold
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Check if the color is in RGB format
|
|
119
|
-
const rgbMatch = trimmedColor.match(/^rgb\((\d+),(\d+),(\d+)\)$/)
|
|
120
|
-
|
|
121
|
-
if (rgbMatch) {
|
|
122
|
-
// Extract RGB components from RGB
|
|
123
|
-
const r = parseInt(rgbMatch[1], 10)
|
|
124
|
-
const g = parseInt(rgbMatch[2], 10)
|
|
125
|
-
const b = parseInt(rgbMatch[3], 10)
|
|
126
|
-
|
|
127
|
-
// Calculate the relative luminance of the color
|
|
128
|
-
// This formula gives more weight to green as the human eye is more sensitive to it
|
|
129
|
-
const luminance = 0.299 * r + 0.587 * g + 0.114 * b
|
|
130
|
-
|
|
131
|
-
// You can adjust the threshold to your preference
|
|
132
|
-
const threshold = 128
|
|
133
|
-
|
|
134
|
-
// Determine whether the color is light or dark
|
|
135
|
-
return luminance > threshold
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Remove the '#' character if present
|
|
139
|
-
const hexColor = trimmedColor.replace('#', '')
|
|
140
|
-
|
|
141
|
-
// Check if the hex color has a valid format
|
|
142
|
-
if (!/^[0-9A-Fa-f]{6}$/.test(hexColor)) {
|
|
143
|
-
throw new Error(
|
|
144
|
-
`Invalid color format '${color}'. Please provide a valid HEX, RGB, or RGBA color.`
|
|
145
|
-
)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Convert the hex color to RGB components
|
|
149
|
-
const r = parseInt(hexColor.slice(0, 2), 16)
|
|
150
|
-
const g = parseInt(hexColor.slice(2, 4), 16)
|
|
151
|
-
const b = parseInt(hexColor.slice(4, 6), 16)
|
|
152
|
-
|
|
153
|
-
// Calculate the relative luminance of the color
|
|
154
|
-
// This formula gives more weight to green as the human eye is more sensitive to it
|
|
155
|
-
const luminance = 0.299 * r + 0.587 * g + 0.114 * b
|
|
156
|
-
|
|
157
|
-
// You can adjust the threshold to your preference
|
|
158
|
-
const threshold = 128
|
|
159
|
-
|
|
160
|
-
// Determine whether the color is light or dark
|
|
161
|
-
return luminance > threshold
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const colorful = new Colorful()
|
|
166
|
-
|
|
167
|
-
export default colorful
|