1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# FlatMatter
A YAML-like data serialization language with support for functions. FlatMatter is considered feature-complete and is
intentionally kept simple. Bug fixes and performance improvements are still made, of course.
Example FlatMatter:
```yaml
title: "My Blog"
last_updated: (get-content "posts") / (limit 1) / (get "published_at") / (date "YYYY-mm-dd")
posts: "posts" / get-content
```
FlatMatter aims to be more-or-less syntactically compatible with YAML for the simple reason of not
needing new editor plugins to have syntax highlighting, but it differs in that there is no indentation,
but instead dots to indicate hierarchy, like `site.title` which would result in a `site` object that
contains the `title` key.
FlatMatter also supports functions, allowing you to build your own data DSL, and functions can also be piped with the
forward slash `/` character, meaning that the result of the left operation will be passed as the first argument
of the next function, and so on, to produce an end result.
## Install
```shell
npm i flatmatter
```
## Usage
The most basic usage looks like this:
```typescript
import {FlatMatter, ToObject} from 'flatmatter';
const fm = new FlatMatter('key: "value"');
const config = fm.serialize(new ToObject);
// {key: "value"}
```
However, you most likely want to use it with functions. Those you have to create yourself.
**TypeScript:**
```typescript
import {FlatMatterFn} from "flatmatter";
class ToUpper implements FlatMatterFn {
name = "to-upper";
compute(input: string): unknown {
return input.toUpperCase();
}
}
```
**JavaScript:**
```javascript
class ToUpper {
name = "to-upper";
compute(input) {
return input.toUpperCase();
}
}
```
A FlatMatter function has to implement the `FlatMatterFn` interface. And like I mentioned before, a thing to keep in
mind is that if the function is piped, the first argument passed to it will be the result of the previous operation.
Once you have your functions, simply pass them to FlatMatter like this:
```typescript
import {FlatMatter, ToObject} from 'flatmatter';
const fm = new FlatMatter('key: "value" / to-upper', [new MyFunction()]);
const config = fm.serialize(new ToObject());
// {key: "VALUE"}
```
**Note:** if you don't like classes, you can also pass objects that have the `name` key and the `compute` callback
function.
## Serializers
FlatMatter comes built-in with two very simple serializers: `ToObject` and `ToJson`. You can create your own by
creating a class that implements the `Serializer` interface.
**TypeScript:**
```typescript
import {Matter, Serializer} from 'flatmatter';
class ToJson implements Serializer {
serialize(parsedConfig: Matter): string {
return JSON.stringify(parsedConfig);
}
}
```
**JavaScript:**
```javascript
class ToJson {
serialize(parsedConfig) {
return JSON.stringify(parsedConfig);
}
}
```
**Note:** if you don't like classes, you can also pass objects that have the `serialize` callback function.
|