diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-01-13 01:49:30 +0200 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-01-13 01:49:30 +0200 |
| commit | 3c59955180b596f071604eb1313c62c435e12287 (patch) | |
| tree | 36a7fc5e6f65ec2d042f6aa02f3bb43eecef2f26 | |
| parent | 4d453c8585caa6bebd6f952897e924542e5138b1 (diff) | |
bump
| -rw-r--r-- | README.md | 55 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | src/flatmatter.test.ts | 0 | ||||
| -rw-r--r-- | src/index.ts | 3 |
4 files changed, 53 insertions, 7 deletions
@@ -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 diff --git a/package.json b/package.json index 74c39c0..1ead8bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flatmatter", - "version": "0.1.0", + "version": "0.2.0", "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", diff --git a/src/flatmatter.test.ts b/src/flatmatter.test.ts deleted file mode 100644 index e69de29..0000000 --- a/src/flatmatter.test.ts +++ /dev/null diff --git a/src/index.ts b/src/index.ts index 7ac5415..75f9d98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,11 @@ -import FlatMatter, {FlatMatterFn, Serializer} from "./flatmatter.ts"; +import FlatMatter, {FlatMatterFn, Serializer, Matter} from "./flatmatter.ts"; import ToObject from "./serializers/to_object.ts"; import ToJson from "./serializers/to_json.ts"; export { FlatMatter, FlatMatterFn, + Matter, Serializer, ToObject, ToJson, |
