Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ in the future, but also provides a place for less general, yet useful utilities.
* [jsonpath](jsonpath.md)
* [number](number.md)
* [string](string.md)
* [units](units.md)
* [url](url.md)
61 changes: 61 additions & 0 deletions docs/units.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
permalink: /units/
---

# units

```jsonnet
local units = import "github.com/jsonnet-libs/xtd/units.libsonnet"
```

`units` implements helper functions for converting units.

## Index

* [`fn formatDuration(seconds)`](#fn-formatduration)
* [`fn parseDuration(duration)`](#fn-parseduration)
* [`fn parseKubernetesCPU(input)`](#fn-parsekubernetescpu)
* [`fn siToBytes(str)`](#fn-sitobytes)

## Fields

### fn formatDuration

```ts
formatDuration(seconds)
```

`formatDuration` formats a number of seconds into a human-readable duration string.
Returns the duration in the smallest appropriate unit (s, m, h, or combined formats like "4m30s").


### fn parseDuration

```ts
parseDuration(duration)
```

`parseDuration` parses a duration string and returns the number of seconds.
Handles milliseconds (ms), seconds (s), minutes (m), hours (h), and combined formats like "4m30s" or "1h30m".


### fn parseKubernetesCPU

```ts
parseKubernetesCPU(input)
```

`parseKubernetesCPU` parses a Kubernetes CPU string/number into a number of cores.
The function assumes the input is in a correct Kubernetes format, i.e., an integer, a float,
a string representation of an integer or a float, or a string containing a number ending with 'm'
representing a number of millicores.


### fn siToBytes

```ts
siToBytes(str)
```

`siToBytes` converts Kubernetes byte units to bytes.
Only works for limited set of SI prefixes: Ki, Mi, Gi, Ti.
1 change: 1 addition & 0 deletions main.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
jsonpath: (import './jsonpath.libsonnet'),
number: (import './number.libsonnet'),
string: (import './string.libsonnet'),
units: (import './units.libsonnet'),
url: (import './url.libsonnet'),
}
248 changes: 248 additions & 0 deletions test/units_test.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
local units = import '../units.libsonnet';
local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet';

test.new(std.thisFile)
// parseCPU(10) = parseCPU("10") = 10
// parseCPU(4.5) = parse("4.5") = 4.5
// parseCPU("3000m") = 3000 / 1000
// parseCPU("3580m") = 3580 / 1000
// parseCPU("3980.7m") = 3980.7 / 1000
// parseCPU(0.5) = parse("0.5") = parse("500m") = 0.5
+ test.case.new(
name='parseCPU - integer',
test=test.expect.eq(
actual=units.parseKubernetesCPU(10),
expected=10,
)
)
+ test.case.new(
name='parseCPU - string integer',
test=test.expect.eq(
actual=units.parseKubernetesCPU('10'),
expected=10,
)
)
+ test.case.new(
name='parseCPU - float',
test=test.expect.eq(
actual=units.parseKubernetesCPU(4.5),
expected=4.5,
)
)
+ test.case.new(
name='parseCPU - string float',
test=test.expect.eq(
actual=units.parseKubernetesCPU('4.5'),
expected=4.5,
)
)
+ test.case.new(
name='parseCPU - string millicores',
test=test.expect.eq(
actual=units.parseKubernetesCPU('3000m'),
expected=3,
)
)
+ test.case.new(
name='parseCPU - string millicores',
test=test.expect.eq(
actual=units.parseKubernetesCPU('3580m'),
expected=3.58,
)
)
+ test.case.new(
name='parseCPU - string millicores',
test=test.expect.eq(
actual=units.parseKubernetesCPU('3980.7m'),
expected=3980.7 / 1000, // Use a division to avoid floating point precision issues
)
)
+ test.case.new(
name='parseCPU - fraction',
test=test.expect.eq(
actual=units.parseKubernetesCPU(0.5),
expected=0.5,
)
)
+ test.case.new(
name='parseCPU - string fraction',
test=test.expect.eq(
actual=units.parseKubernetesCPU('0.5'),
expected=0.5,
)
)
+ test.case.new(
name='parseCPU - fraction millicores',
test=test.expect.eq(
actual=units.parseKubernetesCPU('500m'),
expected=0.5,
)
)

// siToBytes tests
+ test.case.new(
name='siToBytes - plain number',
test=test.expect.eq(
actual=units.siToBytes('1024'),
expected=1024,
)
)
+ test.case.new(
name='siToBytes - Ki',
test=test.expect.eq(
actual=units.siToBytes('1Ki'),
expected=1024,
)
)
+ test.case.new(
name='siToBytes - Mi',
test=test.expect.eq(
actual=units.siToBytes('1Mi'),
expected=1048576,
)
)
+ test.case.new(
name='siToBytes - Gi',
test=test.expect.eq(
actual=units.siToBytes('2Gi'),
expected=2147483648,
)
)
+ test.case.new(
name='siToBytes - Ti',
test=test.expect.eq(
actual=units.siToBytes('1Ti'),
expected=1099511627776,
)
)
+ test.case.new(
name='siToBytes - fractional Ki',
test=test.expect.eq(
actual=units.siToBytes('1.5Ki'),
expected=1536,
)
)

// parseDuration tests
+ test.case.new(
name='parseDuration - milliseconds',
test=test.expect.eq(
actual=units.parseDuration('500ms'),
expected=0.5,
)
)
+ test.case.new(
name='parseDuration - seconds',
test=test.expect.eq(
actual=units.parseDuration('30s'),
expected=30,
)
)
+ test.case.new(
name='parseDuration - minutes',
test=test.expect.eq(
actual=units.parseDuration('5m'),
expected=300,
)
)
+ test.case.new(
name='parseDuration - hours',
test=test.expect.eq(
actual=units.parseDuration('2h'),
expected=7200,
)
)
+ test.case.new(
name='parseDuration - combined minutes and seconds',
test=test.expect.eq(
actual=units.parseDuration('4m30s'),
expected=270,
)
)
+ test.case.new(
name='parseDuration - combined hours and fractional seconds',
test=test.expect.eq(
actual=units.parseDuration('1h30.5s'),
expected=3600 + 30.5,
)
)
+ test.case.new(
name='parseDuration - combined hours and minutes',
test=test.expect.eq(
actual=units.parseDuration('1h30m'),
expected=5400,
)
)
+ test.case.new(
name='parseDuration - combined hours and milliseconds',
test=test.expect.eq(
actual=units.parseDuration('1h500ms'),
expected=3600 + 0.5,
)
)

// formatDuration tests
+ test.case.new(
name='formatDuration - milliseconds',
test=test.expect.eq(
actual=units.formatDuration(0.5),
expected='500ms',
)
)
+ test.case.new(
name='formatDuration - seconds',
test=test.expect.eq(
actual=units.formatDuration(45),
expected='45s',
)
)
+ test.case.new(
name='formatDuration - minutes',
test=test.expect.eq(
actual=units.formatDuration(300),
expected='5m',
)
)
+ test.case.new(
name='formatDuration - hours',
test=test.expect.eq(
actual=units.formatDuration(7200),
expected='2h',
)
)
+ test.case.new(
name='formatDuration - combined minutes and seconds',
test=test.expect.eq(
actual=units.formatDuration(270),
expected='4m30s',
)
)
+ test.case.new(
name='formatDuration - combined hours and minutes',
test=test.expect.eq(
actual=units.formatDuration(5400),
expected='1h30m',
)
)
+ test.case.new(
name='formatDuration - combined hours and minutes and seconds',
test=test.expect.eq(
actual=units.formatDuration(5400 + 30),
expected='1h30m30s',
)
)
+ test.case.new(
name='formatDuration - combined hours and minutes and seconds and milliseconds',
test=test.expect.eq(
actual=units.formatDuration(5400 + 30 + 0.5),
expected='1h30m30s500ms',
)
)

+ test.case.new(
name='formatDuration - combined hours and milliseconds',
test=test.expect.eq(
actual=units.formatDuration(3600 + 0.5),
expected='1h500ms',
)
)
Loading
Loading