From 4cda1bf4888210d4d9716ddab9b636e662916a76 Mon Sep 17 00:00:00 2001 From: Marcin Jaworski Date: Fri, 13 Nov 2020 17:10:04 +0100 Subject: [PATCH 1/2] Added ability to return default value from struct_val() if keyword is not present --- structs.scad | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/structs.scad b/structs.scad index 15831ce..c37d62a 100644 --- a/structs.scad +++ b/structs.scad @@ -64,16 +64,17 @@ function struct_remove(struct, keyword) = // Function: struct_val() // Usage: -// struct_val(struct,keyword) +// struct_val(struct, keyword, default) // Description: -// Returns the value for the specified keyword in the structure, or undef if the keyword is not present +// Returns the value for the specified keyword in the structure, or default value if the keyword is not present // Arguments: // struct = input structure // keyword = keyword whose value to return -function struct_val(struct,keyword) = +// default = default value to return if keyword is not present, defaults to undef +function struct_val(struct, keyword, default=undef) = assert(is_def(keyword),"keyword is missing") let(ind = search([keyword],struct)[0]) - ind == [] ? undef : struct[ind][1]; + ind == [] ? default : struct[ind][1]; // Function: struct_keys() From 8cd753074318c0a39b50a9c5aecadc684e741b26 Mon Sep 17 00:00:00 2001 From: Marcin Jaworski Date: Fri, 13 Nov 2020 17:19:29 +0100 Subject: [PATCH 2/2] Added tests for struct_val() with default value --- tests/test_structs.scad | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_structs.scad b/tests/test_structs.scad index a4a4d92..fdb8d35 100644 --- a/tests/test_structs.scad +++ b/tests/test_structs.scad @@ -27,6 +27,9 @@ module test_struct_val() { assert(struct_val(st,"Foo") == 91); assert(struct_val(st,"Bar") == 28); assert(struct_val(st,"Baz") == 9); + assert(struct_val(st,"Baz",5) == 9); + assert(struct_val(st,"Qux") == undef); + assert(struct_val(st,"Qux",5) == 5); } test_struct_val();