1
+ // Add type-safe set method to Storage Prototype
2
+ Storage . prototype . set = function < T > ( key : string , obj : T ) : void {
3
+ this . setItem ( key , JSON . stringify ( obj ) ) ;
4
+ }
5
+
6
+ // Add type-safe get method to Storage prototype
7
+ Storage . prototype . get = function < T > ( key : string ) : T | null {
8
+ const item = this . getItem ( key ) ;
9
+ if ( ! item ) return null ;
10
+ try {
11
+ return JSON . parse ( item ) as T ;
12
+ } catch ( e ) {
13
+ console . error ( `Failed to parse stored item ${ key } :` , e ) ;
14
+ return null ;
15
+ }
16
+
17
+ }
18
+
19
+ // Type-safe object size function
20
+ export function objectSize ( obj : Record < string , any > ) : number {
21
+ return Object . keys ( obj ) . length ;
22
+ }
23
+
24
+ // Find key by value in an object
25
+ export function getKey < T extends Record < string , any > > ( obj : T , val : any ) : string | undefined {
26
+ return Object . keys ( obj ) . find ( key => {
27
+ const value = obj [ key ] ;
28
+ if ( typeof value === 'object' && value !== null ) {
29
+ return JSON . stringify ( value ) === JSON . stringify ( val ) ;
30
+ }
31
+ return value === val ;
32
+ } ) ;
33
+ }
34
+
35
+ // OS detection patterns
36
+ const OS_PATTERNS : Record < string , RegExp > = {
37
+ 'Windows' : / w i n d o w s / i,
38
+ 'MacOS' : / m a c / i,
39
+ 'Linux' : / l i n u x / i,
40
+ 'UNIX' : / x 1 1 / i
41
+ } ;
42
+ export function getOS ( ) : string {
43
+ const userInput = `${ navigator . platform } ${ navigator . userAgent } ` . toLowerCase ( ) ;
44
+
45
+ for ( const [ os , pattern ] of Object . entries ( OS_PATTERNS ) ) {
46
+ if ( pattern . test ( userInput ) ) {
47
+ return os ;
48
+ }
49
+ }
50
+
51
+ return '' ;
52
+ }
53
+ // Check for restricted key combinations
54
+ export function checkRestricted ( key : string ) : boolean {
55
+ const restrictedKeys : string [ ] = [
56
+ 'Ctrl + N' , 'Ctrl + W' , 'Ctrl + T' , 'Ctrl + C' , 'Ctrl + V' ,
57
+ 'Ctrl + Delete' , 'Ctrl + Backspace' , 'Ctrl + /' , 'Ctrl + \\' ,
58
+ 'Ctrl + ]' , "Ctrl + '" , 'Ctrl + `' , 'Ctrl + [' , 'Ctrl + ~' ,
59
+ 'Ctrl + Num1' , 'Ctrl + Num2' , 'Ctrl + Num3' , 'Ctrl + Num4' ,
60
+ 'Ctrl + Num5' , 'Ctrl + Num6' , 'Ctrl + Num*' , 'Ctrl + Num/' ,
61
+ 'Ctrl + Num.' , 'Ctrl + Num0'
62
+ ] ;
63
+
64
+ // Adjust for MacOS if needed
65
+ const modifiedKeys = getOS ( ) === 'MacOS'
66
+ ? restrictedKeys . map ( value =>
67
+ value . startsWith ( 'Ctrl' )
68
+ ? value . replace ( 'Ctrl' , 'Meta' )
69
+ : value
70
+ )
71
+ : restrictedKeys ;
72
+
73
+ return modifiedKeys . includes ( key ) ;
74
+ }
0 commit comments