diff --git a/array.libsonnet b/array.libsonnet index f000c87..8411210 100644 --- a/array.libsonnet +++ b/array.libsonnet @@ -34,4 +34,22 @@ local d = import 'doc-util/main.libsonnet'; else std.length(indexable), }; indexable[invar.index:invar.end:step], + + '#filterMapWithIndex':: d.fn( + ||| + `filterMapWithIndex` works the same as `std.filterMap` with the addition that the index is passed to the functions. + + `filter_func` and `map_func` function signature: `function(index, array_item)` + |||, + [ + d.arg('filter_func', d.T.func), + d.arg('map_func', d.T.func), + d.arg('arr', d.T.array), + ], + ), + filterMapWithIndex(filter_func, map_func, arr): [ + map_func(i, arr[i]) + for i in std.range(0, std.length(arr) - 1) + if filter_func(i, arr[i]) + ], } diff --git a/docs/README.md b/docs/README.md index fbd33a5..61d9c39 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,5 +21,6 @@ in the future, but also provides a place for less general, yet useful utilities. * [date](date.md) * [inspect](inspect.md) * [jsonpath](jsonpath.md) +* [number](number.md) * [string](string.md) * [url](url.md) \ No newline at end of file diff --git a/docs/array.md b/docs/array.md index f273293..cc538d8 100644 --- a/docs/array.md +++ b/docs/array.md @@ -12,10 +12,22 @@ local array = import "github.com/jsonnet-libs/xtd/array.libsonnet" ## Index +* [`fn filterMapWithIndex(filter_func, map_func, arr)`](#fn-filtermapwithindex) * [`fn slice(indexable, index, end='null', step=1)`](#fn-slice) ## Fields +### fn filterMapWithIndex + +```ts +filterMapWithIndex(filter_func, map_func, arr) +``` + +`filterMapWithIndex` works the same as `std.filterMap` with the addition that the index is passed to the functions. + +`filter_func` and `map_func` function signature: `function(index, array_item)` + + ### fn slice ```ts diff --git a/docs/number.md b/docs/number.md new file mode 100644 index 0000000..aa30879 --- /dev/null +++ b/docs/number.md @@ -0,0 +1,43 @@ +--- +permalink: /number/ +--- + +# number + +```jsonnet +local number = import "github.com/jsonnet-libs/xtd/number.libsonnet" +``` + +`number` implements helper functions for processing number. + +## Index + +* [`fn inRange(v, from, to)`](#fn-inrange) +* [`fn maxInArray(arr, default=0)`](#fn-maxinarray) +* [`fn minInArray(arr, default=0)`](#fn-mininarray) + +## Fields + +### fn inRange + +```ts +inRange(v, from, to) +``` + +`inRange` returns true if `v` is in the given from/to range.` + +### fn maxInArray + +```ts +maxInArray(arr, default=0) +``` + +`maxInArray` finds the biggest number in an array + +### fn minInArray + +```ts +minInArray(arr, default=0) +``` + +`minInArray` finds the smallest number in an array \ No newline at end of file diff --git a/main.libsonnet b/main.libsonnet index 59c4034..74a98ad 100644 --- a/main.libsonnet +++ b/main.libsonnet @@ -19,6 +19,7 @@ local d = import 'doc-util/main.libsonnet'; date: (import './date.libsonnet'), inspect: (import './inspect.libsonnet'), jsonpath: (import './jsonpath.libsonnet'), + number: (import './number.libsonnet'), string: (import './string.libsonnet'), url: (import './url.libsonnet'), } diff --git a/number.libsonnet b/number.libsonnet new file mode 100644 index 0000000..f91014a --- /dev/null +++ b/number.libsonnet @@ -0,0 +1,48 @@ +local d = import 'doc-util/main.libsonnet'; + +{ + '#': d.pkg( + name='number', + url='github.com/jsonnet-libs/xtd/number.libsonnet', + help='`number` implements helper functions for processing number.', + ), + + '#inRange':: d.fn( + '`inRange` returns true if `v` is in the given from/to range.`', + [ + d.arg('v', d.T.number), + d.arg('from', d.T.number), + d.arg('to', d.T.number), + ] + ), + inRange(v, from, to): + v > from && v <= to, + + '#maxInArray':: d.fn( + '`maxInArray` finds the biggest number in an array', + [ + d.arg('arr', d.T.array), + d.arg('default', d.T.number, default=0), + ] + ), + maxInArray(arr, default=0): + std.foldl( + std.max, + std.set(arr), + default, + ), + + '#minInArray':: d.fn( + '`minInArray` finds the smallest number in an array', + [ + d.arg('arr', d.T.array), + d.arg('default', d.T.number, default=0), + ] + ), + minInArray(arr, default=0): + std.foldl( + std.min, + std.set(arr), + default, + ), +}