summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2025-01-13 01:49:30 +0200
committerAsko Nõmm <asko@nmm.ee>2025-01-13 01:49:30 +0200
commit3c59955180b596f071604eb1313c62c435e12287 (patch)
tree36a7fc5e6f65ec2d042f6aa02f3bb43eecef2f26 /README.md
parent4d453c8585caa6bebd6f952897e924542e5138b1 (diff)
bump
Diffstat (limited to 'README.md')
-rw-r--r--README.md55
1 files changed, 50 insertions, 5 deletions
diff --git a/README.md b/README.md
index e85ec81..bdb8f13 100644
--- a/README.md
+++ b/README.md
@@ -31,16 +31,17 @@ npm i flatmatter
The most basic usage looks like this:
```typescript
-import FlatMatter, {ToObject} from "flatmatter";
+import {FlatMatter, ToObject} from 'flatmatter';
const fm = new FlatMatter('key: "value"');
-const config = fm.serialize(new ToObject());
+const config = fm.serialize(new ToObject);
// {key: "value"}
```
-However, you most likely want to use it with functions. Those you have to create yourself. An example function looks
-like this:
+However, you most likely want to use it with functions. Those you have to create yourself.
+
+**TypeScript:**
```typescript
import {FlatMatterFn} from "flatmatter";
@@ -54,16 +55,60 @@ class ToUpper implements FlatMatterFn {
}
```
+**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";
+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. \ No newline at end of file