Skip to content

Commit

Permalink
feat(array/number): add helpers (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic authored Jan 4, 2024
1 parent c1a315a commit 80bdea4
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
18 changes: 18 additions & 0 deletions array.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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])
],
}
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions docs/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions docs/number.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions main.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
}
48 changes: 48 additions & 0 deletions number.libsonnet
Original file line number Diff line number Diff line change
@@ -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,
),
}

0 comments on commit 80bdea4

Please sign in to comment.