-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
135 lines (121 loc) · 3.31 KB
/
utils.js
File metadata and controls
135 lines (121 loc) · 3.31 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
/** help method to detect if code runs on server
* @example
* // returns true if is in server
* isServer()
* @returns {boolean}
*/
function isServer(){
return (typeof window === 'undefined')
}
/** help method to detect if code runs on client
* @example
* // returns true if is in server
* isClient()
* @returns {boolean}
*/
function isClient(){
return !isServer()
}
/** get input value from react ref.
* @param {object} - the ref of element
* @example
* getRefValue(inputRef)
* @returns {any} Current value of input
*/
function getRefValue(ref) {
if (typeof ref?.current?.value === "boolean") {
return ref?.current?.value;
}
return ref?.current?.value || null;
}
/** method to render conditionally a react component.
* @param {boolean} [condition=true] - condition to display the component
* @param {string} [ifComponent=null] - component to render if condition is true
* @param {string} [elseComponent=null] - component to render if condition is false
* @example
* const loading = true
* renderIf(loading, (<span>loading</span>)) // return <span>loading</span>
* @example
* const loading = false
* renderIf(loading, (<span>loading</span>), (<span>loaded</span>)) // <span>loaded</span>
* @returns {ReactComponent}
*/
function renderIf(condition = true, ifComponent = null, elseComponent = null) {
return condition ? ifComponent : elseComponent;
}
/** return this method on getServerSideProps to 404 redirect
* @param {object} [redirect=null] - redirect object
* @example
*
export async function getServerSideProps(res){
try {
return await SomePromise();
} catch(e){
return redirect404()
}
}
*/
function redirect404(redirect = {
permanent: false,
destination: "/not-found",
}){
return { redirect }
}
/** Method to format number as money / currency
* @param {string} [lang=pt-BR] - language
* @param {string} [style=currency] - style
* @param {string} [currency=BRL] - currency
* @example
*
* const Formarter = moneyFormatter();
* Formarter.format(10) // 'R$ 10,00'
*
*@returns {Object} - new Intl.NumberFormat(lang, { style, currency })
*/
function moneyFormatter(lang = 'pt-BR', style='currency', currency='BRL'){
return new Intl.NumberFormat(lang, { style, currency });
}
/** check if process.env.NODE_ENV === 'production'
* @example
* isProduction() // true or false
*@returns {boolean}
*/
function isProduction(){
return process.env.NODE_ENV.toLocaleLowerCase() === 'production'
}
/** cache server side props (Only production env)
* @param {object} [res] - response object from nextjs
* @param {string} [maxage=900] - maxage param
* @param {string} [revalidate=910] - revalidate param
* @example
*
export async function getServerSideProps(res){
//will cache this request
cacheServeSideProps(res);
try {
return await SomePromise();
} catch(e){
return redirect404()
}
}
*/
function cacheServeSideProps (res, maxage='900', revalidate='910'){
if(isProduction()){
if(res){
res.setHeader(
'Cache-Control',
`public, s-maxage=${maxage}, stale-while-revalidate=${revalidate}`
)
}
}
}
module.exports = {
isServer,
isClient,
getRefValue,
renderIf,
redirect404,
moneyFormatter,
cacheServeSideProps,
isProduction,
}