1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 09:10:40 -05:00
denoland-deno/toml/README.md

119 lines
3.1 KiB
Markdown
Raw Normal View History

2019-03-28 12:31:15 -04:00
# TOML
This module parse TOML files. It follows as much as possible the
[TOML specs](https://github.com/toml-lang/toml). Be sure to read the supported
types as not every specs is supported at the moment and the handling in
TypeScript side is a bit different.
## Supported types and handling
- :heavy_check_mark: [Keys](https://github.com/toml-lang/toml#string)
- :exclamation: [String](https://github.com/toml-lang/toml#string)
- :heavy_check_mark:
[Multiline String](https://github.com/toml-lang/toml#string)
- :heavy_check_mark: [Literal String](https://github.com/toml-lang/toml#string)
- :exclamation: [Integer](https://github.com/toml-lang/toml#integer)
- :heavy_check_mark: [Float](https://github.com/toml-lang/toml#float)
- :heavy_check_mark: [Boolean](https://github.com/toml-lang/toml#boolean)
- :heavy_check_mark:
[Offset Date-time](https://github.com/toml-lang/toml#offset-date-time)
- :heavy_check_mark:
[Local Date-time](https://github.com/toml-lang/toml#local-date-time)
- :heavy_check_mark: [Local Date](https://github.com/toml-lang/toml#local-date)
- :exclamation: [Local Time](https://github.com/toml-lang/toml#local-time)
- :heavy_check_mark: [Table](https://github.com/toml-lang/toml#table)
- :heavy_check_mark: [Inline Table](https://github.com/toml-lang/toml#inline-table)
2019-03-28 12:31:15 -04:00
- :exclamation: [Array of Tables](https://github.com/toml-lang/toml#array-of-tables)
:exclamation: _Supported with warnings see [Warning](#Warning)._
### :warning: Warning
#### String
- Regex : Due to the spec, there is no flag to detect regex properly
in a TOML declaration. So the regex is stored as string.
#### Integer
For **Binary** / **Octal** / **Hexadecimal** numbers,
they are stored as string to be not interpreted as Decimal.
#### Local Time
Because local time does not exist in JavaScript, the local time is stored as a string.
#### Inline Table
Inline tables are supported. See below:
2019-03-28 12:31:15 -04:00
```toml
animal = { type = { name = "pug" } }
# Output
2019-03-28 12:31:15 -04:00
animal = { type.name = "pug" }
# Output { animal : { type : { name : "pug" } }
animal.as.leaders = "tosin"
# Output { animal: { as: { leaders: "tosin" } } }
"tosin.abasi" = "guitarist"
# Output
"tosin.abasi" : "guitarist"
2019-03-28 12:31:15 -04:00
```
#### Array of Tables
At the moment only simple declarations like below are supported:
```toml
[[bin]]
name = "deno"
path = "cli/main.rs"
[[bin]]
name = "deno_core"
path = "src/foo.rs"
[[nib]]
name = "node"
path = "not_found"
```
will output:
```json
{
"bin": [
{ "name": "deno", "path": "cli/main.rs" },
{ "name": "deno_core", "path": "src/foo.rs" }
],
"nib": [{ "name": "node", "path": "not_found" }]
}
```
## Usage
2019-04-05 00:23:05 -04:00
### Parse
2019-03-28 12:31:15 -04:00
```ts
2019-04-25 18:47:18 -04:00
import { parse } from "./parser.ts";
import { readFileStrSync } from "../fs/read_file_str.ts";
2019-03-28 12:31:15 -04:00
2019-04-25 18:47:18 -04:00
const tomlObject = parse(readFileStrSync("file.toml"));
2019-03-28 12:31:15 -04:00
const tomlString = 'foo.bar = "Deno"';
const tomlObject22 = parse(tomlString);
```
2019-04-05 00:23:05 -04:00
### Stringify
```ts
import { stringify } from "./parser.ts";
const obj = {
bin: [
{ name: "deno", path: "cli/main.rs" },
{ name: "deno_core", path: "src/foo.rs" }
],
nib: [{ name: "node", path: "not_found" }]
};
const tomlString = stringify(obj);
```