2023-09-11 12:59:07 +00:00
|
|
|
# formats.nu
|
|
|
|
#
|
|
|
|
# This file contains functions for formatting data in various ways.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# use std format *
|
|
|
|
# use std format <function name>
|
|
|
|
#
|
|
|
|
# These functions help `open` the files with unsupported extensions such as ndjson.
|
|
|
|
#
|
|
|
|
|
2024-04-13 23:59:43 +00:00
|
|
|
# Convert from [NDJSON](https://github.com/ndjson/ndjson-spec) to structured data.
|
2023-09-11 12:59:07 +00:00
|
|
|
export def "from ndjson" []: string -> any {
|
|
|
|
from json --objects
|
|
|
|
}
|
|
|
|
|
2023-10-02 09:50:07 +00:00
|
|
|
# Convert from [JSONL](https://jsonlines.org/) to structured data.
|
2023-09-11 12:59:07 +00:00
|
|
|
export def "from jsonl" []: string -> any {
|
|
|
|
from json --objects
|
|
|
|
}
|
2023-10-02 09:50:07 +00:00
|
|
|
|
2024-04-13 23:59:43 +00:00
|
|
|
# Convert structured data to [NDJSON](https://github.com/ndjson/ndjson-spec).
|
2023-10-02 09:50:07 +00:00
|
|
|
export def "to ndjson" []: any -> string {
|
|
|
|
each { to json --raw } | to text
|
|
|
|
}
|
|
|
|
|
|
|
|
# Convert structured data to [JSONL](https://jsonlines.org/).
|
|
|
|
export def "to jsonl" []: any -> string {
|
|
|
|
each { to json --raw } | to text
|
|
|
|
}
|
2024-11-27 01:43:49 +00:00
|
|
|
|
|
|
|
# Convert from NDNUON (newline-delimited NUON), to structured data
|
|
|
|
export def "from ndnuon" []: [string -> any] {
|
|
|
|
lines | each { from nuon }
|
|
|
|
}
|
|
|
|
|
|
|
|
# Convert structured data to NDNUON, i.e. newline-delimited NUON
|
|
|
|
export def "to ndnuon" []: [any -> string] {
|
2024-12-05 13:53:33 +00:00
|
|
|
each { to nuon --raw | str replace --all "\n" '\n' } | to text
|
2024-11-27 01:43:49 +00:00
|
|
|
}
|