Skip to content

Commit

Permalink
handle on/off in bools, and treat empty strings as None
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Sep 23, 2024
1 parent 5b822db commit 69e45b7
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 71 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# master

- Add `allowEmptyString` option to `FormDataHelpers.getString`, and make sure that empty strings are treated as `None` by default.
- Handle `on` and `off` values for `FormDataHelpers.getBool`.

# 0.1.1

- Fix issue with Vite transform and CSS files with imports.
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
"author": "Gabriel Nordeborn",
"license": "MIT",
"peerDependencies": {
"rescript": ">=11.1.0-rc.2",
"rescript": ">=11.1.0",
"@rescript/core": ">=1.0.0",
"vite": ">=4.4.11",
"rescript-bun": ">=0.4.1"
},
"devDependencies": {
"@rescript/core": "1.0.0",
"@rescript/core": "1.6.0",
"fast-glob": "^3.3.1",
"rescript": "11.1.0-rc.2",
"rescript-bun": "0.4.1"
"rescript": "11.1.4",
"rescript-bun": "0.5.0"
},
"dependencies": {
"fast-glob": "^3.3.1"
Expand Down
2 changes: 1 addition & 1 deletion src/BunUtils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/BunUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let serveStaticFile = async request => {
let pathname = url->URL.pathname

let path = pathname->String.split("/")->Array.filter(p => p !== "")
let joined = path->Array.joinWith("/")
let joined = path->Array.join("/")

switch staticFiles->Map.get(joined) {
| None => None
Expand Down
17 changes: 11 additions & 6 deletions src/FormDataHelpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/FormDataHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ let getOrRaise = (opt, ~name, ~expectedType, ~message=?) =>
| Some(v) => v
}

let getString = (t, name) =>
let getString = (t, name, ~allowEmptyString=false) =>
switch t->FormData.get(name) {
| String(s) => Some(s)
| String(s) =>
switch s {
| "" if allowEmptyString => Some("")
| _ => Some(s)
}
| _ => None
}

let getInt = (t, name) => t->getString(name)->Option.flatMap(s => s->Int.fromString)
let getFloat = (t, name) => t->getString(name)->Option.flatMap(s => s->Float.fromString)
let getBool = (t, name) =>
switch t->FormData.get(name) {
| String("true") => Some(true)
| String("false") => Some(false)
| String("true" | "on") => Some(true)
| String("false" | "off") => Some(false)
| _ => None
}

Expand Down
4 changes: 2 additions & 2 deletions src/Handlers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Handlers.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ let make = (~requestToContext, ~options=?) => {

let useContext = t => t.asyncLocalStorage->AsyncHooks.AsyncLocalStorage.getStoreUnsafe

let defaultRenderTitle = segments => segments->Array.joinWith(" | ")
let defaultRenderTitle = segments => segments->Array.join(" | ")

@module("./vendor/hyperons.js")
external escapeString: string => string = "escapeString"
Expand Down
8 changes: 4 additions & 4 deletions src/Htmx.res
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Swap = {
),
]
->Array.keepSome
->Array.joinWith(" ")
->Array.join(" ")
}

type hxTarget =
Expand Down Expand Up @@ -71,8 +71,8 @@ module Params = {
switch p {
| IncludeAll => "*"
| IncludeNone => "none"
| Not(list) => `not ${list->Array.joinWith(",")}`
| Only(list) => list->Array.joinWith(",")
| Not(list) => `not ${list->Array.join(",")}`
| Only(list) => list->Array.join(",")
}
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ module Disinherit = {
let make = d =>
switch d {
| All => "*"
| Attributes(attrs) => attrs->Array.map(a => (a :> string))->Array.joinWith(" ")
| Attributes(attrs) => attrs->Array.map(a => (a :> string))->Array.join(" ")
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ResXClient.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/StaticExporter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/StaticExporter.res
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ let run = async (server: Server.t, ~urls: array<string>) => {

switch dirStructure {
| [] => ()
| dirStructure => await Fs.mkdir(dirStructure->Array.joinWith("/"), {recursive: true})
| dirStructure => await Fs.mkdir(dirStructure->Array.join("/"), {recursive: true})
}

dirStructure->Array.push(fileName)
let filePath = dirStructure->Array.joinWith("/")
let filePath = dirStructure->Array.join("/")

let _ = await Bun.Write.writeResponseToFile(
~file=dirStructure->Array.joinWith("/")->Bun.file,
~file=dirStructure->Array.join("/")->Bun.file,
~response=res,
)

Expand Down
10 changes: 5 additions & 5 deletions src/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ module CacheControl = {
} else {
None
},
expiration->Option.map(e => e->Array.map(expirationToString)->Array.joinWith(", ")),
revalidation->Option.map(r => r->Array.map(revalidationToString)->Array.joinWith(", ")),
modifiers->Option.map(r => r->Array.map(modifiersToString)->Array.joinWith(", ")),
extensions->Option.map(r => r->Array.map(extensionsToString)->Array.joinWith(", ")),
expiration->Option.map(e => e->Array.map(expirationToString)->Array.join(", ")),
revalidation->Option.map(r => r->Array.map(revalidationToString)->Array.join(", ")),
modifiers->Option.map(r => r->Array.map(modifiersToString)->Array.join(", ")),
extensions->Option.map(r => r->Array.map(extensionsToString)->Array.join(", ")),
]
->Array.keepSome
->Array.joinWith(", ")
->Array.join(", ")
}

module Presets = {
Expand Down
4 changes: 2 additions & 2 deletions test/FormActionHandlers.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/HtmlRendering.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/HtmxHandlers.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 69e45b7

Please sign in to comment.