summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md34
-rw-r--r--lib.c35
2 files changed, 68 insertions, 1 deletions
diff --git a/README.md b/README.md
index 929d261..0d98fd1 100644
--- a/README.md
+++ b/README.md
@@ -1192,3 +1192,37 @@ file path.
If `depth` exceeds the size of the call stack, the function returns `null`
as well.
+
+#### 6.60. `min([val1 [, val2 [, ...]]])`
+
+Return the smallest value among all parameters passed to the function.
+The function does a `val1 < val2` comparison internally, which means that
+the same value coercion rules as for relational operators apply. If both
+strings and numbers are passed to `min()`, then any string values will be
+effectively ignored since both `1 < "abc"` and `1 > "abc"` comparisons
+yield false results.
+
+```javascript
+min(5, 2.1, 3, "abc", 0.3); // 0.3
+min(1, "abc"); // 1
+min("1", "abc"); // "1"
+min("def", "abc", "ghi"); // "abc"
+min(true, false); // false
+```
+
+#### 6.61. `max([val1 [, val2 [, ...]]])`
+
+Return the largest value among all parameters passed to the function.
+The function does a `val1 > val2` comparison internally, which means that
+the same value coercion rules as for relational operators apply. If both
+strings and numbers are passed to `min()`, then any string values will be
+effectively ignored since both `1 < "abc"` and `1 > "abc"` comparisons
+yield false results.
+
+```javascript
+max(5, 2.1, 3, "abc", 0.3); // 5
+max(1, "abc"); // 1 (!)
+max("1", "abc"); // "abc"
+max("def", "abc", "ghi"); // "ghi"
+max(true, false); // true
+```
diff --git a/lib.c b/lib.c
index 4d09ab0..c9ebcb2 100644
--- a/lib.c
+++ b/lib.c
@@ -2583,6 +2583,37 @@ uc_sourcepath(uc_vm *vm, size_t nargs)
return rv;
}
+static uc_value_t *
+uc_min_max(uc_vm *vm, size_t nargs, int cmp)
+{
+ uc_value_t *rv = NULL, *val;
+ bool set = false;
+ size_t i;
+
+ for (i = 0; i < nargs; i++) {
+ val = uc_get_arg(i);
+
+ if (!set || uc_cmp(cmp, val, rv)) {
+ set = true;
+ rv = val;
+ }
+ }
+
+ return ucv_get(rv);
+}
+
+static uc_value_t *
+uc_min(uc_vm *vm, size_t nargs)
+{
+ return uc_min_max(vm, nargs, TK_LT);
+}
+
+static uc_value_t *
+uc_max(uc_vm *vm, size_t nargs)
+{
+ return uc_min_max(vm, nargs, TK_GT);
+}
+
static const uc_cfunction_list functions[] = {
{ "chr", uc_chr },
{ "die", uc_die },
@@ -2636,7 +2667,9 @@ static const uc_cfunction_list functions[] = {
{ "render", uc_render },
{ "regexp", uc_regexp },
{ "wildcard", uc_wildcard },
- { "sourcepath", uc_sourcepath }
+ { "sourcepath", uc_sourcepath },
+ { "min", uc_min },
+ { "max", uc_max }
};