@osimatic/helpers-js 1.4.23 → 1.4.25
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/.claude/settings.local.json +13 -0
- package/CHANGELOG +2 -1
- package/array.js +2 -2
- package/bank.js +3 -3
- package/chartjs.js +1 -1
- package/contact_details.js +2 -2
- package/date_time.js +25 -16
- package/draw.js +3 -2
- package/duration.js +12 -15
- package/event_bus.js +2 -2
- package/file.js +1 -1
- package/form_helper.js +1 -1
- package/http_client.js +2 -0
- package/jwt.js +18 -6
- package/location.js +6 -2
- package/media.js +1 -1
- package/number.js +2 -3
- package/package.json +28 -2
- package/social_network.js +5 -0
- package/string.js +11 -2
- package/tests/array.test.js +458 -0
- package/tests/bank.test.js +158 -0
- package/tests/chartjs.test.js +273 -0
- package/tests/contact_details.test.js +391 -0
- package/tests/date_time/DatePeriod.test.js +179 -0
- package/tests/date_time/DateTime.test.js +492 -0
- package/tests/date_time/SqlDate.test.js +205 -0
- package/tests/date_time/SqlDateTime.test.js +326 -0
- package/tests/date_time/SqlTime.test.js +162 -0
- package/tests/date_time/TimestampUnix.test.js +262 -0
- package/tests/draw.test.js +271 -0
- package/tests/duration.test.js +365 -0
- package/tests/event_bus.test.js +268 -0
- package/tests/file.test.js +358 -0
- package/tests/form_date.test.js +417 -0
- package/tests/form_helper.test.js +415 -0
- package/tests/http_client.test.js +570 -0
- package/tests/jwt.test.js +804 -0
- package/tests/location.test.js +509 -0
- package/tests/media.test.js +458 -0
- package/tests/network.test.js +489 -0
- package/tests/number.test.js +448 -0
- package/tests/open_street_map.test.js +388 -0
- package/tests/shopping_cart.test.js +355 -0
- package/tests/social_network.test.js +333 -0
- package/tests/string.test.js +473 -0
- package/tests/user.test.js +204 -0
- package/tests/util.test.js +99 -0
- package/tests/visitor.test.js +508 -0
- package/tmpclaude-00a6-cwd +1 -0
- package/tmpclaude-0526-cwd +1 -0
- package/tmpclaude-0973-cwd +1 -0
- package/tmpclaude-0b61-cwd +1 -0
- package/tmpclaude-0fa4-cwd +1 -0
- package/tmpclaude-104f-cwd +1 -0
- package/tmpclaude-1468-cwd +1 -0
- package/tmpclaude-146f-cwd +1 -0
- package/tmpclaude-223d-cwd +1 -0
- package/tmpclaude-2330-cwd +1 -0
- package/tmpclaude-282a-cwd +1 -0
- package/tmpclaude-2846-cwd +1 -0
- package/tmpclaude-28a6-cwd +1 -0
- package/tmpclaude-2b5a-cwd +1 -0
- package/tmpclaude-2def-cwd +1 -0
- package/tmpclaude-324b-cwd +1 -0
- package/tmpclaude-35d3-cwd +1 -0
- package/tmpclaude-3906-cwd +1 -0
- package/tmpclaude-3b32-cwd +1 -0
- package/tmpclaude-3da9-cwd +1 -0
- package/tmpclaude-3dc3-cwd +1 -0
- package/tmpclaude-3e3b-cwd +1 -0
- package/tmpclaude-43b6-cwd +1 -0
- package/tmpclaude-4495-cwd +1 -0
- package/tmpclaude-462f-cwd +1 -0
- package/tmpclaude-4aa8-cwd +1 -0
- package/tmpclaude-4b29-cwd +1 -0
- package/tmpclaude-4db5-cwd +1 -0
- package/tmpclaude-4e01-cwd +1 -0
- package/tmpclaude-5101-cwd +1 -0
- package/tmpclaude-524f-cwd +1 -0
- package/tmpclaude-5636-cwd +1 -0
- package/tmpclaude-5cdd-cwd +1 -0
- package/tmpclaude-5f1f-cwd +1 -0
- package/tmpclaude-6078-cwd +1 -0
- package/tmpclaude-622e-cwd +1 -0
- package/tmpclaude-6802-cwd +1 -0
- package/tmpclaude-6e36-cwd +1 -0
- package/tmpclaude-7793-cwd +1 -0
- package/tmpclaude-7f96-cwd +1 -0
- package/tmpclaude-8566-cwd +1 -0
- package/tmpclaude-8874-cwd +1 -0
- package/tmpclaude-8915-cwd +1 -0
- package/tmpclaude-8c8b-cwd +1 -0
- package/tmpclaude-94df-cwd +1 -0
- package/tmpclaude-9859-cwd +1 -0
- package/tmpclaude-9ac5-cwd +1 -0
- package/tmpclaude-9f18-cwd +1 -0
- package/tmpclaude-a202-cwd +1 -0
- package/tmpclaude-a741-cwd +1 -0
- package/tmpclaude-ab5f-cwd +1 -0
- package/tmpclaude-b008-cwd +1 -0
- package/tmpclaude-b0a1-cwd +1 -0
- package/tmpclaude-b63d-cwd +1 -0
- package/tmpclaude-b681-cwd +1 -0
- package/tmpclaude-b72d-cwd +1 -0
- package/tmpclaude-b92f-cwd +1 -0
- package/tmpclaude-bc49-cwd +1 -0
- package/tmpclaude-bc50-cwd +1 -0
- package/tmpclaude-bccf-cwd +1 -0
- package/tmpclaude-be55-cwd +1 -0
- package/tmpclaude-c228-cwd +1 -0
- package/tmpclaude-c717-cwd +1 -0
- package/tmpclaude-c7ce-cwd +1 -0
- package/tmpclaude-cf3e-cwd +1 -0
- package/tmpclaude-d142-cwd +1 -0
- package/tmpclaude-d5bc-cwd +1 -0
- package/tmpclaude-d6ae-cwd +1 -0
- package/tmpclaude-d77a-cwd +1 -0
- package/tmpclaude-d8da-cwd +1 -0
- package/tmpclaude-dbdb-cwd +1 -0
- package/tmpclaude-de61-cwd +1 -0
- package/tmpclaude-de81-cwd +1 -0
- package/tmpclaude-df9d-cwd +1 -0
- package/tmpclaude-e786-cwd +1 -0
- package/tmpclaude-f01d-cwd +1 -0
- package/tmpclaude-f2a9-cwd +1 -0
- package/tmpclaude-fc36-cwd +1 -0
- package/tmpclaude-ffef-cwd +1 -0
- package/visitor.js +2 -2
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
const { TimestampUnix } = require('../../date_time');
|
|
2
|
+
|
|
3
|
+
describe('TimestampUnix', () => {
|
|
4
|
+
describe('parse', () => {
|
|
5
|
+
test('should parse Unix timestamp to Date', () => {
|
|
6
|
+
const timestamp = 1705314600; // 2024-01-15 10:30:00 UTC
|
|
7
|
+
const result = TimestampUnix.parse(timestamp);
|
|
8
|
+
expect(result instanceof Date).toBe(true);
|
|
9
|
+
expect(result.getTime()).toBe(1705314600000);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('should return null for null input', () => {
|
|
13
|
+
expect(TimestampUnix.parse(null)).toBeNull();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('should handle timestamp 0', () => {
|
|
17
|
+
const result = TimestampUnix.parse(0);
|
|
18
|
+
expect(result.getTime()).toBe(0);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('getCurrent', () => {
|
|
23
|
+
test('should return current Unix timestamp', () => {
|
|
24
|
+
const result = TimestampUnix.getCurrent();
|
|
25
|
+
const now = Math.trunc(Date.now() / 1000);
|
|
26
|
+
expect(result).toBeCloseTo(now, 0);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('should return an integer', () => {
|
|
30
|
+
const result = TimestampUnix.getCurrent();
|
|
31
|
+
expect(Number.isInteger(result)).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('getDateDigitalDisplay', () => {
|
|
36
|
+
test('should format timestamp date in digital format', () => {
|
|
37
|
+
const timestamp = 1705314600;
|
|
38
|
+
const result = TimestampUnix.getDateDigitalDisplay(timestamp, 'fr-FR', 'UTC');
|
|
39
|
+
expect(result).toContain('15');
|
|
40
|
+
expect(result).toContain('01');
|
|
41
|
+
expect(result).toContain('2024');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('getDateTextDisplay', () => {
|
|
46
|
+
test('should format timestamp date in text format', () => {
|
|
47
|
+
const timestamp = 1705314600;
|
|
48
|
+
const result = TimestampUnix.getDateTextDisplay(timestamp, 'fr-FR', 'UTC');
|
|
49
|
+
expect(typeof result).toBe('string');
|
|
50
|
+
expect(result.length).toBeGreaterThan(10);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('getTimeDisplay', () => {
|
|
55
|
+
test('should format timestamp time', () => {
|
|
56
|
+
const timestamp = 1705314600;
|
|
57
|
+
const result = TimestampUnix.getTimeDisplay(timestamp, 'fr-FR', 'UTC');
|
|
58
|
+
expect(result).toContain('10');
|
|
59
|
+
expect(result).toContain('30');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('getTimeDisplayWithNbDays', () => {
|
|
64
|
+
test('should display time without days difference', () => {
|
|
65
|
+
const timestamp = 1705314600;
|
|
66
|
+
const result = TimestampUnix.getTimeDisplayWithNbDays(timestamp, 0, 'fr-FR', 'UTC');
|
|
67
|
+
expect(result).toContain('10');
|
|
68
|
+
expect(result).toContain('30');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('should display time with days difference', () => {
|
|
72
|
+
const timestamp1 = 1705315800; // 2024-01-15 10:30:00
|
|
73
|
+
const timestamp2 = 1705574400; // 2024-01-18 10:00:00
|
|
74
|
+
const result = TimestampUnix.getTimeDisplayWithNbDays(timestamp2, timestamp1, 'fr-FR', 'UTC');
|
|
75
|
+
expect(result).toContain('J+');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('getTimeDigitalDisplay', () => {
|
|
80
|
+
test('should format timestamp time in digital format', () => {
|
|
81
|
+
const timestamp = 1705314600;
|
|
82
|
+
const result = TimestampUnix.getTimeDigitalDisplay(timestamp, 'fr-FR', 'UTC');
|
|
83
|
+
expect(result).toMatch(/\d{2}:\d{2}:\d{2}/);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('getYear', () => {
|
|
88
|
+
test('should return year from timestamp', () => {
|
|
89
|
+
const timestamp = 1705314600;
|
|
90
|
+
const result = TimestampUnix.getYear(timestamp, 'UTC');
|
|
91
|
+
expect(result).toBe(2024);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('getMonth', () => {
|
|
96
|
+
test('should return month from timestamp', () => {
|
|
97
|
+
const timestamp = 1705314600;
|
|
98
|
+
const result = TimestampUnix.getMonth(timestamp, 'UTC');
|
|
99
|
+
expect(result).toBe(1);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('getDayOfMonth', () => {
|
|
104
|
+
test('should return day of month from timestamp', () => {
|
|
105
|
+
const timestamp = 1705314600;
|
|
106
|
+
const result = TimestampUnix.getDayOfMonth(timestamp, 'UTC');
|
|
107
|
+
expect(result).toBe(15);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('getHour', () => {
|
|
112
|
+
test('should return hour from timestamp', () => {
|
|
113
|
+
const timestamp = 1705314600;
|
|
114
|
+
const result = TimestampUnix.getHour(timestamp, 'UTC');
|
|
115
|
+
expect(result).toBe(10);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('getMinute', () => {
|
|
120
|
+
test('should return minute from timestamp', () => {
|
|
121
|
+
const timestamp = 1705314600;
|
|
122
|
+
const result = TimestampUnix.getMinute(timestamp, 'UTC');
|
|
123
|
+
expect(result).toBe(30);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('getSecond', () => {
|
|
128
|
+
test('should return second from timestamp', () => {
|
|
129
|
+
const timestamp = 1705314600;
|
|
130
|
+
const result = TimestampUnix.getSecond(timestamp, 'UTC');
|
|
131
|
+
expect(result).toBe(0);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('should return 45 seconds', () => {
|
|
135
|
+
const timestamp = 1705315845;
|
|
136
|
+
const result = TimestampUnix.getSecond(timestamp, 'UTC');
|
|
137
|
+
expect(result).toBe(45);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('getSqlDateTime', () => {
|
|
142
|
+
test('should convert timestamp to SQL datetime', () => {
|
|
143
|
+
const timestamp = 1705314600;
|
|
144
|
+
const result = TimestampUnix.getSqlDateTime(timestamp, 'UTC');
|
|
145
|
+
expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('getSqlDate', () => {
|
|
150
|
+
test('should convert timestamp to SQL date', () => {
|
|
151
|
+
const timestamp = 1705314600;
|
|
152
|
+
const result = TimestampUnix.getSqlDate(timestamp, 'UTC');
|
|
153
|
+
expect(result).toBe('2024-01-15');
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('getSqlTime', () => {
|
|
158
|
+
test('should convert timestamp to SQL time', () => {
|
|
159
|
+
const timestamp = 1705314600;
|
|
160
|
+
const result = TimestampUnix.getSqlTime(timestamp, 'UTC');
|
|
161
|
+
expect(result).toBe('10:30:00');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('getDateForInputDate', () => {
|
|
166
|
+
test('should return date for input field', () => {
|
|
167
|
+
const timestamp = 1705314600;
|
|
168
|
+
const result = TimestampUnix.getDateForInputDate(timestamp, 'UTC');
|
|
169
|
+
expect(result).toBe('2024-01-15');
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe('getTimeForInputTime', () => {
|
|
174
|
+
test('should return time for input field without seconds', () => {
|
|
175
|
+
const timestamp = 1705314600;
|
|
176
|
+
const result = TimestampUnix.getTimeForInputTime(timestamp, 'UTC', false);
|
|
177
|
+
expect(result).toMatch(/^\d{2}:\d{2}$/);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('should return time for input field with seconds', () => {
|
|
181
|
+
const timestamp = 1705314600;
|
|
182
|
+
const result = TimestampUnix.getTimeForInputTime(timestamp, 'UTC', true);
|
|
183
|
+
expect(result).toMatch(/^\d{2}:\d{2}:\d{2}$/);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe('isDateEqual', () => {
|
|
188
|
+
test('should return true for timestamps on same date', () => {
|
|
189
|
+
const timestamp1 = 1705315800; // 2024-01-15 10:30:00
|
|
190
|
+
const timestamp2 = 1705329600; // 2024-01-15 14:20:00
|
|
191
|
+
expect(TimestampUnix.isDateEqual(timestamp1, timestamp2)).toBe(true);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('should return false for timestamps on different dates', () => {
|
|
195
|
+
const timestamp1 = 1705315800; // 2024-01-15
|
|
196
|
+
const timestamp2 = 1705402200; // 2024-01-16
|
|
197
|
+
expect(TimestampUnix.isDateEqual(timestamp1, timestamp2)).toBe(false);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('getNbDayBetweenTwo', () => {
|
|
202
|
+
test('should calculate days between two timestamps', () => {
|
|
203
|
+
const timestamp1 = 1705315800; // 2024-01-15 10:30:00
|
|
204
|
+
const timestamp2 = 1705747800; // 2024-01-20 10:30:00
|
|
205
|
+
expect(TimestampUnix.getNbDayBetweenTwo(timestamp1, timestamp2, true, 'UTC')).toBe(5);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('should return 0 for same day', () => {
|
|
209
|
+
const timestamp1 = 1705315800; // 2024-01-15 10:30:00
|
|
210
|
+
const timestamp2 = 1705329600; // 2024-01-15 14:20:00
|
|
211
|
+
expect(TimestampUnix.getNbDayBetweenTwo(timestamp1, timestamp2, false, 'UTC')).toBe(0);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe('isDateInThePast', () => {
|
|
216
|
+
test('should return true for past timestamp', () => {
|
|
217
|
+
const timestamp = 1577836800; // 2020-01-01
|
|
218
|
+
expect(TimestampUnix.isDateInThePast(timestamp)).toBe(true);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('should return false for future timestamp', () => {
|
|
222
|
+
const timestamp = 1893456000; // 2030-01-01
|
|
223
|
+
expect(TimestampUnix.isDateInThePast(timestamp)).toBe(false);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('isDateTimeInThePast', () => {
|
|
228
|
+
test('should return true for past timestamp', () => {
|
|
229
|
+
const timestamp = 1577836800; // 2020-01-01
|
|
230
|
+
expect(TimestampUnix.isDateTimeInThePast(timestamp)).toBe(true);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('should return false for future timestamp', () => {
|
|
234
|
+
const timestamp = 1893456000; // 2030-01-01
|
|
235
|
+
expect(TimestampUnix.isDateTimeInThePast(timestamp)).toBe(false);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('isDateInTheFuture', () => {
|
|
240
|
+
test('should return true for future timestamp', () => {
|
|
241
|
+
const timestamp = 1893456000; // 2030-01-01
|
|
242
|
+
expect(TimestampUnix.isDateInTheFuture(timestamp)).toBe(true);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('should return false for past timestamp', () => {
|
|
246
|
+
const timestamp = 1577836800; // 2020-01-01
|
|
247
|
+
expect(TimestampUnix.isDateInTheFuture(timestamp)).toBe(false);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe('isDateTimeInTheFuture', () => {
|
|
252
|
+
test('should return true for future timestamp', () => {
|
|
253
|
+
const timestamp = 1893456000; // 2030-01-01
|
|
254
|
+
expect(TimestampUnix.isDateTimeInTheFuture(timestamp)).toBe(true);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test('should return false for past timestamp', () => {
|
|
258
|
+
const timestamp = 1577836800; // 2020-01-01
|
|
259
|
+
expect(TimestampUnix.isDateTimeInTheFuture(timestamp)).toBe(false);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
});
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
const { HexColor, RgbColor } = require('../draw');
|
|
2
|
+
|
|
3
|
+
describe('HexColor', () => {
|
|
4
|
+
describe('check', () => {
|
|
5
|
+
test('should return true for valid 6-character hex color', () => {
|
|
6
|
+
expect(HexColor.check('FF5733')).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test('should return true for lowercase hex color', () => {
|
|
10
|
+
expect(HexColor.check('ff5733')).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('should return true for mixed case hex color', () => {
|
|
14
|
+
expect(HexColor.check('Ff5733')).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('should return true for all zeros', () => {
|
|
18
|
+
expect(HexColor.check('000000')).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should return true for all Fs', () => {
|
|
22
|
+
expect(HexColor.check('FFFFFF')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should return false for hex with # prefix', () => {
|
|
26
|
+
expect(HexColor.check('#FF5733')).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('should return false for 3-character hex', () => {
|
|
30
|
+
expect(HexColor.check('F57')).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('should return false for 5-character hex', () => {
|
|
34
|
+
expect(HexColor.check('FF573')).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('should return false for 7-character hex', () => {
|
|
38
|
+
expect(HexColor.check('FF57333')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('should return false for invalid characters', () => {
|
|
42
|
+
expect(HexColor.check('GG5733')).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('should return false for empty string', () => {
|
|
46
|
+
expect(HexColor.check('')).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('convertToRgb', () => {
|
|
51
|
+
test('should convert valid hex to RGB array', () => {
|
|
52
|
+
expect(HexColor.convertToRgb('FF5733')).toEqual([255, 87, 51]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('should convert white to RGB', () => {
|
|
56
|
+
expect(HexColor.convertToRgb('FFFFFF')).toEqual([255, 255, 255]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('should convert black to RGB', () => {
|
|
60
|
+
expect(HexColor.convertToRgb('000000')).toEqual([0, 0, 0]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('should handle hex with # prefix', () => {
|
|
64
|
+
expect(HexColor.convertToRgb('#FF5733')).toEqual([255, 87, 51]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('should handle lowercase hex', () => {
|
|
68
|
+
expect(HexColor.convertToRgb('ff5733')).toEqual([255, 87, 51]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('should handle mixed case hex', () => {
|
|
72
|
+
expect(HexColor.convertToRgb('Ff5733')).toEqual([255, 87, 51]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('should convert 3-character hex by doubling', () => {
|
|
76
|
+
expect(HexColor.convertToRgb('F57')).toEqual([255, 85, 119]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('should handle 3-character hex with # prefix', () => {
|
|
80
|
+
expect(HexColor.convertToRgb('#F57')).toEqual([255, 85, 119]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('should convert 3-character white', () => {
|
|
84
|
+
expect(HexColor.convertToRgb('FFF')).toEqual([255, 255, 255]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('should convert 3-character black', () => {
|
|
88
|
+
expect(HexColor.convertToRgb('000')).toEqual([0, 0, 0]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('should return null for invalid hex', () => {
|
|
92
|
+
expect(HexColor.convertToRgb('GG5733')).toBeNull();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('should return null for empty string', () => {
|
|
96
|
+
expect(HexColor.convertToRgb('')).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('should return null for too long hex', () => {
|
|
100
|
+
expect(HexColor.convertToRgb('FF57333')).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('should convert red', () => {
|
|
104
|
+
expect(HexColor.convertToRgb('FF0000')).toEqual([255, 0, 0]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('should convert green', () => {
|
|
108
|
+
expect(HexColor.convertToRgb('00FF00')).toEqual([0, 255, 0]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('should convert blue', () => {
|
|
112
|
+
expect(HexColor.convertToRgb('0000FF')).toEqual([0, 0, 255]);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('isLight', () => {
|
|
117
|
+
test('should return true for white', () => {
|
|
118
|
+
expect(HexColor.isLight('FFFFFF')).toBe(true);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('should return false for black', () => {
|
|
122
|
+
expect(HexColor.isLight('000000')).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('should return true for yellow (light color)', () => {
|
|
126
|
+
expect(HexColor.isLight('FFFF00')).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('should return false for dark blue', () => {
|
|
130
|
+
expect(HexColor.isLight('0000FF')).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('should return false for dark red', () => {
|
|
134
|
+
expect(HexColor.isLight('8B0000')).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('should return true for light cyan', () => {
|
|
138
|
+
expect(HexColor.isLight('E0FFFF')).toBe(true);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('should handle # prefix', () => {
|
|
142
|
+
expect(HexColor.isLight('#FFFFFF')).toBe(true);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('should handle 3-character hex', () => {
|
|
146
|
+
expect(HexColor.isLight('FFF')).toBe(true);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('should return false for invalid hex', () => {
|
|
150
|
+
expect(HexColor.isLight('GGGGGG')).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('should return false for empty string', () => {
|
|
154
|
+
expect(HexColor.isLight('')).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('should return true for light gray', () => {
|
|
158
|
+
expect(HexColor.isLight('CCCCCC')).toBe(true);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('should return false for dark gray', () => {
|
|
162
|
+
expect(HexColor.isLight('333333')).toBe(false);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test('should correctly evaluate medium gray (borderline)', () => {
|
|
166
|
+
// Medium gray (808080 = 128,128,128) should be evaluated by the luminance formula
|
|
167
|
+
// coeff = 0.2125*128 + 0.7154*128 + 0.0721*128 = 128
|
|
168
|
+
// coeff > 128 returns false, so it's considered dark
|
|
169
|
+
expect(HexColor.isLight('808080')).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('should return true for slightly lighter than medium gray', () => {
|
|
173
|
+
expect(HexColor.isLight('818181')).toBe(true);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('should handle green (important for luminance calculation)', () => {
|
|
177
|
+
// Green has highest weight in luminance formula (0.7154)
|
|
178
|
+
expect(HexColor.isLight('00FF00')).toBe(true);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('RgbColor', () => {
|
|
184
|
+
describe('isLight', () => {
|
|
185
|
+
test('should return true for white (255,255,255)', () => {
|
|
186
|
+
expect(RgbColor.isLight(255, 255, 255)).toBe(true);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('should return false for black (0,0,0)', () => {
|
|
190
|
+
expect(RgbColor.isLight(0, 0, 0)).toBe(false);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('should return true for pure red at high intensity (255,0,0)', () => {
|
|
194
|
+
expect(RgbColor.isLight(255, 0, 0)).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('should return true for pure green at high intensity (0,255,0)', () => {
|
|
198
|
+
// Green has highest weight (0.7154) so pure green should be light
|
|
199
|
+
expect(RgbColor.isLight(0, 255, 0)).toBe(true);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('should return false for pure blue at high intensity (0,0,255)', () => {
|
|
203
|
+
// Blue has lowest weight (0.0721) so pure blue should be dark
|
|
204
|
+
expect(RgbColor.isLight(0, 0, 255)).toBe(false);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('should return true for yellow (255,255,0)', () => {
|
|
208
|
+
expect(RgbColor.isLight(255, 255, 0)).toBe(true);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test('should return true for cyan (0,255,255)', () => {
|
|
212
|
+
expect(RgbColor.isLight(0, 255, 255)).toBe(true);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('should return false for magenta (255,0,255)', () => {
|
|
216
|
+
expect(RgbColor.isLight(255, 0, 255)).toBe(false);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('should return true for light gray (200,200,200)', () => {
|
|
220
|
+
expect(RgbColor.isLight(200, 200, 200)).toBe(true);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('should return false for dark gray (50,50,50)', () => {
|
|
224
|
+
expect(RgbColor.isLight(50, 50, 50)).toBe(false);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test('should handle edge case at exactly 128 threshold', () => {
|
|
228
|
+
// coeff = 0.2125*128 + 0.7154*128 + 0.0721*128 = 128
|
|
229
|
+
// coeff > 128 is false
|
|
230
|
+
expect(RgbColor.isLight(128, 128, 128)).toBe(false);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('should return true for just above threshold', () => {
|
|
234
|
+
expect(RgbColor.isLight(129, 129, 129)).toBe(true);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('should return false for just below threshold', () => {
|
|
238
|
+
expect(RgbColor.isLight(127, 127, 127)).toBe(false);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test('should correctly weight green more than red', () => {
|
|
242
|
+
// Test that green contributes more to lightness
|
|
243
|
+
// (100,0,0) vs (0,100,0)
|
|
244
|
+
expect(RgbColor.isLight(100, 0, 0)).toBe(false);
|
|
245
|
+
expect(RgbColor.isLight(0, 100, 0)).toBe(false); // Still too dark
|
|
246
|
+
expect(RgbColor.isLight(0, 180, 0)).toBe(true); // Green reaches light faster
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('should correctly weight blue less than red', () => {
|
|
250
|
+
// Test that blue contributes least to lightness
|
|
251
|
+
expect(RgbColor.isLight(150, 0, 0)).toBe(false);
|
|
252
|
+
expect(RgbColor.isLight(0, 0, 150)).toBe(false);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test('should handle orange color (255,165,0)', () => {
|
|
256
|
+
expect(RgbColor.isLight(255, 165, 0)).toBe(true);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test('should handle brown color (165,42,42)', () => {
|
|
260
|
+
expect(RgbColor.isLight(165, 42, 42)).toBe(false);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('should handle pink color (255,192,203)', () => {
|
|
264
|
+
expect(RgbColor.isLight(255, 192, 203)).toBe(true);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('should handle navy color (0,0,128)', () => {
|
|
268
|
+
expect(RgbColor.isLight(0, 0, 128)).toBe(false);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|