-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
139 lines (104 loc) · 3.16 KB
/
index.js
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
import { memoize } from "underscore";
import { ExampleColors } from "../../example";
import {
CreateTextStyles,
DefaultTextStyles,
WIDTH,
HEIGHT,
} from "../commonAssets";
let GlobalColors = ExampleColors;
let GlobalTextStyles = {};
class CommonAssets {
constructor(config = {}) {
const { Colors, TextStyles, textStylesConfig } = config;
this.Colors = Colors || ExampleColors;
this.TextStyles =
TextStyles || textStylesConfig
? new CreateTextStyles(textStylesConfig).allTextStyles
: DefaultTextStyles.allTextStyles;
GlobalColors = {
...this.Colors,
};
GlobalTextStyles = {
...this.TextStyles,
};
}
get colors() {
return this.Colors;
}
get textStyles() {
return this.TextStyles;
}
capitalizeFirstLetter = word => word[0].toUpperCase() + word.slice(1);
getColorFromString = color => {
const colorToReturn = this.Colors[color];
if (colorToReturn) {
return colorToReturn;
}
console.warn(`No color in your Colors obj named: ${color}. Please see the README for how to setup Colors.
Returning the color.`);
return color;
};
getDisabledColorFromString = color => {
const disabledColorToReturn = this.Colors[
`disabled${this.capitalizeFirstLetter(color)}`
];
if (disabledColorToReturn) {
return this.Colors[`disabled${this.capitalizeFirstLetter(color)}`];
}
console.warn(
`No color found in Colors for disabled${this.capitalizeFirstLetter(
color,
)}. Returning for ${color} instead. `,
);
return this.getColorFromString(color);
};
}
export const getGlobalColors = () => GlobalColors;
export const getGlobalTextStyles = () => GlobalTextStyles;
export const capitalizeFirstLetter = word =>
word[0].toUpperCase() + word.slice(1);
export const getColorFromString = memoize(color => {
const colorToReturn = getGlobalColors()[color];
if (colorToReturn) {
return colorToReturn;
}
console.warn(`No color in your Colors obj named: ${color}. Please see the README for how to setup Colors.
Returning the color: ${color}`);
return color;
});
export const getDisabledColorFromString = memoize(color => {
const disabledColorToReturn = getGlobalColors()[
`disabled${capitalizeFirstLetter(color)}`
];
if (disabledColorToReturn) {
return getGlobalColors()[`disabled${capitalizeFirstLetter(color)}`];
}
console.warn(
`No color found in Colors for disabled${capitalizeFirstLetter(
color,
)}. Returning for ${color} instead. `,
);
return getColorFromString(color);
});
export const nToPercent = (n, widthOrHeight) => {
const percent = n / 100;
if (widthOrHeight === "width") {
return percent * WIDTH;
}
return percent * HEIGHT;
};
// to set GlobalColors and GlobalTextStyles
const DefaultCommonAssets = new CommonAssets();
export const getImageSource = memoize(image => {
if (typeof image === "number") {
return image;
}
if (typeof image === "string") {
return { uri: image };
}
throw new Error("Image is neither local or a uri.");
});
export * from "./defaultRefs";
export * from "./styleHelpers";
export { CommonAssets, DefaultCommonAssets };