Skip to content

Commit a5a08d4

Browse files
committed
feat: add handlers for each platform
1 parent e723470 commit a5a08d4

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export {
22
platform as default,
3-
PlatformKey,
4-
PlatformSelectObject,
3+
TPlatformKey,
4+
TPlatformSelectObject,
5+
TPlatformMainHandler,
6+
TPlatformEachHandler,
7+
IPlatform,
58
} from './platform';

src/platform.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ type Interpolation =
88
| Array<Interpolation>;
99
type RuleSet = Array<Interpolation>;
1010

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+
};
1814

19-
export type PlatformKey = 'android' | 'ios';
15+
const tuple = <T extends string[]>(...args: T) => args;
2016

2117
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;
2323

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 = []) => {
2533
if (typeof platformSelectObjectOrPlatformKeyString === 'string') {
2634
return styles;
2735
}
@@ -34,17 +42,26 @@ function _platform_main(platformSelectObjectOrPlatformKeyString: PlatformSelectO
3442
});
3543

3644
return [];
37-
}
45+
};
3846

39-
interface platform {
40-
(platformSelectObjectOrPlatformKeyString: PlatformSelectObject | PlatformKey, styles?: RuleSet): RuleSet;
41-
android: (styles?: RuleSet) => RuleSet | void;
42-
}
47+
class StyledPlatform {
48+
platform: any;
4349

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+
}
4965

50-
export const platform = platformGenerator as platform;
66+
const styledPlatform = new StyledPlatform();
67+
export const platform = styledPlatform.platform as IPlatform;

0 commit comments

Comments
 (0)