-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathUtil.res
81 lines (68 loc) · 1.83 KB
/
Util.res
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
/**
module Debounce = {
// See: https://davidwalsh.name/javascript-debounce-function
let debounce = (~wait, fn) => {
let timeout = ref(None)
() => {
let unset = () => timeout := None
switch timeout.contents {
| Some(id) => Js.Global.clearTimeout(id)
| None => fn()
}
timeout := Some(Js.Global.setTimeout(unset, wait))
}
}
let debounce3 = (~wait, ~immediate=false, fn) => {
let timeout = ref(None)
(a1, a2, a3) => {
let unset = () => {
timeout := None
immediate ? fn(a1, a2, a3) : ()
}
switch timeout.contents {
| Some(id) => Js.Global.clearTimeout(id)
| None => fn(a1, a2, a3)
}
timeout := Some(Js.Global.setTimeout(unset, wait))
if immediate && timeout.contents === None {
fn(a1, a2, a3)
} else {
()
}
}
}
}
**/
module Unsafe = {
external elementAsString: React.element => string = "%identity"
}
module String = {
let camelCase: string => string = %raw("str => {
return str.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
}")
let capitalize: string => string = %raw("str => {
return str && str.charAt(0).toUpperCase() + str.substring(1);
}")
}
module Url = {
let isAbsolute: string => bool = %raw(`
function(str) {
var r = new RegExp('^(?:[a-z]+:)?//', 'i');
if (r.test(str))
{
return true
}
return false;
}
`) //', 'i');
}
module Date = {
type intl
@new @scope("Intl")
external dateTimeFormat: (string, {"month": string, "day": string, "year": string}) => intl =
"DateTimeFormat"
@send external format: (intl, Date.t) => string = "format"
let toDayMonthYear = (date: Date.t) => {
dateTimeFormat("en-US", {"month": "short", "day": "numeric", "year": "numeric"})->format(date)
}
}