Skip to content

Commit

Permalink
constraint functions naming
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Apr 22, 2024
1 parent 2b4944c commit 39c7943
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
11 changes: 5 additions & 6 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,9 @@ fn string_map(s: String, f: fn(String) -> String) -> String {
pub type Constraint(a) =
fn(a) -> snag.Result(a)

/// one_of returns a Constraint that ensures the parsed flag value is
/// one of the allowed values.
/// returns a Constraint that ensures the parsed flag value is one of the allowed values.
///
pub fn one_of(allowed: List(a)) -> Constraint(a) {
pub fn constrain_one_of(allowed: List(a)) -> Constraint(a) {
let allowed_set = set.from_list(allowed)
fn(val: a) -> snag.Result(a) {
case set.contains(allowed_set, val) {
Expand All @@ -848,9 +847,9 @@ pub fn one_of(allowed: List(a)) -> Constraint(a) {
}
}

/// none_of returns a Constraint that ensures the parsed flag value is not one of the disallowed values.
/// returns a Constraint that ensures the parsed flag value is not one of the disallowed values.
///
pub fn none_of(disallowed: List(a)) -> Constraint(a) {
pub fn constrain_none_of(disallowed: List(a)) -> Constraint(a) {
let disallowed_set = set.from_list(disallowed)
fn(val: a) -> snag.Result(a) {
case set.contains(disallowed_set, val) {
Expand Down Expand Up @@ -880,7 +879,7 @@ pub fn none_of(disallowed: List(a)) -> Constraint(a) {
/// ```gleam
/// [1, 2, 3, 4] |> one_of |> each
/// ```
pub fn each(constraint: Constraint(a)) -> Constraint(List(a)) {
pub fn constrain_each(constraint: Constraint(a)) -> Constraint(List(a)) {
fn(l: List(a)) -> snag.Result(List(a)) {
l
|> list.try_map(constraint)
Expand Down
62 changes: 31 additions & 31 deletions test/contraint_test.gleam
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
import gleeunit/should
import glint.{each, none_of, one_of}
import glint

pub fn one_of_test() {
1
|> one_of([1, 2, 3])
|> glint.constrain_one_of([1, 2, 3])
|> should.equal(Ok(1))

1
|> one_of([2, 3, 4])
|> glint.constrain_one_of([2, 3, 4])
|> should.be_error()

[1, 2, 3]
|> {
[5, 4, 3, 2, 1]
|> one_of
|> each
|> glint.constrain_one_of
|> glint.constrain_each
}
|> should.equal(Ok([1, 2, 3]))

[1, 6, 3]
|> {
[5, 4, 3, 2, 1]
|> one_of
|> each
|> glint.constrain_one_of
|> glint.constrain_each
}
|> should.be_error()
}

pub fn none_of_test() {
1
|> none_of([1, 2, 3])
|> glint.constrain_none_of([1, 2, 3])
|> should.be_error

1
|> glint.none_of([2, 3, 4])
|> glint.constrain_none_of([2, 3, 4])
|> should.equal(Ok(1))

[1, 2, 3]
|> {
[4, 5, 6, 7, 8]
|> none_of
|> each
|> glint.constrain_none_of
|> glint.constrain_each
}
|> should.equal(Ok([1, 2, 3]))

[1, 6, 3]
|> {
[4, 5, 6, 7, 8]
|> none_of
|> each
|> glint.constrain_none_of
|> glint.constrain_each
}
|> should.be_error()
}

pub fn flag_one_of_none_of_test() {
let #(test_flag, success, failure) = #(
glint.int("i")
|> glint.constraint(one_of([1, 2, 3]))
|> glint.constraint(none_of([4, 5, 6])),
|> glint.constraint(glint.constrain_one_of([1, 2, 3]))
|> glint.constraint(glint.constrain_none_of([4, 5, 6])),
"1",
"6",
)
Expand Down Expand Up @@ -86,13 +86,13 @@ pub fn flag_one_of_none_of_test() {
glint.ints("li")
|> glint.constraint(
[1, 2, 3]
|> one_of
|> each,
|> glint.constrain_one_of
|> glint.constrain_each,
)
|> glint.constraint(
[4, 5, 6]
|> none_of
|> each,
|> glint.constrain_none_of
|> glint.constrain_each,
),
"1,1,1",
"2,2,6",
Expand Down Expand Up @@ -120,8 +120,8 @@ pub fn flag_one_of_none_of_test() {

let #(test_flag, success, failure) = #(
glint.float("f")
|> glint.constraint(one_of([1.0, 2.0, 3.0]))
|> glint.constraint(none_of([4.0, 5.0, 6.0])),
|> glint.constraint(glint.constrain_one_of([1.0, 2.0, 3.0]))
|> glint.constraint(glint.constrain_none_of([4.0, 5.0, 6.0])),
"1.0",
"6.0",
)
Expand Down Expand Up @@ -149,13 +149,13 @@ pub fn flag_one_of_none_of_test() {
glint.floats("lf")
|> glint.constraint(
[1.0, 2.0, 3.0]
|> one_of()
|> each,
|> glint.constrain_one_of()
|> glint.constrain_each,
)
|> glint.constraint(
[4.0, 5.0, 6.0]
|> none_of()
|> each,
|> glint.constrain_none_of()
|> glint.constrain_each,
),
"3.0,2.0,1.0",
"2.0,3.0,6.0",
Expand All @@ -182,8 +182,8 @@ pub fn flag_one_of_none_of_test() {

let #(test_flag, success, failure) = #(
glint.string("s")
|> glint.constraint(one_of(["t1", "t2", "t3"]))
|> glint.constraint(none_of(["t4", "t5", "t6"])),
|> glint.constraint(glint.constrain_one_of(["t1", "t2", "t3"]))
|> glint.constraint(glint.constrain_none_of(["t4", "t5", "t6"])),
"t3",
"t4",
)
Expand Down Expand Up @@ -212,13 +212,13 @@ pub fn flag_one_of_none_of_test() {
glint.strings("ls")
|> glint.constraint(
["t1", "t2", "t3"]
|> one_of
|> each,
|> glint.constrain_one_of
|> glint.constrain_each,
)
|> glint.constraint(
["t4", "t5", "t6"]
|> none_of
|> each,
|> glint.constrain_none_of
|> glint.constrain_each,
),
"t3,t2,t1",
"t2,t4,t1",
Expand Down

0 comments on commit 39c7943

Please sign in to comment.