Created
August 1, 2019 15:36
-
-
Save MangelMaxime/2d59603d4d727cb35af9e17825bd5a55 to your computer and use it in GitHub Desktop.
Created with Fable REPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, | |
body { | |
font-size: 16px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Thoth.Json | |
open Fable.Core | |
open Fable.Core.JsInterop | |
module Helpers = | |
open Fable.Core | |
[<Emit("typeof $0")>] | |
let jsTypeof (_ : JsonValue) : string = jsNative | |
[<Emit("$0 instanceof SyntaxError")>] | |
let isSyntaxError (_ : JsonValue) : bool = jsNative | |
let inline getField (fieldName: string) (o: JsonValue) = o?(fieldName) | |
let inline isString (o: JsonValue) : bool = o :? string | |
let inline isBoolean (o: JsonValue) : bool = o :? bool | |
let inline isNumber (o: JsonValue) : bool = jsTypeof o = "number" | |
let inline isArray (o: JsonValue) : bool = JS.Array.isArray(o) | |
[<Emit("$0 === null ? false : (Object.getPrototypeOf($0 || false) === Object.prototype)")>] | |
let isObject (_ : JsonValue) : bool = jsNative | |
let inline isNaN (o: JsonValue) : bool = JS.Number.isNaN(!!o) | |
let inline isNullValue (o: JsonValue): bool = isNull o | |
[<Emit("-2147483648 < $0 && $0 < 2147483647 && ($0 | 0) === $0")>] | |
let isValidIntRange (_: JsonValue) : bool = jsNative | |
[<Emit("isFinite($0) && !($0 % 1)")>] | |
let isIntFinite (_: JsonValue) : bool = jsNative | |
let isUndefined (o: JsonValue): bool = jsTypeof o = "undefined" | |
[<Emit("JSON.stringify($0, null, 4) + ''")>] | |
let anyToString (_: JsonValue) : string = jsNative | |
let inline isFunction (o: JsonValue) : bool = jsTypeof o = "function" | |
let inline objectKeys (o: JsonValue) : string seq = upcast JS.Object.keys(o) | |
let inline asBool (o: JsonValue): bool = unbox o | |
let inline asInt (o: JsonValue): int = unbox o | |
let inline asFloat (o: JsonValue): float = unbox o | |
let inline asString (o: JsonValue): string = unbox o | |
let inline asArray (o: JsonValue): JsonValue[] = unbox o | |
type MenuItem = | |
| MenuItem of string | |
| MenuList of string * MenuItem [] //JS.Map<string, MenuItem list> | |
type MenuConfig = MenuItem [] | |
let test2 = | |
[| | |
MenuList ( | |
"API", | |
[| | |
MenuItem "API/nacara-config-json" | |
MenuList ( | |
"SubMenu", | |
[| | |
MenuItem "API/SubMenu/test" | |
|] | |
) | |
|] | |
) | |
|] | |
let json = | |
""" | |
{ | |
"API": [ | |
"API/nacara-config-json", | |
{ | |
"SubMenu" : [ | |
"API/SubMenu/test" | |
] | |
} | |
] | |
} | |
""" | |
let unwrap path decoder value = | |
Decode.fromValue path decoder value | |
|> function | |
| Ok value -> value | |
| Error msg -> failwith msg | |
let v = JS.JSON.parse(json) | |
type Processor = | |
static member ProcessObject (v : obj) = | |
let keys = Helpers.objectKeys v | |
let values = | |
[ | |
for key in keys do | |
let currentValue = v?(key) | |
if Helpers.isArray currentValue then | |
let items = Helpers.asArray currentValue | |
let x = | |
[ | |
for item in items do | |
yield! Processor.ProcessEntry item | |
// |> Array.map Processor.ProcessEntry | |
] | |
|> List.toArray | |
yield MenuList (key, x) | |
else | |
failwith "An array was expected" | |
// yield MenuList (key, [| MenuItem "Maxime" |]) | |
] | |
values | |
static member ProcessEntry (v : obj) = | |
if Helpers.isString v then | |
let v = | |
v | |
|> Helpers.asString | |
|> MenuItem | |
[ v ] | |
else if Helpers.isObject v then | |
Processor.ProcessObject v | |
else | |
failwith "Expecting a string or an object" | |
let result : MenuConfig = | |
Processor.ProcessObject v | |
|> List.toArray | |
JS.console.log(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'> | |
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> | |
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> | |
<link rel="stylesheet" href="__HOST__/libs/css/bulma.min.css" /> | |
<link rel="stylesheet" href="__HOST__/libs/css/all.min.css" /> | |
</head> | |
<body class="app-container"> | |
<pre> | |
{ | |
"Getting Started": [ | |
"index" | |
], | |
"API": [ | |
"API/nacara-config-json", | |
"API/page-attributes" | |
] | |
} | |
</pre> | |
<div id="elmish-app" class="elmish-app"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment