@@ -8,20 +8,28 @@ type Interpolation =
8
8
| Array < Interpolation > ;
9
9
type RuleSet = Array < Interpolation > ;
10
10
11
- export interface PlatformSelectObject {
12
- ios ?: RuleSet ;
13
- android ?: RuleSet ;
14
- macos ?: RuleSet ;
15
- windows ?: RuleSet ;
16
- web ?: RuleSet ;
17
- }
11
+ type PartialRecord < K extends string , T > = {
12
+ [ P in K ] ?: T ;
13
+ } ;
18
14
19
- export type PlatformKey = 'android' | 'ios' ;
15
+ const tuple = < T extends string [ ] > ( ... args : T ) => args ;
20
16
21
17
const currentPlatform = Platform . OS ;
22
- const platformKeys : PlatformKey [ ] = [ 'android' , 'ios' ] ;
18
+ const platformKeys = tuple ( 'android' , 'ios' , 'macos' , 'windows' , 'web' ) ;
19
+
20
+ export type TPlatformKey = typeof platformKeys [ number ] ;
21
+ export type TPlatformSelectObject = PartialRecord < string , RuleSet > ;
22
+ type TPlatformSelectObjectOrPlatformKey = TPlatformSelectObject | TPlatformKey ;
23
23
24
- function _platform_main ( platformSelectObjectOrPlatformKeyString : PlatformSelectObject | PlatformKey , styles : RuleSet = [ ] ) : RuleSet {
24
+ export type TPlatformMainHandler =
25
+ ( platformSelectObjectOrPlatformKeyString : TPlatformSelectObjectOrPlatformKey , styles ?: RuleSet ) => RuleSet ;
26
+ export type TPlatformEachHandler = ( styles ?: RuleSet ) => RuleSet | void ;
27
+
28
+ export interface IPlatform extends Record < TPlatformKey , TPlatformEachHandler > {
29
+ ( platformSelectObjectOrPlatformKeyString : TPlatformSelectObjectOrPlatformKey , styles ?: RuleSet ) : RuleSet ;
30
+ }
31
+
32
+ const platformMainHandler : TPlatformMainHandler = ( platformSelectObjectOrPlatformKeyString , styles = [ ] ) => {
25
33
if ( typeof platformSelectObjectOrPlatformKeyString === 'string' ) {
26
34
return styles ;
27
35
}
@@ -34,17 +42,26 @@ function _platform_main(platformSelectObjectOrPlatformKeyString: PlatformSelectO
34
42
} ) ;
35
43
36
44
return [ ] ;
37
- }
45
+ } ;
38
46
39
- interface platform {
40
- ( platformSelectObjectOrPlatformKeyString : PlatformSelectObject | PlatformKey , styles ?: RuleSet ) : RuleSet ;
41
- android : ( styles ?: RuleSet ) => RuleSet | void ;
42
- }
47
+ class StyledPlatform {
48
+ platform : any ;
43
49
44
- const platformGenerator = ( ( ) => {
45
- const platformSolution : any = _platform_main ;
46
- platformSolution . android = ( styles : RuleSet ) => styles ;
47
- return _platform_main ;
48
- } ) ( ) ;
50
+ constructor ( ) {
51
+ this . initializeMain ( ) ;
52
+ this . registerPlatformMethods ( ) ;
53
+ }
54
+
55
+ private initializeMain ( ) {
56
+ this . platform = platformMainHandler ;
57
+ }
58
+
59
+ private registerPlatformMethods ( ) {
60
+ platformKeys . forEach ( ( platformKey : TPlatformKey ) =>
61
+ this . platform [ platformKey ] = ( styles : RuleSet ) =>
62
+ currentPlatform === platformKey ? styles : [ ] ) ;
63
+ }
64
+ }
49
65
50
- export const platform = platformGenerator as platform ;
66
+ const styledPlatform = new StyledPlatform ( ) ;
67
+ export const platform = styledPlatform . platform as IPlatform ;
0 commit comments