-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPorting.ml
560 lines (510 loc) · 16.1 KB
/
Porting.ml
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
let (++) (a: string) (b: string) = a ^ b
let registerGlobal name key tagger decoder =
let open Vdom in
let enableCall callbacks_base =
let callbacks = ref callbacks_base in
let fn = fun ev ->
let open Tea_json.Decoder in
let open Tea_result in
match decodeEvent decoder ev with
| Error _ -> None
| Ok pos -> Some (tagger pos) in
let handler = EventHandlerCallback (key, fn) in
let elem = Web_node.document_node in
let cache = eventHandler_Register callbacks elem name handler in
fun () ->
let _ = eventHandler_Unregister elem name cache in
()
in Tea_sub.registration key enableCall
module PageVisibility = struct
type visibility = Hidden | Visible
end
let (<|) a b = a b
let (>>) (f1: 'a -> 'b) (f2: 'b -> 'c) : 'a -> 'c =
fun x -> x |> f1 |> f2
let (<<) (f1: 'b -> 'c) (f2: 'a -> 'b) : 'a -> 'c =
fun x -> x |> f2 |> f1
module Debug = struct
let crash (str: string) : 'a =
failwith str
let log (msg: string) (data: 'a) : 'a =
Js.log msg;
Js.log data;
data
end
let toString (v : 'a) : string =
Js.String.make v
let toOption (value: 'a) (sentinel: 'a) : 'a option =
if value = sentinel
then None
else Some value
let identity (value: 'a) : 'a =
value
module List = struct
let sum (l: int list) : int =
Belt.List.reduce l 0 (+)
let floatSum (l: float list) : float =
Belt.List.reduce l 0.0 (+.)
let map (fn: 'a -> 'b) (l: 'a list) : 'b list =
Belt.List.map l fn
let indexedMap (fn: 'int -> 'a -> 'b) (l: 'a list) : 'b list =
Belt.List.mapWithIndex l fn
let map2 (fn: 'a -> 'b -> 'c) (a: 'a list) (b: 'b list) : 'c list =
Belt.List.mapReverse2 a b fn |> Belt.List.reverse
let getBy (fn: 'a -> bool) (l: 'a list) : 'a option =
Belt.List.getBy l fn
let elemIndex (a: 'a) (l : 'a list) : int option =
l
|> Array.of_list
|> Js.Array.findIndex ((=) a)
|> toOption (-1)
let rec last (l : 'a list) : 'a option =
match l with
| [] -> None
| [a] -> Some a
| _ :: tail -> last tail
let member (i: 'a) (l : 'a list) : bool =
Belt.List.has l i (=)
let uniqueBy (f: 'a -> string) (l: 'a list) : 'a list =
let rec uniqueHelp
(f: 'a -> string)
(existing: Belt.Set.String.t)
(remaining: 'a list)
(accumulator: 'a list) =
match remaining with
| [] -> List.rev accumulator
| first :: rest ->
let computedFirst = f first in
if Belt.Set.String.has existing computedFirst
then uniqueHelp f existing rest accumulator
else
uniqueHelp
f
(Belt.Set.String.add existing computedFirst)
rest
(first :: accumulator)
in
uniqueHelp f Belt.Set.String.empty l []
let find (f: 'a -> bool) (l: 'a list) : 'a option =
Belt.List.getBy l f
let getAt (i: int) (l: 'a list) : 'a option =
Belt.List.get l i
let any (fn: 'a -> bool) (l: 'a list) : bool =
List.exists fn l
let head (l: 'a list) : 'a option =
Belt.List.head l
let drop (count: int) (l: 'a list) : 'a list =
Belt.List.drop l count
|. Belt.Option.getWithDefault []
let init (l: 'a list) : 'a list option =
match List.rev l with
| _ :: rest -> Some (List.rev rest)
| [] -> None
let filterMap (fn: 'a -> 'b option) (l: 'a list) : 'b list =
Belt.List.keepMap l fn
let filter (fn: 'a -> bool) (l: 'a list) : 'a list =
Belt.List.keep l fn
let concat (ls: 'a list list) : 'a list =
ls
|> Belt.List.toArray
|> Belt.List.concatMany
let partition (fn: 'a -> bool) (l: 'a list) : 'a list * 'a list =
List.partition fn l
let foldr (fn: 'a -> 'b -> 'b) (init: 'b) (l: 'a list) : 'b =
List.fold_right fn l init
let foldl (fn: 'a -> 'b -> 'b) (init: 'b) (l: 'a list) : 'b =
List.fold_right fn (List.rev l) init
let rec findIndexHelp (index : int) (predicate : 'a -> bool) (list : 'a list) :
int option =
match list with
| [] -> None
| x :: xs ->
if predicate x then Some index
else findIndexHelp (index + 1) predicate xs
let findIndex (fn: 'a -> bool) (l: 'a list) : int option =
findIndexHelp 0 fn l
let take (count: int) (l: 'a list) : 'a list =
Belt.List.take l count
|. Belt.Option.getWithDefault []
let updateAt (index : int) (fn : 'a -> 'a) (list : 'a list) : 'a list =
if index < 0 then list
else
let head = take index list in
let tail = drop index list in
match tail with x :: xs -> head @ (fn x :: xs) | _ -> list
let length (l: 'a list) : int =
List.length l
let reverse (l: 'a list) : 'a list =
List.rev l
let rec dropWhile (predicate : 'a -> bool) (list : 'a list) : 'a list =
match list with
| [] -> []
| x :: xs -> if predicate x then dropWhile predicate xs else list
let isEmpty (l: 'a list) : bool =
l = []
let cons (item: 'a) (l: 'a list) : 'a list =
item :: l
let takeWhile (predicate : 'a -> bool) (l : 'a list) : 'a list =
let rec takeWhileMemo memo list =
match list with
| [] -> List.rev memo
| x :: xs ->
if predicate x then takeWhileMemo (x :: memo) xs else List.rev memo
in
takeWhileMemo [] l
let all (fn: 'a -> bool) (l: 'a list) : bool =
Belt.List.every l fn
let tail (l: 'a list) : 'a list option =
match l with
| [] -> None
| _ :: rest -> Some rest
let append (l1: 'a list) (l2: 'a list) : 'a list =
l1 @ l2
let removeAt (index : int) (l : 'a list) : 'a list =
if index < 0 then l
else
let head = take index l in
let tail = drop index l |> tail in
match tail with None -> l | Some t -> append head t
let minimumBy (f : 'a -> 'comparable) (ls : 'a list) : 'a option =
let minBy x (y, fy) =
let fx = f x in
if fx < fy then (x, fx) else (y, fy)
in
match ls with
| [l_] -> Some l_
| l_ :: ls_ -> Some (fst <| foldl minBy (l_, f l_) ls_)
| _ -> None
let maximum (list : 'comparable list) : 'comparable option =
match list with x :: xs -> Some (foldl max x xs) | _ -> None
let sortBy (fn: 'a -> 'b) (l: 'a list) : 'a list =
Belt.List.sort l (fun a b ->
let a' = fn a in
let b' = fn b in
if a' = b'
then 0
else if a' < b'
then -1
else 1)
let span (p : 'a -> bool) (xs : 'a list) : 'a list * 'a list =
(takeWhile p xs, dropWhile p xs)
let rec groupWhile (eq : 'a -> 'a -> bool) (xs_ : 'a list) : 'a list list =
match xs_ with
| [] -> []
| x :: xs ->
let ys, zs = span (eq x) xs in
(x :: ys) :: groupWhile eq zs
let splitAt (n : int) (xs : 'a list) : 'a list * 'a list =
(take n xs, drop n xs)
let splitWhen (predicate : 'a -> bool) (list : 'a list) :
('a list * 'a list) option =
findIndex predicate list |. Belt.Option.map (fun i -> splitAt i list)
let intersperse (sep : 'a) (xs : 'a list) : 'a list =
match xs with
| [] -> []
| hd :: tl ->
let step x rest = sep :: x :: rest in
let spersed = foldr step [] tl in
hd :: spersed
let initialize (n : int) (f : int -> 'a) : 'a list =
let rec step i acc = if i < 0 then acc else step (i - 1) (f i :: acc) in
step (n - 1) []
let sortWith (fn: 'a -> 'a -> int) (l: 'a list) : 'a list =
Belt.List.sort l fn
end
module Result = struct
type ('err, 'ok) t = ('ok, 'err) Belt.Result.t
let withDefault (default: 'ok) (r: ('err, 'ok) t) : 'ok =
Belt.Result.getWithDefault r default
let map2 (fn: 'a -> 'b -> 'c) (a: ('err, 'a) t) (b: ('err, 'b) t) : ('err, 'c) t =
match a,b with
| Ok a, Ok b -> Ok (fn a b)
| Error a, Ok _ -> Error a
| Ok _, Error b -> Error b
| Error a, Error b -> Error a
let combine (l : ('x, 'a) t list) : ('x, 'a list) t =
(List.foldr (map2 (fun a b -> a :: b)) (Ok []) l)
let map (fn: 'ok -> 'value) (r: ('err, 'ok) t) : ('err, 'value) t =
Belt.Result.map r fn
let toOption (r: ('err, 'ok) t) : 'ok option =
match r with
| Ok v -> Some v
| _ -> None
end
type ('err, 'ok) result = ('err, 'ok) Result.t
module Base64 = struct
let encode (str: string) : string =
Webapi.Base64.btoa str
let decode (b64: string) : (string, string) result =
try
Ok (Webapi.Base64.atob b64)
with e ->
Error (Printexc.to_string e)
end
module Regex = struct
let regex s : Js.Re.t = Js.Re.fromString ("/" ^ s ^ "/")
let contains (re: Js.Re.t) (s: string) : bool = Js.Re.test s re
let replace (re: string) (repl: string) (str: string) =
Js.String.replaceByRe (regex re) repl str
let matches (re: Js.Re.t) (s: string) : Js.Re.result option = Js.Re.exec s re
end
(* let deOption (msg: string) (value: 'a option) : 'a = *)
(* match value with *)
(* | Some v -> v *)
(* | None -> failwith msg *)
module Option = struct
type 'a t = 'a option
let andThen (fn: 'a -> 'b option) (o: 'a option) : 'b option =
match o with
| None -> None
| Some x -> fn x
let orElse (ma : 'a option) (mb: 'a option) : ('a option) =
match mb with
| None -> ma
| Some _ -> mb
let map (f: 'a -> 'b) (o: 'a option) : 'b option =
Belt.Option.map o f
let withDefault (a: 'a) (o: 'a option) : 'a =
Belt.Option.getWithDefault o a
let foldrValues (item : 'a option) (list : 'a list) : 'a list =
match item with None -> list | Some v -> v :: list
let values (l : 'a option list) : 'a list = List.foldr foldrValues [] l
let toList (o: 'a option) : 'a list =
match o with
| None -> []
| Some o -> [o]
let isSome = Belt.Option.isSome
end
module Char = struct
let toCode (c: char) : int = Char.code c
let fromCode (i: int) : char = Char.chr i
end
module Tuple2 = struct
let create a b = (a,b)
end
module Tuple = struct
let mapSecond (fn: 'b -> 'c) ((a,b): 'a * 'b) : 'a * 'c =
(a, fn b)
let second ((a,b): 'a * 'b) : 'b =
b
let first ((a,b): 'a * 'b) : 'a =
a
let create a b = (a,b)
end
module String = struct
let length = String.length
let toInt (s: string) : (string, int) result =
try
Ok (int_of_string s)
with e ->
Error (Printexc.to_string e)
let toFloat (s: string) : (string, float) result =
try
Ok (float_of_string s)
with e ->
Error (Printexc.to_string e)
let uncons (s: string) : (char * string) option =
match s with
| "" -> None
| s -> Some (String.get s 0, String.sub s 1 (String.length s - 1))
let dropLeft (from: int) (s: string) : string =
Js.String.substr ~from s
let dropRight (from: int) (s: string) : string =
Js.String.sliceToEnd ~from s
let split (delimiter : string) (s: string) : string list =
Js.String.split delimiter s
|> Belt.List.fromArray
let join (sep : string) (l: string list) : string =
String.concat sep l
let endsWith (needle: string) (haystack: string) =
Js.String.endsWith needle haystack
let startsWith (needle: string) (haystack: string) =
Js.String.startsWith needle haystack
let toLower (s: string) : string =
String.lowercase s
let toUpper (s: string) : string =
String.uppercase s
let contains (needle: string) (haystack: string) : bool =
Js.String.includes needle haystack
let repeat (count: int) (s: string) : string =
Js.String.repeat count s
let fromList (l : char list) : string =
l
|> List.map Char.toCode
|> List.map Js.String.fromCharCode
|> String.concat ""
let toList (s: string) : char list =
s
|> Js.String.castToArrayLike
|> Js.Array.from
|> Belt.List.fromArray
let fromInt (i : int) : string =
Printf.sprintf "%d" i
let concat = String.concat ""
let fromChar (c : char) : string =
c |> Char.toCode |> Js.String.fromCharCode
let slice from to_ str =
Js.String.slice ~from ~to_ str
let trim = Js.String.trim
let insertAt (newStr: string) (pos: int) (origStr: string) : string =
(Js.String.slice ~from:0 ~to_:pos origStr)
^ newStr
^ (Js.String.sliceToEnd ~from:pos origStr)
end
module IntSet = struct
module Set = Belt.Set.Int
type t = Set.t
type value = Set.value
let fromList (l: value list) : t =
l
|> Belt.List.toArray
|> Set.fromArray
let member (i: value) (set: t) : bool =
Set.has set i
let diff (set1: t) (set2: t) : t =
Set.diff set1 set2
let isEmpty (s: t) : bool =
Set.isEmpty s
let toList (s: t) : value list =
Set.toList s
end
module StrSet = struct
module Set = Belt.Set.String
type t = Set.t
type value = Set.value
let fromList (l: value list) : t =
l
|> Belt.List.toArray
|> Set.fromArray
let member (i: value) (set: t) : bool =
Set.has set i
let diff (set1: t) (set2: t) : t =
Set.diff set1 set2
let isEmpty (s: t) : bool =
Set.isEmpty s
let toList (s: t) : value list =
Set.toList s
end
module StrDict = struct
module Map = Belt.Map.String
type key = Map.key
type 'value t = 'value Map.t
let toList = Map.toList
let empty = Map.empty
let fromList (l: ('key * 'value) list) : 'value t =
l
|> Belt.List.toArray
|> Map.fromArray
let get (k: key) (v: 'value t) : 'value option =
Map.get v k
let insert (k: key) (v: 'value) (map: 'value t) : 'value t =
Map.set map k v
let keys m : key list =
Map.keysToArray m
|> Belt.List.fromArray
end
module IntDict = struct
module Map = Belt.Map.Int
type key = Map.key
type 'value t = 'value Map.t
let toList = Map.toList
let empty = Map.empty
let fromList (l: ('key * 'value) list) : 'value t =
l
|> Belt.List.toArray
|> Map.fromArray
let get (k: key) (v: 'value t) : 'value option =
Map.get v k
let insert (k: key) (v: 'value) (map: 'value t) : 'value t =
Map.set map k v
let update (k: key) (fn: 'v option -> 'v option) (map: 'value t) : 'value t =
Map.update map k fn
let keys m : key list =
Map.keysToArray m
|> Belt.List.fromArray
end
module Html = struct
include Tea.Html
type 'a html = 'a Vdom.t
end
module Native = struct
exception NativeCodeError of string
module Ext = struct
let window : Dom.window = [%bs.raw "window"]
external getWidth :
Dom.window -> int =
"innerWidth" [@@bs.get]
external getHeight :
Dom.window -> int =
"innerHeight" [@@bs.get]
external getElementsByClassName :
(string -> Dom.element list) =
"getElementsByClassName" [@@bs.val][@@bs.scope "document"]
external querySelectorAll :
(Dom.element -> string -> Dom.element list) =
"querySelectorAll" [@@bs.val]
external getBoundingClientRect :
Dom.element -> bounding_rect =
"getBoundingClientRect" [@@bs.val]
external classList :
Dom.element -> string list =
"classList" [@@bs.get]
end
module Window = struct
let size () : size =
{ width = Ext.getWidth Ext.window
; height = Ext.getHeight Ext.window
}
end
module Random = struct
let random () : int = Js_math.random_int 0 2147483647
end
module Location = struct
external queryString: string = "search" [@@bs.val][@@bs.scope "window", "location"]
external hashString: string = "hash" [@@bs.val][@@bs.scope "window", "location"]
(* TODO write string query parser *)
end
end
module Window = struct
module OnResize = struct
let decode =
let open Tea.Json.Decoder in
let decodeDetail =
map2 (fun width height -> (width, height) )
(field "width" int)
(field "height" int)
in
map (fun msg -> msg)
(field "detail" decodeDetail)
let listen ?(key="") tagger =
registerGlobal "windowResize" key tagger decode
end
module OnFocusChange = struct
let decode =
let open Tea.Json.Decoder in
map (fun visible -> visible)
(field "detail" bool)
let listen?(key="") tagger =
registerGlobal "windowFocusChange" key tagger decode
end
end
module Rollbar = struct
external send : (string -> unit) = "error" [@@bs.val][@@bs.scope "window", "Rollbar"]
end
module DisplayClientError = struct
let decode =
let open Tea.Json.Decoder in
map (fun msg -> msg)
(field "detail" string)
let listen ?(key="") tagger =
registerGlobal "displayError" key tagger decode
end
module OnWheel = struct
let decode =
let open Tea.Json.Decoder in
map2 (fun dX dY -> (dX, dY))
(field "deltaX" int)
(field "deltaY" int)
let listen?(key="") tagger =
registerGlobal "wheel" key tagger decode
end