-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
976 lines (830 loc) · 35.5 KB
/
code.ts
File metadata and controls
976 lines (830 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
// Show the plugin UI
figma.showUI(__html__, { width: 420, height: 540 });
// Message handler types
type PopulateTableMessage = {
type: 'populate-table';
data: any;
format: 'objects';
};
type GenerateDummyDataMessage = {
type: 'generate-dummy-data';
rowCount: number;
columnCount: number;
};
type GenerateCustomDummyDataMessage = {
type: 'generate-custom-dummy-data';
headers: string[];
rowCount: number;
};
type CancelMessage = {
type: 'cancel';
};
type CheckSelectionMessage = {
type: 'check-selection';
};
type ShowNotificationMessage = {
type: 'show-notification';
message: string;
};
type PluginMessage =
| PopulateTableMessage
| GenerateDummyDataMessage
| GenerateCustomDummyDataMessage
| CancelMessage
| CheckSelectionMessage
| ShowNotificationMessage;
// Listen for messages from the UI
figma.ui.onmessage = async (msg: PluginMessage) => {
if (msg.type === 'populate-table') {
await populateTable(msg.data);
} else if (msg.type === 'generate-dummy-data') {
await populateTableWithDummyData(msg.rowCount, msg.columnCount);
} else if (msg.type === 'generate-custom-dummy-data') {
await populateTableWithCustomDummyData(msg.headers, msg.rowCount);
} else if (msg.type === 'cancel') {
figma.closePlugin();
} else if (msg.type === 'check-selection') {
const hasSelection = figma.currentPage.selection.length > 0;
figma.ui.postMessage({
type: 'selection-result',
hasSelection: hasSelection
});
if (!hasSelection) {
figma.notify('🚨 Please select table cells or rows to populate');
}
} else if (msg.type === 'show-notification') {
figma.notify(msg.message);
}
};
// Load fonts before editing text
async function loadFonts(nodes: SceneNode[]): Promise<void> {
const textNodes = nodes.filter(node => node.type === 'TEXT') as TextNode[];
const fontNames = new Set<string>();
textNodes.forEach(node => {
if (node.fontName !== figma.mixed) {
fontNames.add(JSON.stringify(node.fontName));
}
});
const fontLoadPromises = Array.from(fontNames).map(fontStr => {
const font = JSON.parse(fontStr) as FontName;
return figma.loadFontAsync(font);
});
await Promise.all(fontLoadPromises);
}
// Find value layers within a cell component
function findValueLayer(node: SceneNode): TextNode | null {
if (node.name.toLowerCase() === 'value' && node.type === 'TEXT') {
return node as TextNode;
}
if ('children' in node) {
const parent = node as ChildrenMixin & SceneNode;
for (const child of parent.children) {
const value = findValueLayer(child);
if (value) return value;
}
}
return null;
}
// Get all cell instances from a row or direct selection
function getAllCells(selection: readonly SceneNode[]): InstanceNode[] {
const cells: InstanceNode[] = [];
selection.forEach(node => {
// If the node is a component instance that might be a cell
if (node.type === 'INSTANCE') {
if (node.name.toLowerCase() === 'cell') {
cells.push(node);
} else {
// This might be a row, so check its children
if ('children' in node) {
const parent = node as ChildrenMixin & SceneNode;
const cellInstances = parent.children.filter(child =>
child.type === 'INSTANCE' && child.name.toLowerCase() === 'cell'
) as InstanceNode[];
cells.push(...cellInstances);
}
}
}
// If the node might be a row or container with cells
else if ('children' in node) {
const parent = node as ChildrenMixin & SceneNode;
// First level - might be rows
parent.children.forEach(child => {
if (child.type === 'INSTANCE') {
if (child.name.toLowerCase() === 'cell') {
cells.push(child);
} else if ('children' in child) {
// This might be a row, check its children
const rowNode = child as InstanceNode & ChildrenMixin;
const cellInstances = rowNode.children.filter(grandchild =>
grandchild.type === 'INSTANCE' && grandchild.name.toLowerCase() === 'cell'
) as InstanceNode[];
cells.push(...cellInstances);
}
} else if ('children' in child) {
// This might be a container with cells
const containerNode = child as SceneNode & ChildrenMixin;
const cellInstances = containerNode.children.filter(grandchild =>
grandchild.type === 'INSTANCE' && grandchild.name.toLowerCase() === 'cell'
) as InstanceNode[];
cells.push(...cellInstances);
}
});
}
});
return cells;
}
// Organize cells into rows
function organizeCellsIntoRows(cells: InstanceNode[]): InstanceNode[][] {
// Group cells by their parent (row)
const rowMap = new Map<string, InstanceNode[]>();
cells.forEach(cell => {
if (cell.parent) {
const parentId = cell.parent.id;
if (!rowMap.has(parentId)) {
rowMap.set(parentId, []);
}
rowMap.get(parentId)?.push(cell);
}
});
// Sort cells within each row by their x position
rowMap.forEach(rowCells => {
rowCells.sort((a, b) => a.x - b.x);
});
// Convert map to array of rows
const rows: InstanceNode[][] = Array.from(rowMap.values());
// Sort rows by their y position (top to bottom)
rows.sort((rowA, rowB) => {
if (rowA.length === 0 || rowB.length === 0) return 0;
return rowA[0].y - rowB[0].y;
});
return rows;
}
// Convert array of objects to a 2D array format
function convertObjectsTo2DArray(data: any[]): any[][] {
if (!Array.isArray(data) || data.length === 0) {
return [];
}
// Extract keys from the first object to use as headers
const keys = Object.keys(data[0]);
// Create header row
const result: any[][] = [keys];
// Add data rows
data.forEach(obj => {
const row = keys.map(key => obj[key]);
result.push(row);
});
return result;
}
// Populate table with provided data
async function populateTable(data: any[]): Promise<void> {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.notify('🚨 Please select table cells or rows to populate');
return;
}
// Get all cells from selection
const allCells = getAllCells(selection);
if (allCells.length === 0) {
figma.notify('🚨 No table cells found in selection');
return;
}
// Organize cells into rows
const rows = organizeCellsIntoRows(allCells);
// Get all text nodes within the cells to load fonts
const allTextNodes: SceneNode[] = [];
allCells.forEach(cell => {
const findTextNodes = (node: SceneNode) => {
if (node.type === 'TEXT') {
allTextNodes.push(node);
} else if ('children' in node) {
const parent = node as ChildrenMixin & SceneNode;
parent.children.forEach(child => findTextNodes(child));
}
};
findTextNodes(cell);
});
await loadFonts(allTextNodes);
// Convert data to 2D array format
const dataRows = convertObjectsTo2DArray(data);
// Populate cells with data
let updatedCount = 0;
for (let rowIndex = 0; rowIndex < rows.length && rowIndex < dataRows.length; rowIndex++) {
const rowCells = rows[rowIndex];
const rowData = dataRows[rowIndex];
for (let colIndex = 0; colIndex < rowCells.length && colIndex < rowData.length; colIndex++) {
const cell = rowCells[colIndex];
const valueLayer = findValueLayer(cell);
if (valueLayer) {
valueLayer.characters = String(rowData[colIndex] !== null && rowData[colIndex] !== undefined ? rowData[colIndex] : '');
updatedCount++;
}
}
}
// Success notification with checkmark
figma.notify(`✅ Updated ${updatedCount} cells with data`);
}
// Generate and populate with dummy data
async function populateTableWithDummyData(rowCount: number, columnCount: number): Promise<void> {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.notify('🚨 Please select table cells or rows to populate');
return;
}
// Get all cells from selection
const allCells = getAllCells(selection);
if (allCells.length === 0) {
figma.notify('🚨 No table cells found in selection');
return;
}
// Organize cells into rows
const rows = organizeCellsIntoRows(allCells);
// Determine actual column count based on the first row
const actualColumnCount = rows[0]?.length || 0;
// Get all text nodes within the cells to load fonts
const allTextNodes: SceneNode[] = [];
allCells.forEach(cell => {
const findTextNodes = (node: SceneNode) => {
if (node.type === 'TEXT') {
allTextNodes.push(node);
} else if ('children' in node) {
const parent = node as ChildrenMixin & SceneNode;
parent.children.forEach(child => findTextNodes(child));
}
};
findTextNodes(cell);
});
await loadFonts(allTextNodes);
// Generate dummy data
const departments = [
"Design", "Engineering", "Marketing", "Product", "Sales",
"Finance", "Legal", "Human Resources", "Customer Support", "Operations",
"Research", "Development", "Quality Assurance", "Business Development", "IT"
];
const locations = [
"London", "New York", "Singapore", "Berlin", "Madrid",
"Paris", "Tokyo", "Sydney", "Toronto", "Dubai",
"Amsterdam", "Stockholm", "Hong Kong", "Mumbai", "São Paulo",
"Seoul", "Mexico City", "Cape Town", "Milan", "Zurich"
];
const accessLevels = ["Admin", "Editor", "Viewer", "Owner", "Guest"];
const statuses = ["Active", "Inactive", "Pending", "Suspended", "Archived"];
// First names with more variety
const firstNames = [
"Mark", "Sarah", "James", "Priya", "Michael",
"Emma", "David", "Olivia", "Thomas", "Aisha",
"Robert", "Sofia", "Daniel", "Mei", "Carlos",
"Fatima", "John", "Zara", "Alexander", "Leila",
"William", "Sophia", "Luis", "Elena", "Mohammed",
"Chloe", "Hiroshi", "Isabella", "Raj", "Ingrid"
];
// Last names with more variety
const lastNames = [
"Darnalds", "Johnson", "Chen", "Patel", "Rodriguez",
"Wilson", "Kim", "Martinez", "Schmidt", "Ahmed",
"Brown", "Garcia", "Nguyen", "Singh", "Müller",
"Taylor", "Sato", "Lopez", "Ivanov", "Silva",
"Anderson", "Kowalski", "Rossi", "Jensen", "Ali",
"Wang", "Dubois", "Hernandez", "O'Connor", "Yamamoto"
];
// Additional data fields for extended columns
const teams = [
"Alpha", "Beta", "Gamma", "Delta", "Epsilon",
"Omega", "Phoenix", "Titan", "Nexus", "Quantum",
"Horizon", "Apex", "Zenith", "Pulse", "Fusion"
];
const projects = [
"Dashboard Redesign", "Mobile App", "API Integration", "Data Migration", "Cloud Infrastructure",
"Security Audit", "UI Component Library", "Analytics Platform", "Customer Portal", "Automation Framework",
"Blockchain Solution", "Machine Learning Model", "IoT Platform", "AR Experience", "Payment Gateway"
];
const skills = [
"JavaScript", "Python", "Design Systems", "Product Strategy", "Data Analysis",
"Project Management", "UX Research", "Cloud Architecture", "DevOps", "Machine Learning",
"Blockchain", "Mobile Development", "Cybersecurity", "AI", "Leadership"
];
const languages = [
"English", "Spanish", "Mandarin", "French", "German",
"Japanese", "Russian", "Arabic", "Portuguese", "Hindi",
"Korean", "Italian", "Dutch", "Swedish", "Turkish"
];
const startDates = [
"Jan 2022", "Mar 2021", "Sep 2020", "Feb 2023", "Nov 2019",
"Apr 2022", "Jul 2021", "Dec 2020", "May 2023", "Aug 2018",
"Oct 2022", "Jun 2021", "Jan 2020", "Mar 2023", "Sep 2019"
];
const phoneNumbers = [
"+1 (555) 123-4567", "+44 20 7946 0958", "+65 8765 4321", "+49 30 1234 5678", "+34 91 123 4567",
"+33 1 23 45 67 89", "+81 3 1234 5678", "+61 2 1234 5678", "+1 (416) 123-4567", "+971 4 123 4567",
"+31 20 123 4567", "+46 8 123 45 67", "+852 1234 5678", "+91 22 1234 5678", "+55 11 1234-5678"
];
// Shuffle arrays for randomization
const shuffleArray = <T>(array: T[]): T[] => {
const shuffled = [...array];
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]];
}
return shuffled;
};
const shuffledFirstNames = shuffleArray(firstNames);
const shuffledLastNames = shuffleArray(lastNames);
const shuffledDepartments = shuffleArray(departments);
const shuffledLocations = shuffleArray(locations);
const shuffledTeams = shuffleArray(teams);
const shuffledProjects = shuffleArray(projects);
const shuffledSkills = shuffleArray(skills);
const shuffledLanguages = shuffleArray(languages);
const shuffledStartDates = shuffleArray(startDates);
const shuffledPhoneNumbers = shuffleArray(phoneNumbers);
// Create dummy data objects
const dummyData: any[] = [];
for (let i = 0; i < Math.max(rowCount, 30); i++) {
// Use modulo to cycle through the shuffled arrays
const firstName = shuffledFirstNames[i % shuffledFirstNames.length];
const lastName = shuffledLastNames[(i + 3) % shuffledLastNames.length]; // Offset to avoid matching patterns
// Generate a unique email with a small random variation
const emailPrefix = Math.random() < 0.2
? `${firstName.toLowerCase()[0]}${lastName.toLowerCase()}`
: `${firstName.toLowerCase()}.${lastName.toLowerCase()}`;
const email = `${emailPrefix}@company.com`;
// Select from shuffled arrays with some randomness
const department = shuffledDepartments[i % shuffledDepartments.length];
const location = shuffledLocations[(i + Math.floor(Math.random() * 3)) % shuffledLocations.length];
const accessLevel = accessLevels[Math.floor(Math.random() * accessLevels.length)];
// Make "Active" status more common than others
const status = Math.random() < 0.7 ? "Active" : statuses[Math.floor(Math.random() * statuses.length)];
// Additional fields for extended columns
const team = shuffledTeams[i % shuffledTeams.length];
const project = shuffledProjects[(i + 2) % shuffledProjects.length];
const skill = shuffledSkills[(i + 4) % shuffledSkills.length];
const language = shuffledLanguages[(i + 1) % shuffledLanguages.length];
const startDate = shuffledStartDates[(i + 5) % shuffledStartDates.length];
const phoneNumber = shuffledPhoneNumbers[(i + 3) % shuffledPhoneNumbers.length];
// Create base object with standard fields
const dataObj: any = {
"Name": `${firstName} ${lastName}`,
"Department": department,
"Email": email,
"Location": location,
"Access Level": accessLevel,
"Status": status
};
// Add extended fields if needed
if (actualColumnCount > 6) {
dataObj["Team"] = team;
}
if (actualColumnCount > 7) {
dataObj["Project"] = project;
}
if (actualColumnCount > 8) {
dataObj["Skill"] = skill;
}
if (actualColumnCount > 9) {
dataObj["Language"] = language;
}
if (actualColumnCount > 10) {
dataObj["Start Date"] = startDate;
}
if (actualColumnCount > 11) {
dataObj["Phone"] = phoneNumber;
}
dummyData.push(dataObj);
}
// Shuffle the final data array for extra randomness
const shuffledData = shuffleArray(dummyData).slice(0, rowCount);
// Convert to 2D array for populating
const dataRows = convertObjectsTo2DArray(shuffledData);
// Populate cells with dummy data
let updatedCount = 0;
for (let rowIndex = 0; rowIndex < rows.length && rowIndex < dataRows.length; rowIndex++) {
const rowCells = rows[rowIndex];
const rowData = dataRows[rowIndex];
for (let colIndex = 0; colIndex < rowCells.length && colIndex < rowData.length; colIndex++) {
const cell = rowCells[colIndex];
const valueLayer = findValueLayer(cell);
if (valueLayer) {
valueLayer.characters = String(rowData[colIndex]);
updatedCount++;
}
}
}
// Success notification with checkmark
figma.notify(`✅ Updated ${updatedCount} cells with dummy data`);
}
// Generate and populate with custom dummy data
async function populateTableWithCustomDummyData(headers: string[], rowCount: number): Promise<void> {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.notify('🚨 Please select table cells or rows to populate');
return;
}
// Get all cells from selection
const allCells = getAllCells(selection);
if (allCells.length === 0) {
figma.notify('🚨 No table cells found in selection');
return;
}
// Organize cells into rows
const rows = organizeCellsIntoRows(allCells);
// Get all text nodes within the cells to load fonts
const allTextNodes: SceneNode[] = [];
allCells.forEach(cell => {
const findTextNodes = (node: SceneNode) => {
if (node.type === 'TEXT') {
allTextNodes.push(node);
} else if ('children' in node) {
const parent = node as ChildrenMixin & SceneNode;
parent.children.forEach(child => findTextNodes(child));
}
};
findTextNodes(cell);
});
await loadFonts(allTextNodes);
// Generate data based on headers
const data = generateDataFromHeaders(headers, rowCount);
// Convert to 2D array for populating
const dataRows = [headers, ...data]; // Add headers as first row
// Populate cells with dummy data
let updatedCount = 0;
for (let rowIndex = 0; rowIndex < rows.length && rowIndex < dataRows.length; rowIndex++) {
const rowCells = rows[rowIndex];
const rowData = dataRows[rowIndex];
for (let colIndex = 0; colIndex < rowCells.length && colIndex < rowData.length; colIndex++) {
const cell = rowCells[colIndex];
const valueLayer = findValueLayer(cell);
if (valueLayer) {
valueLayer.characters = String(rowData[colIndex]);
updatedCount++;
}
}
}
// Success notification with checkmark
figma.notify(`✅ Updated ${updatedCount} cells with custom data`);
}
// Function to generate data based on header names
function generateDataFromHeaders(headers: string[], rowCount: number): string[][] {
// Data generation resources
const firstNames = [
"Mark", "Sarah", "James", "Priya", "Michael",
"Emma", "David", "Olivia", "Thomas", "Aisha",
"Robert", "Sofia", "Daniel", "Mei", "Carlos",
"Fatima", "John", "Zara", "Alexander", "Leila",
"William", "Sophia", "Luis", "Elena", "Mohammed",
"Chloe", "Hiroshi", "Isabella", "Raj", "Ingrid"
];
const lastNames = [
"Darnalds", "Johnson", "Chen", "Patel", "Rodriguez",
"Wilson", "Kim", "Martinez", "Schmidt", "Ahmed",
"Brown", "Garcia", "Nguyen", "Singh", "Müller",
"Taylor", "Sato", "Lopez", "Ivanov", "Silva",
"Anderson", "Kowalski", "Rossi", "Jensen", "Ali",
"Wang", "Dubois", "Hernandez", "O'Connor", "Yamamoto"
];
const departments = [
"Design", "Engineering", "Marketing", "Product", "Sales",
"Finance", "Legal", "Human Resources", "Customer Support", "Operations",
"Research", "Development", "Quality Assurance", "Business Development", "IT"
];
const locations = [
"London", "New York", "Singapore", "Berlin", "Madrid",
"Paris", "Tokyo", "Sydney", "Toronto", "Dubai",
"Amsterdam", "Stockholm", "Hong Kong", "Mumbai", "São Paulo"
];
const roles = [
"Manager", "Director", "Associate", "Specialist", "Lead",
"Coordinator", "Analyst", "Designer", "Developer", "Consultant",
"Administrator", "Strategist", "Engineer", "Architect", "Executive"
];
const teams = [
"Alpha", "Beta", "Gamma", "Delta", "Epsilon",
"Omega", "Phoenix", "Titan", "Nexus", "Quantum",
"Horizon", "Apex", "Zenith", "Pulse", "Fusion"
];
const projects = [
"Dashboard Redesign", "Mobile App", "API Integration", "Data Migration", "Cloud Infrastructure",
"Security Audit", "UI Component Library", "Analytics Platform", "Customer Portal", "Automation Framework"
];
const skills = [
"JavaScript", "Python", "Design Systems", "Product Strategy", "Data Analysis",
"Project Management", "UX Research", "Cloud Architecture", "DevOps", "Machine Learning"
];
const languages = [
"English", "Spanish", "Mandarin", "French", "German",
"Japanese", "Russian", "Arabic", "Portuguese", "Hindi"
];
const statuses = ["Active", "Inactive", "Pending", "Suspended", "Archived", "Completed", "In Progress", "On Hold"];
const companies = [
"Acme Inc.", "Globex Corp", "Initech", "Umbrella Corp", "Stark Industries",
"Wayne Enterprises", "Cyberdyne Systems", "Soylent Corp", "Massive Dynamic", "Hooli"
];
const addresses = [
"123 Main St", "456 Park Ave", "789 Broadway", "321 Oak Lane", "654 Pine Road",
"987 Maple Drive", "741 Cedar Blvd", "852 Elm Street", "963 Willow Way", "159 Birch Court"
];
const cities = [
"New York", "London", "Tokyo", "Paris", "Berlin",
"Sydney", "Toronto", "Singapore", "Dubai", "Mumbai"
];
const phoneNumbers = [
"+1 (555) 123-4567", "+44 20 7946 0958", "+65 8765 4321", "+49 30 1234 5678", "+34 91 123 4567",
"+33 1 23 45 67 89", "+81 3 1234 5678", "+61 2 1234 5678", "+1 (416) 123-4567", "+971 4 123 4567"
];
const dates = [
"2023-01-15", "2023-02-28", "2023-03-10", "2023-04-22", "2023-05-05",
"2023-06-18", "2023-07-30", "2023-08-12", "2023-09-25", "2023-10-08",
"2023-11-20", "2023-12-03", "2024-01-16", "2024-02-29", "2024-03-11"
];
const prices = [
"$19.99", "$24.50", "$99.00", "$149.99", "$7.99",
"$35.75", "$199.99", "$49.00", "$299.99", "$12.50",
"$59.99", "$85.25", "$129.00", "$14.99", "$249.99"
];
const quantities = ["1", "2", "5", "10", "25", "50", "100", "250", "500", "1000"];
const ratings = ["★★★★★", "★★★★☆", "★★★☆☆", "★★☆☆☆", "★☆☆☆☆"];
const ids = [];
for (let i = 1; i <= 100; i++) {
// Use a simple string padding approach instead of padStart
const paddedNum = ('0000' + i).slice(-4);
ids.push(`ID-${paddedNum}`);
}
// Shuffle arrays for randomization
const shuffleArray = <T>(array: T[]): T[] => {
const shuffled = [...array];
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]];
}
return shuffled;
};
// Shuffle all data arrays
const shuffledFirstNames = shuffleArray(firstNames);
const shuffledLastNames = shuffleArray(lastNames);
const shuffledDepartments = shuffleArray(departments);
const shuffledLocations = shuffleArray(locations);
const shuffledRoles = shuffleArray(roles);
const shuffledTeams = shuffleArray(teams);
const shuffledProjects = shuffleArray(projects);
const shuffledSkills = shuffleArray(skills);
const shuffledLanguages = shuffleArray(languages);
const shuffledStatuses = shuffleArray(statuses);
const shuffledCompanies = shuffleArray(companies);
const shuffledAddresses = shuffleArray(addresses);
const shuffledCities = shuffleArray(cities);
const shuffledPhoneNumbers = shuffleArray(phoneNumbers);
const shuffledDates = shuffleArray(dates);
const shuffledPrices = shuffleArray(prices);
const shuffledQuantities = shuffleArray(quantities);
const shuffledRatings = shuffleArray(ratings);
const shuffledIds = shuffleArray(ids);
// Function to determine data type from header name
function getDataTypeForHeader(header: string): string {
const headerLower = header.toLowerCase().trim();
// Name-related headers
if (headerLower.includes('first') && (headerLower.includes('name') || headerLower === 'first')) {
return 'firstName';
} else if (headerLower.includes('last') && (headerLower.includes('name') || headerLower === 'last')) {
return 'lastName';
} else if (headerLower === 'name' || headerLower === 'full name' || headerLower.includes('full') && headerLower.includes('name')) {
return 'fullName';
}
// Contact information
else if (headerLower.includes('email') || headerLower.includes('e-mail') || headerLower.includes('mail')) {
return 'email';
} else if (headerLower.includes('phone') || headerLower.includes('mobile') || headerLower.includes('cell') ||
(headerLower.includes('number') && !headerLower.includes('id'))) {
return 'phone';
} else if (headerLower.includes('website') || headerLower.includes('site') || headerLower.includes('url') ||
headerLower.includes('web') || headerLower.includes('link')) {
return 'website';
}
// Location-related headers
else if (headerLower.includes('address') || headerLower.includes('street')) {
return 'address';
} else if (headerLower === 'city') {
return 'city';
} else if (headerLower === 'state' || headerLower === 'province') {
return 'state';
} else if (headerLower === 'country') {
return 'country';
} else if (headerLower.includes('zip') || headerLower.includes('postal') || headerLower.includes('post code')) {
return 'zipCode';
} else if (headerLower.includes('location') || headerLower.includes('region') || headerLower.includes('area')) {
return 'location';
}
// Organizational headers
else if (headerLower.includes('department') || headerLower.includes('dept')) {
return 'department';
} else if (headerLower.includes('role') || headerLower.includes('position') || headerLower.includes('title') ||
headerLower.includes('job')) {
return 'role';
} else if (headerLower.includes('team') || headerLower.includes('group')) {
return 'team';
} else if (headerLower.includes('project') || headerLower.includes('initiative')) {
return 'project';
} else if (headerLower.includes('company') || headerLower.includes('organization') ||
headerLower.includes('employer') || headerLower.includes('business')) {
return 'company';
}
// Skills and attributes
else if (headerLower.includes('skill') || headerLower.includes('expertise') ||
headerLower.includes('specialty') || headerLower.includes('proficiency')) {
return 'skill';
} else if (headerLower.includes('language') || headerLower.includes('lang')) {
return 'language';
}
// Status and metrics
else if (headerLower === 'status' || headerLower.includes('state') && !headerLower.includes('country')) {
return 'status';
} else if (headerLower.includes('date') || headerLower.includes('time') || headerLower.includes('when')) {
return 'date';
} else if (headerLower.includes('price') || headerLower.includes('cost') || headerLower.includes('$') ||
headerLower.includes('fee') || headerLower.includes('amount') && !headerLower.includes('quantity')) {
return 'price';
} else if (headerLower.includes('quantity') || headerLower.includes('qty') ||
headerLower.includes('count') || headerLower.includes('number of')) {
return 'quantity';
} else if (headerLower.includes('rating') || headerLower.includes('score') ||
headerLower.includes('rank') || headerLower.includes('review')) {
return 'rating';
}
// Identifiers
else if (headerLower === 'id' || headerLower.includes('identifier') || headerLower.includes('uuid') ||
headerLower.includes('code') || headerLower.includes('number') && headerLower.includes('id')) {
return 'id';
} else if (headerLower.includes('access') || headerLower.includes('permission') || headerLower.includes('privilege')) {
return 'accessLevel';
}
// Descriptions and content
else if (headerLower.includes('description') || headerLower.includes('desc') ||
headerLower.includes('about') || headerLower.includes('details')) {
return 'description';
} else if (headerLower.includes('comment') || headerLower.includes('note') || headerLower.includes('feedback')) {
return 'comment';
}
// Time-related
else if (headerLower.includes('duration') || headerLower.includes('length') || headerLower.includes('time span')) {
return 'duration';
} else if (headerLower.includes('start') && headerLower.includes('date')) {
return 'startDate';
} else if (headerLower.includes('end') && headerLower.includes('date')) {
return 'endDate';
} else if (headerLower.includes('deadline')) {
return 'deadline';
}
// Financial
else if (headerLower.includes('budget')) {
return 'budget';
} else if (headerLower.includes('revenue') || headerLower.includes('sales')) {
return 'revenue';
} else if (headerLower.includes('profit') || headerLower.includes('margin')) {
return 'profit';
}
// For any other header, use a generic type based on the header name itself
return 'generic:' + header.trim();
}
// Function to generate data for a specific type and row index
function generateDataForType(type: string, rowIndex: number): string {
// Handle generic types (headers that don't match any pattern)
if (type.startsWith('generic:')) {
const headerName = type.substring(8); // Remove 'generic:' prefix
// Generate sensible data based on the header name itself
// This ensures we always return something reasonable even for unknown headers
// Check if the header might be a percentage
if (headerName.toLowerCase().includes('percent') || headerName.toLowerCase().includes('%')) {
return `${Math.floor(Math.random() * 100)}%`;
}
// Check if the header might be a year
if (headerName.toLowerCase().includes('year')) {
const currentYear = new Date().getFullYear();
return `${currentYear - Math.floor(Math.random() * 5)}`;
}
// Check if the header might be related to currency but wasn't caught earlier
if (headerName.toLowerCase().includes('price') || headerName.toLowerCase().includes('cost') ||
headerName.toLowerCase().includes('payment') || headerName.toLowerCase().includes('salary')) {
return `$${(Math.random() * 1000).toFixed(2)}`;
}
// For truly generic headers, generate simple text with the row number
return `${headerName} ${rowIndex + 1}`;
}
// Handle all the specific data types
switch (type) {
case 'fullName':
return `${shuffledFirstNames[rowIndex % shuffledFirstNames.length]} ${shuffledLastNames[rowIndex % shuffledLastNames.length]}`;
case 'firstName':
return shuffledFirstNames[rowIndex % shuffledFirstNames.length];
case 'lastName':
return shuffledLastNames[rowIndex % shuffledLastNames.length];
case 'email': {
const firstName = shuffledFirstNames[rowIndex % shuffledFirstNames.length];
const lastName = shuffledLastNames[rowIndex % shuffledLastNames.length];
return `${firstName.toLowerCase()}.${lastName.toLowerCase()}@company.com`;
}
case 'phone':
return shuffledPhoneNumbers[rowIndex % shuffledPhoneNumbers.length];
case 'website': {
const company = shuffledCompanies[rowIndex % shuffledCompanies.length].toLowerCase().replace(/[^a-z0-9]/g, '');
return `https://www.${company}.com`;
}
case 'department':
return shuffledDepartments[rowIndex % shuffledDepartments.length];
case 'location':
return shuffledLocations[rowIndex % shuffledLocations.length];
case 'city':
return shuffledCities[rowIndex % shuffledCities.length];
case 'state':
return ["California", "New York", "Texas", "Florida", "Illinois", "Pennsylvania", "Ohio", "Georgia", "Michigan", "North Carolina"][rowIndex % 10];
case 'country':
return ["United States", "United Kingdom", "Canada", "Australia", "Germany", "France", "Japan", "India", "Brazil", "China"][rowIndex % 10];
case 'zipCode':
return `${10000 + Math.floor(Math.random() * 90000)}`;
case 'role':
return shuffledRoles[rowIndex % shuffledRoles.length];
case 'team':
return shuffledTeams[rowIndex % shuffledTeams.length];
case 'project':
return shuffledProjects[rowIndex % shuffledProjects.length];
case 'skill':
return shuffledSkills[rowIndex % shuffledSkills.length];
case 'language':
return shuffledLanguages[rowIndex % shuffledLanguages.length];
case 'status':
return shuffledStatuses[rowIndex % shuffledStatuses.length];
case 'company':
return shuffledCompanies[rowIndex % shuffledCompanies.length];
case 'address':
return `${shuffledAddresses[rowIndex % shuffledAddresses.length]}, ${shuffledCities[rowIndex % shuffledCities.length]}`;
case 'date':
return shuffledDates[rowIndex % shuffledDates.length];
case 'startDate':
return shuffledDates[(rowIndex + 2) % shuffledDates.length];
case 'endDate':
return shuffledDates[(rowIndex + 5) % shuffledDates.length];
case 'deadline':
// Generate a future date
const futureDate = new Date();
futureDate.setDate(futureDate.getDate() + (10 + rowIndex % 30));
return futureDate.toISOString().split('T')[0];
case 'price':
return shuffledPrices[rowIndex % shuffledPrices.length];
case 'budget':
return `$${(10000 + Math.floor(Math.random() * 90000)).toLocaleString()}`;
case 'revenue':
return `$${(100000 + Math.floor(Math.random() * 900000)).toLocaleString()}`;
case 'profit':
return `$${(5000 + Math.floor(Math.random() * 50000)).toLocaleString()}`;
case 'quantity':
return shuffledQuantities[rowIndex % shuffledQuantities.length];
case 'rating':
return shuffledRatings[rowIndex % shuffledRatings.length];
case 'id':
return shuffledIds[rowIndex % shuffledIds.length];
case 'accessLevel':
return ['Admin', 'Editor', 'Viewer', 'Owner', 'Guest'][rowIndex % 5];
case 'description':
return [
"A comprehensive solution for enterprise needs.",
"Designed with user experience in mind.",
"Optimized for performance and scalability.",
"Innovative approach to solving complex problems.",
"Industry-leading features and capabilities.",
"Built on cutting-edge technology.",
"Tailored for maximum efficiency.",
"Seamless integration with existing systems.",
"Revolutionary design that sets new standards.",
"Engineered for reliability and durability."
][rowIndex % 10];
case 'comment':
return [
"Looks great, approved!",
"Need some minor adjustments.",
"Excellent work on this.",
"Let's discuss this further.",
"I have some concerns about this.",
"Very impressive results.",
"This exceeds expectations.",
"Please review and get back to me.",
"I'd like to see alternative options.",
"This is exactly what we needed."
][rowIndex % 10];
case 'duration':
return [`${1 + rowIndex % 12} hours`, `${1 + rowIndex % 30} days`, `${1 + rowIndex % 12} months`][rowIndex % 3];
default:
return `Item ${rowIndex + 1}`;
}
}
// Generate data rows
const rows: string[][] = [];
// Determine data type for each header
const headerTypes = headers.map(header => getDataTypeForHeader(header));
// Generate data for each row
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
const row: string[] = [];
// Generate data for each column based on its header type
for (let colIndex = 0; colIndex < headers.length; colIndex++) {
const dataType = headerTypes[colIndex];
row.push(generateDataForType(dataType, rowIndex));
}
rows.push(row);
}
return rows;
}