diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-02-16 01:10:27 +0200 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-02-16 01:10:27 +0200 |
| commit | aba97b1a564845aa12c5b5d41c6f991ef74388c8 (patch) | |
| tree | 54a098ad8509c19244778411b796a947c7ef7dae | |
| parent | 187c2681bdccddadedcbf003c8623f0f8188e99d (diff) | |
Bump 2.0.1
| -rw-r--r-- | README.md | 70 | ||||
| -rw-r--r-- | package.json | 2 |
2 files changed, 11 insertions, 61 deletions
@@ -49,84 +49,34 @@ npm i flatmatter The most basic usage looks like this: ```typescript -import { FlatMatter, ToObject } from "flatmatter"; +import FlatMatter from "flatmatter"; -const fm = new FlatMatter('key: "value"'); -const config = fm.serialize(new ToObject()); +const config = FlatMatter.config('key: "value"'); // {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 { +const toUpper = { + 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. +A FlatMatter function has to have a `name` property and a `compute` callable which takes a string and returns something. 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 from "flatmatter"; -const fm = new FlatMatter('key: "value" / to-upper', [new MyFunction()]); -const config = fm.serialize(new ToObject()); +const config = FlatMatter.config('key: "value" / to-upper', [toUpper]); // {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 { Serializer } from "flatmatter"; - -class ToJson implements Serializer { - serialize(config: Record<string, unknown>): string { - return JSON.stringify(config); - } -} -``` - -**JavaScript:** - -```javascript -class ToJson { - serialize(config) { - return JSON.stringify(config); - } -} -``` - -**Note:** if you don't like classes, you can also pass objects that have the `serialize` callback function. diff --git a/package.json b/package.json index 6d490b4..70c9622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flatmatter", - "version": "2.0.0", + "version": "2.0.1", "description": "A data serialization language, and library, with support for functions.", "author": "Asko Nõmm <asko@nmm.ee> (https://nmm.ee)", "main": "dist/index.js", |
