summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2025-01-28 21:29:22 +0200
committerAsko Nõmm <asko@nmm.ee>2025-01-28 21:29:22 +0200
commit95692cf5540f1c0777ec08464ad5cc2a420406d6 (patch)
tree2746ecd1664ae1679d019d2d697aa6a092782e76
parent8a53d6c1870533c5020b00a509025293caa2637e (diff)
Improve docs, namings
-rw-r--r--README.md25
-rw-r--r--package-lock.json177
-rw-r--r--src/flatmatter.test.ts41
-rw-r--r--src/flatmatter.ts654
-rw-r--r--src/serializers/to_json.test.ts12
-rw-r--r--src/serializers/to_json.ts6
-rw-r--r--src/serializers/to_object.test.ts31
-rw-r--r--src/serializers/to_object.ts8
-rw-r--r--src/utils.ts4
-rw-r--r--tsconfig.json16
-rw-r--r--vite.config.ts9
11 files changed, 467 insertions, 516 deletions
diff --git a/README.md b/README.md
index 1522392..787736a 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,6 @@ Additionally, FlatMatter also parses FrontMatter content, meaning that a FlatMat
---
title: "Hello, World!"
---
-
Content goes here.
```
@@ -34,8 +33,8 @@ Would result in this data being created:
```json
{
- "title": "Hello, World!",
- "content": "Content goes here."
+ "title": "Hello, World!",
+ "content": "Content goes here."
}
```
@@ -50,10 +49,10 @@ 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"}
```
@@ -63,7 +62,7 @@ However, you most likely want to use it with functions. Those you have to create
**TypeScript:**
```typescript
-import {FlatMatterFn} from "flatmatter";
+import { FlatMatterFn } from "flatmatter";
class ToUpper implements FlatMatterFn {
name = "to-upper";
@@ -92,7 +91,7 @@ mind is that if the function is piped, the first argument passed to it will be t
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());
@@ -111,11 +110,11 @@ creating a class that implements the `Serializer` interface.
**TypeScript:**
```typescript
-import {Matter, Serializer} from 'flatmatter';
+import { Serializer } from "flatmatter";
class ToJson implements Serializer {
- serialize(parsedConfig: Matter): string {
- return JSON.stringify(parsedConfig);
+ serialize(config: Record<string, unknown>): string {
+ return JSON.stringify(config);
}
}
```
@@ -124,10 +123,10 @@ class ToJson implements Serializer {
```javascript
class ToJson {
- serialize(parsedConfig) {
- return JSON.stringify(parsedConfig);
+ serialize(config) {
+ return JSON.stringify(config);
}
}
```
-**Note:** if you don't like classes, you can also pass objects that have the `serialize` callback function. \ No newline at end of file
+**Note:** if you don't like classes, you can also pass objects that have the `serialize` callback function.
diff --git a/package-lock.json b/package-lock.json
index 61c0397..c75d73a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "flatmatter",
- "version": "1.1.0",
+ "version": "1.1.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "flatmatter",
- "version": "1.1.0",
+ "version": "1.1.1",
"license": "MIT",
"devDependencies": {
"@types/node": "^22.10.10",
@@ -17,6 +17,9 @@
"typescript": "^5.7.3",
"vite": "^5.4.11",
"vitest": "^2.1.8"
+ },
+ "engines": {
+ "node": ">=18"
}
},
"node_modules/@ampproject/remapping": {
@@ -815,9 +818,9 @@
"license": "MIT"
},
"node_modules/@rollup/rollup-android-arm-eabi": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.32.0.tgz",
- "integrity": "sha512-G2fUQQANtBPsNwiVFg4zKiPQyjVKZCUdQUol53R8E71J7AsheRMV/Yv/nB8giOcOVqP7//eB5xPqieBYZe9bGg==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.32.1.tgz",
+ "integrity": "sha512-/pqA4DmqyCm8u5YIDzIdlLcEmuvxb0v8fZdFhVMszSpDTgbQKdw3/mB3eMUHIbubtJ6F9j+LtmyCnHTEqIHyzA==",
"cpu": [
"arm"
],
@@ -829,9 +832,9 @@
]
},
"node_modules/@rollup/rollup-android-arm64": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.32.0.tgz",
- "integrity": "sha512-qhFwQ+ljoymC+j5lXRv8DlaJYY/+8vyvYmVx074zrLsu5ZGWYsJNLjPPVJJjhZQpyAKUGPydOq9hRLLNvh1s3A==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.32.1.tgz",
+ "integrity": "sha512-If3PDskT77q7zgqVqYuj7WG3WC08G1kwXGVFi9Jr8nY6eHucREHkfpX79c0ACAjLj3QIWKPJR7w4i+f5EdLH5Q==",
"cpu": [
"arm64"
],
@@ -843,9 +846,9 @@
]
},
"node_modules/@rollup/rollup-darwin-arm64": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.32.0.tgz",
- "integrity": "sha512-44n/X3lAlWsEY6vF8CzgCx+LQaoqWGN7TzUfbJDiTIOjJm4+L2Yq+r5a8ytQRGyPqgJDs3Rgyo8eVL7n9iW6AQ==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.32.1.tgz",
+ "integrity": "sha512-zCpKHioQ9KgZToFp5Wvz6zaWbMzYQ2LJHQ+QixDKq52KKrF65ueu6Af4hLlLWHjX1Wf/0G5kSJM9PySW9IrvHA==",
"cpu": [
"arm64"
],
@@ -857,9 +860,9 @@
]
},
"node_modules/@rollup/rollup-darwin-x64": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.32.0.tgz",
- "integrity": "sha512-F9ct0+ZX5Np6+ZDztxiGCIvlCaW87HBdHcozUfsHnj1WCUTBUubAoanhHUfnUHZABlElyRikI0mgcw/qdEm2VQ==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.32.1.tgz",
+ "integrity": "sha512-sFvF+t2+TyUo/ZQqUcifrJIgznx58oFZbdHS9TvHq3xhPVL9nOp+yZ6LKrO9GWTP+6DbFtoyLDbjTpR62Mbr3Q==",
"cpu": [
"x64"
],
@@ -871,9 +874,9 @@
]
},
"node_modules/@rollup/rollup-freebsd-arm64": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.32.0.tgz",
- "integrity": "sha512-JpsGxLBB2EFXBsTLHfkZDsXSpSmKD3VxXCgBQtlPcuAqB8TlqtLcbeMhxXQkCDv1avgwNjF8uEIbq5p+Cee0PA==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.32.1.tgz",
+ "integrity": "sha512-NbOa+7InvMWRcY9RG+B6kKIMD/FsnQPH0MWUvDlQB1iXnF/UcKSudCXZtv4lW+C276g3w5AxPbfry5rSYvyeYA==",
"cpu": [
"arm64"
],
@@ -885,9 +888,9 @@
]
},
"node_modules/@rollup/rollup-freebsd-x64": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.32.0.tgz",
- "integrity": "sha512-wegiyBT6rawdpvnD9lmbOpx5Sph+yVZKHbhnSP9MqUEDX08G4UzMU+D87jrazGE7lRSyTRs6NEYHtzfkJ3FjjQ==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.32.1.tgz",
+ "integrity": "sha512-JRBRmwvHPXR881j2xjry8HZ86wIPK2CcDw0EXchE1UgU0ubWp9nvlT7cZYKc6bkypBt745b4bglf3+xJ7hXWWw==",
"cpu": [
"x64"
],
@@ -899,9 +902,9 @@
]
},
"node_modules/@rollup/rollup-linux-arm-gnueabihf": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.32.0.tgz",
- "integrity": "sha512-3pA7xecItbgOs1A5H58dDvOUEboG5UfpTq3WzAdF54acBbUM+olDJAPkgj1GRJ4ZqE12DZ9/hNS2QZk166v92A==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.32.1.tgz",
+ "integrity": "sha512-PKvszb+9o/vVdUzCCjL0sKHukEQV39tD3fepXxYrHE3sTKrRdCydI7uldRLbjLmDA3TFDmh418XH19NOsDRH8g==",
"cpu": [
"arm"
],
@@ -913,9 +916,9 @@
]
},
"node_modules/@rollup/rollup-linux-arm-musleabihf": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.32.0.tgz",
- "integrity": "sha512-Y7XUZEVISGyge51QbYyYAEHwpGgmRrAxQXO3siyYo2kmaj72USSG8LtlQQgAtlGfxYiOwu+2BdbPjzEpcOpRmQ==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.32.1.tgz",
+ "integrity": "sha512-9WHEMV6Y89eL606ReYowXuGF1Yb2vwfKWKdD1A5h+OYnPZSJvxbEjxTRKPgi7tkP2DSnW0YLab1ooy+i/FQp/Q==",
"cpu": [
"arm"
],
@@ -927,9 +930,9 @@
]
},
"node_modules/@rollup/rollup-linux-arm64-gnu": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.32.0.tgz",
- "integrity": "sha512-r7/OTF5MqeBrZo5omPXcTnjvv1GsrdH8a8RerARvDFiDwFpDVDnJyByYM/nX+mvks8XXsgPUxkwe/ltaX2VH7w==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.32.1.tgz",
+ "integrity": "sha512-tZWc9iEt5fGJ1CL2LRPw8OttkCBDs+D8D3oEM8mH8S1ICZCtFJhD7DZ3XMGM8kpqHvhGUTvNUYVDnmkj4BDXnw==",
"cpu": [
"arm64"
],
@@ -941,9 +944,9 @@
]
},
"node_modules/@rollup/rollup-linux-arm64-musl": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.32.0.tgz",
- "integrity": "sha512-HJbifC9vex9NqnlodV2BHVFNuzKL5OnsV2dvTw6e1dpZKkNjPG6WUq+nhEYV6Hv2Bv++BXkwcyoGlXnPrjAKXw==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.32.1.tgz",
+ "integrity": "sha512-FTYc2YoTWUsBz5GTTgGkRYYJ5NGJIi/rCY4oK/I8aKowx1ToXeoVVbIE4LGAjsauvlhjfl0MYacxClLld1VrOw==",
"cpu": [
"arm64"
],
@@ -955,9 +958,9 @@
]
},
"node_modules/@rollup/rollup-linux-loongarch64-gnu": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loongarch64-gnu/-/rollup-linux-loongarch64-gnu-4.32.0.tgz",
- "integrity": "sha512-VAEzZTD63YglFlWwRj3taofmkV1V3xhebDXffon7msNz4b14xKsz7utO6F8F4cqt8K/ktTl9rm88yryvDpsfOw==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loongarch64-gnu/-/rollup-linux-loongarch64-gnu-4.32.1.tgz",
+ "integrity": "sha512-F51qLdOtpS6P1zJVRzYM0v6MrBNypyPEN1GfMiz0gPu9jN8ScGaEFIZQwteSsGKg799oR5EaP7+B2jHgL+d+Kw==",
"cpu": [
"loong64"
],
@@ -969,9 +972,9 @@
]
},
"node_modules/@rollup/rollup-linux-powerpc64le-gnu": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.32.0.tgz",
- "integrity": "sha512-Sts5DST1jXAc9YH/iik1C9QRsLcCoOScf3dfbY5i4kH9RJpKxiTBXqm7qU5O6zTXBTEZry69bGszr3SMgYmMcQ==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.32.1.tgz",
+ "integrity": "sha512-wO0WkfSppfX4YFm5KhdCCpnpGbtgQNj/tgvYzrVYFKDpven8w2N6Gg5nB6w+wAMO3AIfSTWeTjfVe+uZ23zAlg==",
"cpu": [
"ppc64"
],
@@ -983,9 +986,9 @@
]
},
"node_modules/@rollup/rollup-linux-riscv64-gnu": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.32.0.tgz",
- "integrity": "sha512-qhlXeV9AqxIyY9/R1h1hBD6eMvQCO34ZmdYvry/K+/MBs6d1nRFLm6BOiITLVI+nFAAB9kUB6sdJRKyVHXnqZw==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.32.1.tgz",
+ "integrity": "sha512-iWswS9cIXfJO1MFYtI/4jjlrGb/V58oMu4dYJIKnR5UIwbkzR0PJ09O0PDZT0oJ3LYWXBSWahNf/Mjo6i1E5/g==",
"cpu": [
"riscv64"
],
@@ -997,9 +1000,9 @@
]
},
"node_modules/@rollup/rollup-linux-s390x-gnu": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.32.0.tgz",
- "integrity": "sha512-8ZGN7ExnV0qjXa155Rsfi6H8M4iBBwNLBM9lcVS+4NcSzOFaNqmt7djlox8pN1lWrRPMRRQ8NeDlozIGx3Omsw==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.32.1.tgz",
+ "integrity": "sha512-RKt8NI9tebzmEthMnfVgG3i/XeECkMPS+ibVZjZ6mNekpbbUmkNWuIN2yHsb/mBPyZke4nlI4YqIdFPgKuoyQQ==",
"cpu": [
"s390x"
],
@@ -1011,9 +1014,9 @@
]
},
"node_modules/@rollup/rollup-linux-x64-gnu": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.32.0.tgz",
- "integrity": "sha512-VDzNHtLLI5s7xd/VubyS10mq6TxvZBp+4NRWoW+Hi3tgV05RtVm4qK99+dClwTN1McA6PHwob6DEJ6PlXbY83A==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.32.1.tgz",
+ "integrity": "sha512-WQFLZ9c42ECqEjwg/GHHsouij3pzLXkFdz0UxHa/0OM12LzvX7DzedlY0SIEly2v18YZLRhCRoHZDxbBSWoGYg==",
"cpu": [
"x64"
],
@@ -1025,9 +1028,9 @@
]
},
"node_modules/@rollup/rollup-linux-x64-musl": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.32.0.tgz",
- "integrity": "sha512-qcb9qYDlkxz9DxJo7SDhWxTWV1gFuwznjbTiov289pASxlfGbaOD54mgbs9+z94VwrXtKTu+2RqwlSTbiOqxGg==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.32.1.tgz",
+ "integrity": "sha512-BLoiyHDOWoS3uccNSADMza6V6vCNiphi94tQlVIL5de+r6r/CCQuNnerf+1g2mnk2b6edp5dk0nhdZ7aEjOBsA==",
"cpu": [
"x64"
],
@@ -1039,9 +1042,9 @@
]
},
"node_modules/@rollup/rollup-win32-arm64-msvc": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.32.0.tgz",
- "integrity": "sha512-pFDdotFDMXW2AXVbfdUEfidPAk/OtwE/Hd4eYMTNVVaCQ6Yl8et0meDaKNL63L44Haxv4UExpv9ydSf3aSayDg==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.32.1.tgz",
+ "integrity": "sha512-w2l3UnlgYTNNU+Z6wOR8YdaioqfEnwPjIsJ66KxKAf0p+AuL2FHeTX6qvM+p/Ue3XPBVNyVSfCrfZiQh7vZHLQ==",
"cpu": [
"arm64"
],
@@ -1053,9 +1056,9 @@
]
},
"node_modules/@rollup/rollup-win32-ia32-msvc": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.32.0.tgz",
- "integrity": "sha512-/TG7WfrCAjeRNDvI4+0AAMoHxea/USWhAzf9PVDFHbcqrQ7hMMKp4jZIy4VEjk72AAfN5k4TiSMRXRKf/0akSw==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.32.1.tgz",
+ "integrity": "sha512-Am9H+TGLomPGkBnaPWie4F3x+yQ2rr4Bk2jpwy+iV+Gel9jLAu/KqT8k3X4jxFPW6Zf8OMnehyutsd+eHoq1WQ==",
"cpu": [
"ia32"
],
@@ -1067,9 +1070,9 @@
]
},
"node_modules/@rollup/rollup-win32-x64-msvc": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.32.0.tgz",
- "integrity": "sha512-5hqO5S3PTEO2E5VjCePxv40gIgyS2KvO7E7/vvC/NbIW4SIRamkMr1hqj+5Y67fbBWv/bQLB6KelBQmXlyCjWA==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.32.1.tgz",
+ "integrity": "sha512-ar80GhdZb4DgmW3myIS9nRFYcpJRSME8iqWgzH2i44u+IdrzmiXVxeFnExQ5v4JYUSpg94bWjevMG8JHf1Da5Q==",
"cpu": [
"x64"
],
@@ -1088,9 +1091,9 @@
"license": "MIT"
},
"node_modules/@types/node": {
- "version": "22.10.10",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-22.10.10.tgz",
- "integrity": "sha512-X47y/mPNzxviAGY5TcYPtYL8JsY3kAq2n8fMmKoRCxq/c4v4pyGNCzM2R6+M5/umG4ZfHuT+sgqDYqWc9rJ6ww==",
+ "version": "22.12.0",
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-22.12.0.tgz",
+ "integrity": "sha512-Fll2FZ1riMjNmlmJOdAyY5pUbkftXslB5DgEzlIuNaiWhXd00FhWxVC/r4yV/4wBb9JfImTu+jiSvXTkJ7F/gA==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -1966,9 +1969,9 @@
"license": "MIT"
},
"node_modules/loupe": {
- "version": "3.1.2",
- "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.1.2.tgz",
- "integrity": "sha512-23I4pFZHmAemUnz8WZXbYRSKYj801VDaNv9ETuMh7IrMc7VuVVSo+Z9iLE3ni30+U48iDWfi30d3twAXBYmnCg==",
+ "version": "3.1.3",
+ "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.1.3.tgz",
+ "integrity": "sha512-kkIp7XSkP78ZxJEsSxW3712C6teJVoeHHwgo9zJ380de7IYyJ2ISlxojcH2pC5OFLewESmnRi/+XCDIEEVyoug==",
"dev": true,
"license": "MIT"
},
@@ -2306,9 +2309,9 @@
}
},
"node_modules/rollup": {
- "version": "4.32.0",
- "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.32.0.tgz",
- "integrity": "sha512-JmrhfQR31Q4AuNBjjAX4s+a/Pu/Q8Q9iwjWBsjRH1q52SPFE2NqRMK6fUZKKnvKO6id+h7JIRf0oYsph53eATg==",
+ "version": "4.32.1",
+ "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.32.1.tgz",
+ "integrity": "sha512-z+aeEsOeEa3mEbS1Tjl6sAZ8NE3+AalQz1RJGj81M+fizusbdDMoEJwdJNHfaB40Scr4qNu+welOfes7maKonA==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -2322,25 +2325,25 @@
"npm": ">=8.0.0"
},
"optionalDependencies": {
- "@rollup/rollup-android-arm-eabi": "4.32.0",
- "@rollup/rollup-android-arm64": "4.32.0",
- "@rollup/rollup-darwin-arm64": "4.32.0",
- "@rollup/rollup-darwin-x64": "4.32.0",
- "@rollup/rollup-freebsd-arm64": "4.32.0",
- "@rollup/rollup-freebsd-x64": "4.32.0",
- "@rollup/rollup-linux-arm-gnueabihf": "4.32.0",
- "@rollup/rollup-linux-arm-musleabihf": "4.32.0",
- "@rollup/rollup-linux-arm64-gnu": "4.32.0",
- "@rollup/rollup-linux-arm64-musl": "4.32.0",
- "@rollup/rollup-linux-loongarch64-gnu": "4.32.0",
- "@rollup/rollup-linux-powerpc64le-gnu": "4.32.0",
- "@rollup/rollup-linux-riscv64-gnu": "4.32.0",
- "@rollup/rollup-linux-s390x-gnu": "4.32.0",
- "@rollup/rollup-linux-x64-gnu": "4.32.0",
- "@rollup/rollup-linux-x64-musl": "4.32.0",
- "@rollup/rollup-win32-arm64-msvc": "4.32.0",
- "@rollup/rollup-win32-ia32-msvc": "4.32.0",
- "@rollup/rollup-win32-x64-msvc": "4.32.0",
+ "@rollup/rollup-android-arm-eabi": "4.32.1",
+ "@rollup/rollup-android-arm64": "4.32.1",
+ "@rollup/rollup-darwin-arm64": "4.32.1",
+ "@rollup/rollup-darwin-x64": "4.32.1",
+ "@rollup/rollup-freebsd-arm64": "4.32.1",
+ "@rollup/rollup-freebsd-x64": "4.32.1",
+ "@rollup/rollup-linux-arm-gnueabihf": "4.32.1",
+ "@rollup/rollup-linux-arm-musleabihf": "4.32.1",
+ "@rollup/rollup-linux-arm64-gnu": "4.32.1",
+ "@rollup/rollup-linux-arm64-musl": "4.32.1",
+ "@rollup/rollup-linux-loongarch64-gnu": "4.32.1",
+ "@rollup/rollup-linux-powerpc64le-gnu": "4.32.1",
+ "@rollup/rollup-linux-riscv64-gnu": "4.32.1",
+ "@rollup/rollup-linux-s390x-gnu": "4.32.1",
+ "@rollup/rollup-linux-x64-gnu": "4.32.1",
+ "@rollup/rollup-linux-x64-musl": "4.32.1",
+ "@rollup/rollup-win32-arm64-msvc": "4.32.1",
+ "@rollup/rollup-win32-ia32-msvc": "4.32.1",
+ "@rollup/rollup-win32-x64-msvc": "4.32.1",
"fsevents": "~2.3.2"
}
},
diff --git a/src/flatmatter.test.ts b/src/flatmatter.test.ts
index bbe26d9..9f00ef9 100644
--- a/src/flatmatter.test.ts
+++ b/src/flatmatter.test.ts
@@ -1,22 +1,33 @@
import FlatMatter from "./flatmatter.ts";
-import {assert} from "vitest";
+import { assert } from "vitest";
import ToObject from "./serializers/to_object.ts";
-test('Line has no value separator', () => {
- assert.throws(() => new FlatMatter('test'), "Line on index 0 doesn't have a value separator.");
-})
+test("Line has no value separator", () => {
+ assert.throws(
+ () => new FlatMatter("test"),
+ "Line on index 0 doesn't have a value separator."
+ );
+});
-test('Line can only have one value separator', () => {
- assert.throws(() => new FlatMatter('test: this: that'), 'Line on index 0 has multiple value separators.')
-})
+test("Line can only have one value separator", () => {
+ assert.throws(
+ () => new FlatMatter("test: this: that"),
+ "Line on index 0 has multiple value separators."
+ );
+});
-test('String values can have colon characters', () => {
- assert.doesNotThrow(() => new FlatMatter('test: "this : that"'), Error)
-})
+test("String values can have colon characters", () => {
+ assert.doesNotThrow(() => new FlatMatter('test: "this : that"'), Error);
+});
-test('FrontMatter creates a new content entry', () => {
- const fm = new FlatMatter(`---\nthis: true\n---\n\nMarkdown goes here.\n\nAnd here.`);
- const result = fm.serialize(new ToObject());
+test("FrontMatter creates a new content entry", () => {
+ const fm = new FlatMatter(
+ `---\nthis: true\n---\n\nMarkdown goes here.\n\nAnd here.`
+ );
+ const result = fm.serialize(new ToObject());
- assert.deepEqual(result, {"this": true, "content": "Markdown goes here.\n\nAnd here."});
-}) \ No newline at end of file
+ assert.deepEqual(result, {
+ this: true,
+ content: "Markdown goes here.\n\nAnd here.",
+ });
+});
diff --git a/src/flatmatter.ts b/src/flatmatter.ts
index 8ffee6f..e81df1f 100644
--- a/src/flatmatter.ts
+++ b/src/flatmatter.ts
@@ -1,401 +1,355 @@
import { EOL } from "node:os";
-import {trimChar} from "./utils.ts";
+import { trimChar } from "./utils.ts";
export type ParsedValue = {
- value: unknown;
- computeActions: ComputeAction[];
+ value: unknown;
+ computeActions: ComputeAction[];
};
export type ComputeAction = {
- identifier: string;
- args: Array<unknown>;
+ identifier: string;
+ args: Array<unknown>;
};
-export interface Serializer {
- serialize(parsedConfig: Record<string, unknown>): unknown;
+export interface Serializer<T> {
+ serialize(config: Record<string, unknown>): T;
}
export interface FlatMatterFn {
- name: string;
+ name: string;
- compute(...args: unknown[]): unknown;
+ compute(...args: unknown[]): unknown;
}
export default class FlatMatter {
- private content: string;
- private parsedConfig: Record<string, unknown> = {};
- private functions: FlatMatterFn[];
-
- constructor(content: string, functions: FlatMatterFn[] = []) {
- this.content = content;
- this.functions = functions;
- this.parse();
+ private content: string;
+ private parsedConfig: Record<string, unknown> = {};
+ private functions: FlatMatterFn[];
+
+ constructor(content: string, functions: FlatMatterFn[] = []) {
+ this.content = content;
+ this.functions = functions;
+ this.parse();
+ }
+
+ private parse(): void {
+ const lines = this.content.split(EOL);
+ let frontMatterBreakCount = 0;
+
+ for (let i = 0; i < lines.length; i++) {
+ if (lines[i].trim() === "---" && frontMatterBreakCount < 2) {
+ frontMatterBreakCount++;
+ continue;
+ }
+
+ // FlatMatter ends, Markdown begins
+ if (frontMatterBreakCount < 2) {
+ this.parseLine(i, lines[i]);
+ continue;
+ }
+
+ this.parsedConfig.content = lines.slice(i).join(EOL).trim();
+ break;
}
-
- private parse(): void {
- const lines = this.content.split(/\r?\n/);
- let frontMatterBreakCount = 0;
-
- for (let i = 0; i < lines.length; i++) {
- if (lines[i].trim() === "---" && frontMatterBreakCount < 2) {
- frontMatterBreakCount++;
- continue;
- }
-
-
- // FlatMatter ends, Markdown begins
- if (frontMatterBreakCount < 2) {
- this.parseLine(i, lines[i]);
- continue;
- }
-
- this.parsedConfig.content = lines.slice(i).join(EOL).trim();
- break;
- }
+ }
+
+ /**
+ * Parses a given line of FlatMatter.
+ */
+ private parseLine(idx: number, line: string): void {
+ this.validateLineConformance(idx, line);
+
+ const keys = line.split(":")[0].trim().split(".");
+ const value = line.split(":").slice(1).join(":").trim();
+ const parsedValue = this.parseValue(value);
+
+ if (!parsedValue) return;
+
+ const config = keys.reduceRight((acc, key) => {
+ return { [key]: acc };
+ }, this.computeValue(parsedValue)) as Record<string, unknown>;
+
+ this.parsedConfig = { ...this.parsedConfig, ...config };
+ }
+
+ /**
+ * For better developer experience, this validates each line
+ * against some common mistakes you can make, and throws an Error
+ * if you did.
+ */
+ private validateLineConformance(idx: number, line: string): void {
+ const validators = [
+ this.validateLineHasKeyVal,
+ this.validateLineHasOnlyOneColonChar,
+ ];
+
+ for (const validator of validators) {
+ validator(idx, line);
+ }
+ }
+
+ /**
+ * Validates that the given line has a value separator.
+ */
+ private validateLineHasKeyVal(idx: number, line: string): void {
+ if (!line.includes(":")) {
+ throw new Error(`Line on index ${idx} doesn't have a value separator.`);
+ }
+ }
+
+ /**
+ * Validates that the given line has only one value separator.
+ */
+ private validateLineHasOnlyOneColonChar(idx: number, line: string): void {
+ let separatorCount = 0;
+ let parts = line.split(":").slice(1);
+
+ for (let i = 0; i < parts.length; i++) {
+ const partsUntilCurrent = parts.slice(0, i).join(":");
+ const quoteCount = partsUntilCurrent.split('"').length - 1;
+
+ if (quoteCount % 2 === 0) {
+ separatorCount++;
+ }
}
- /**
- * Parses a given line of FlatMatter.
- *
- * @param idx
- * @param {string} line
- */
- private parseLine(idx: number, line: string): void {
- this.validateLineConformance(idx, line);
+ if (separatorCount > 1) {
+ throw new Error(`Line on index ${idx} has multiple value separators.`);
+ }
+ }
+
+ /**
+ * Detects if the value is a simple value. A simple value is any
+ * of the following: `"a string"`, boolean `true` or `false`, or
+ * anything numeric like `12345` or `123.45`.
+ */
+ private isSimpleValue(value: string): boolean {
+ const isString = value.startsWith('"') && value.endsWith('"');
+ const isBoolean = value === "true" || value === "false";
+ const isNumber = !isNaN(parseFloat(value));
+
+ return isString || isBoolean || isNumber;
+ }
+
+ /**
+ * Detects if the value is a function value. A function value is any
+ * of the following:
+ *
+ * - A function call with arguments: `(function-name *args)`
+ * - A function call by reference: `function-name`
+ */
+ private isFunctionValue(value: string): boolean {
+ const isFnCall = value.startsWith("(") && value.endsWith(")");
+ const isFnReference = !!value.match(/^([a-zA-Z0-9_-]+)$/);
+
+ return isFnCall || isFnReference;
+ }
+
+ /**
+ * Detects if the value is a piped value. A piped value is a mix of
+ * simple and function value parts, piped together with the forward
+ * slash `/` character. For example:
+ *
+ * ```yaml
+ * posts: (get-content "posts") / (limit 10) / only-published
+ * ```
+ *
+ * or:
+ *
+ * ```yaml
+ * posts: "posts" / get-content / (limit 10) / only-published
+ * ```
+ *
+ * The result of the previous pipe gets passed to the next as a first
+ * argument.
+ */
+ private isPipedValue(value: string): boolean {
+ return this.composePipedValueParts(value).every((part) => {
+ return this.isSimpleValue(part) || this.isFunctionValue(part);
+ });
+ }
+
+ /**
+ * Parses a value to a `ParsedValue` object, or `null`
+ * in case it could not for whatever reason.
+ */
+ private parseValue(value: string): ParsedValue | null {
+ if (this.isSimpleValue(value)) {
+ return {
+ value: this.parseSimpleValue(value),
+ computeActions: [],
+ };
+ }
- const keys = line.split(":")[0].trim().split(".");
- const value = line.split(":").slice(1).join(":").trim();
- const parsedValue = this.parseValue(value);
+ if (this.isFunctionValue(value)) {
+ return {
+ value: null,
+ computeActions: [this.parseFunctionValue(value)],
+ };
+ }
- if (!parsedValue) return;
+ if (this.isPipedValue(value)) {
+ return this.parsePipedValue(value);
+ }
- const config = keys.reduceRight((acc, key) => {
- return {[key]: acc};
- }, this.computeValue(parsedValue)) as Record<string, unknown>;
+ return null;
+ }
- this.parsedConfig = {...this.parsedConfig, ...config};
+ /**
+ * Parses the value part of a line into a simple value, like for example
+ * a `string`, `number` or `boolean`.
+ */
+ private parseSimpleValue(value: string): string | number | boolean {
+ if (value === "true" || value === "false") {
+ return value === "true";
}
- /**
- * For better developer experience, this validates each line
- * against some common mistakes you can make, and throws an Error
- * if you did.
- *
- * @param {number} idx
- * @param {string} line
- */
- private validateLineConformance(idx: number, line: string): void {
- const validators = [
- this.validateLineHasKeyVal,
- this.validateLineHasOnlyOneColonChar,
- ];
-
- for(const validator of validators) {
- validator(idx, line);
- }
+ if (!Number.isNaN(parseInt(value)) && value.indexOf(".") === -1) {
+ return parseInt(value);
}
- /**
- * Validates that the given line has a value separator.
- *
- * @param {number} idx
- * @param {string} line
- */
- private validateLineHasKeyVal(idx: number, line: string): void {
- if (!line.includes(":")) {
- throw new Error(`Line on index ${idx} doesn't have a value separator.`);
- }
+ if (!Number.isNaN(parseFloat(value)) && value.indexOf(".") !== -1) {
+ return parseFloat(value);
}
- /**
- * Validates that the given line has only one value separator.
- *
- * @param {number} idx
- * @param {string} line
- */
- private validateLineHasOnlyOneColonChar(idx: number, line: string): void {
- let separatorCount = 0;
- let parts = line.split(":").slice(1);
-
- for(let i = 0; i < parts.length; i++) {
- const partsUntilCurrent = parts.slice(0, i).join(":");
- const quoteCount = partsUntilCurrent.split('"').length - 1;
-
- if (quoteCount % 2 === 0) {
- separatorCount++;
- }
- }
-
- if (separatorCount > 1) {
- throw new Error(`Line on index ${idx} has multiple value separators.`);
- }
+ return trimChar(value, '"');
+ }
+
+ /**
+ * Parses the value part of a line into a Compute Action, which is
+ * later executed to run the function described in FlatMatter.
+ */
+ private parseFunctionValue(value: string): ComputeAction {
+ const isFn = value.startsWith("(") && value.endsWith(")");
+
+ if (!isFn) {
+ return {
+ identifier: value,
+ args: [],
+ };
}
- /**
- * Detects if the value is a simple value. A simple value is any
- * of the following: `"a string"`, boolean `true` or `false`, or
- * anything numeric like `12345` or `123.45`.
- *
- * @param {string} value
- * @returns {boolean}
- */
- private isSimpleValue(value: string): boolean {
- const isString = value.startsWith('"') && value.endsWith('"');
- const isBoolean = value === "true" || value === "false";
- const isNumber = !isNaN(parseFloat(value));
-
- return isString || isBoolean || isNumber;
+ const fnName = trimChar(value, ["(", ")"]).split(" ")[0].trim();
+ const fnArgs = this.parseFunctionValueArgs(value);
+
+ return {
+ identifier: fnName,
+ args: fnArgs,
+ };
+ }
+
+ /**
+ * Parses the value part of a line into a ParsedValue, which is
+ * composed out of piped parts separated by the forward slash `/` character.
+ *
+ * The ParsedValue will include the default value, if any, and a list of compute
+ * actions which will later be executed.
+ */
+ private parsePipedValue(value: string): ParsedValue {
+ const parts = this.composePipedValueParts(value);
+
+ if (this.isSimpleValue(parts[0])) {
+ return {
+ value: this.parseSimpleValue(parts[0]),
+ computeActions: parts.slice(1).map((p) => this.parseFunctionValue(p)),
+ };
}
- /**
- * Detects if the value is a function value. A function value is any
- * of the following:
- *
- * - A function call with arguments: `(function-name *args)`
- * - A function call by reference: `function-name`
- *
- * @param {string} value
- * @returns {boolean}
- */
- private isFunctionValue(value: string): boolean {
- const isFnCall = value.startsWith("(") && value.endsWith(")");
- const isFnReference = !!value.match(/^([a-zA-Z0-9_-]+)$/);
-
- return isFnCall || isFnReference;
+ return {
+ value: null,
+ computeActions: parts.map((p) => this.parseFunctionValue(p)),
+ };
+ }
+
+ /**
+ * Takes the entire value part of a line and, assuming it is a function value,
+ * parses it into a list of arguments to be passed down to the function.
+ */
+ private parseFunctionValueArgs(value: string): unknown[] {
+ const parts = value
+ .substring(1, value.length - 1)
+ .split(" ")
+ .slice(1);
+
+ if (!parts.length) {
+ return [];
}
- /**
- * Detects if the value is a piped value. A piped value is a mix of
- * simple and function value parts, piped together with the forward
- * slash `/` character. For example:
- *
- * ```yaml
- * posts: (get-content "posts") / (limit 10) / only-published
- * ```
- *
- * or:
- *
- * ```yaml
- * posts: "posts" / get-content / (limit 10) / only-published
- * ```
- *
- * The result of the previous pipe gets passed to the next as a first
- * argument.
- *
- * @param {string} value
- * @returns {boolean}
- */
- private isPipedValue(value: string): boolean {
- return this.composePipedValueParts(value).every(part => {
- return this.isSimpleValue(part) || this.isFunctionValue(part)
- });
- }
+ const normalizedParts = [parts[0]];
- /**
- * Parses a value to a `ParsedValue` object, or `null`
- * in case it could not for whatever reason.
- *
- * @param {string} value
- * @returns {ParsedValue | null}
- */
- private parseValue(value: string): ParsedValue | null {
- if (this.isSimpleValue(value)) {
- return {
- value: this.parseSimpleValue(value),
- computeActions: [],
- };
- }
-
- if (this.isFunctionValue(value)) {
- return {
- value: null,
- computeActions: [this.parseFunctionValue(value)],
- };
- }
-
- if (this.isPipedValue(value)) {
- return this.parsePipedValue(value);
- }
-
- return null;
- }
+ for (let i = 1; i < parts.length; i++) {
+ const untilCurrent = normalizedParts.join(" ");
+ const quoteCount = untilCurrent.split('"').length - 1;
- /**
- * Parses the value part of a line into a simple value, like for example
- * a `string`, `number` or `boolean`.
- *
- * @param {string} value
- * @returns {string | number | boolean}
- */
- private parseSimpleValue(value: string): string | number | boolean {
- if (value === "true" || value === "false") {
- return value === "true";
- }
-
- if (!Number.isNaN(parseInt(value)) && value.indexOf('.') === -1) {
- return parseInt(value);
- }
-
- if (!Number.isNaN(parseFloat(value)) && value.indexOf('.') !== -1) {
- return parseFloat(value);
- }
-
- return trimChar(value, '"');
- }
+ if (quoteCount % 2 === 0) {
+ normalizedParts.push(parts[i]);
+ continue;
+ }
- /**
- * Parses the value part of a line into a Compute Action, which is
- * later executed to run the function described in FlatMatter.
- *
- * @param {string} value
- * @returns {ComputeAction}
- */
- private parseFunctionValue(value: string): ComputeAction {
- const isFn = value.startsWith("(") && value.endsWith(")");
-
- if (!isFn) {
- return {
- identifier: value,
- args: [],
- };
- }
-
- const fnName = trimChar(value, ["(", ")"]).split(" ")[0].trim();
- const fnArgs = this.parseFunctionValueArgs(value);
-
- return {
- identifier: fnName,
- args: fnArgs,
- };
- }
+ const lastIndex = normalizedParts.length - 1;
+ const lastPart = normalizedParts[lastIndex];
- /**
- * Parses the value part of a line into a ParsedValue, which is
- * composed out of piped parts separated by the forward slash `/` character.
- *
- * The ParsedValue will include the default value, if any, and a list of compute
- * actions which will later be executed.
- *
- * @param {string} value
- * @returns {ParsedValue}
- */
- private parsePipedValue(value: string): ParsedValue {
- const parts = this.composePipedValueParts(value);
-
- if (this.isSimpleValue(parts[0])) {
- return {
- value: this.parseSimpleValue(parts[0]),
- computeActions: parts.slice(1).map((p) => this.parseFunctionValue(p)),
- };
- }
-
- return {
- value: null,
- computeActions: parts.map((p) => this.parseFunctionValue(p)),
- };
+ normalizedParts[lastIndex] = `${lastPart} ${parts[i]}`;
}
- /**
- * Takes the entire value part of a line and, assuming it is a function value,
- * parses it into a list of arguments to be passed down to the function.
- *
- * @param {string} value
- * @returns {unknown[]}
- */
- private parseFunctionValueArgs(value: string): unknown[] {
- const parts = value
- .substring(1, value.length - 1)
- .split(" ")
- .slice(1);
-
- if (!parts.length) {
- return [];
- }
-
- const normalizedParts = [parts[0]];
-
- for (let i = 1; i < parts.length; i++) {
- const untilCurrent = normalizedParts.join(" ");
- const quoteCount = untilCurrent.split('"').length - 1;
-
- if (quoteCount % 2 === 0) {
- normalizedParts.push(parts[i]);
- continue;
- }
-
- const lastIndex = normalizedParts.length - 1;
- const lastPart = normalizedParts[lastIndex];
-
- normalizedParts[lastIndex] = `${lastPart} ${parts[i]}`;
- }
-
- return normalizedParts.map((part) => this.parseSimpleValue(part));
- }
+ return normalizedParts.map((part) => this.parseSimpleValue(part));
+ }
+
+ /**
+ * Takes an entire value of a line and composes it into a list
+ * of piped parts.
+ */
+ private composePipedValueParts(value: string): string[] {
+ const parts = value.split(" / ");
+ const normalizedParts = [parts[0]];
- /**
- * Takes an entire value of a line and composes it into a list
- * of piped parts.
- *
- * @param {string} value
- * @returns {string[]}
- */
- private composePipedValueParts(value: string): string[] {
- const parts = value.split(" / ");
- const normalizedParts = [parts[0]];
-
- for (let i = 1; i < parts.length; i++) {
- const untilCurrent = normalizedParts.join(" / ");
- const quoteCount = untilCurrent.split('"').length - 1;
-
- if (quoteCount % 2 === 0) {
- normalizedParts.push(parts[i]);
- continue;
- }
-
- const lastIndex = normalizedParts.length - 1;
- const lastPart = normalizedParts[lastIndex];
-
- normalizedParts[lastIndex] = `${lastPart} / ${parts[i]}`;
- }
-
- return normalizedParts;
+ for (let i = 1; i < parts.length; i++) {
+ const untilCurrent = normalizedParts.join(" / ");
+ const quoteCount = untilCurrent.split('"').length - 1;
+
+ if (quoteCount % 2 === 0) {
+ normalizedParts.push(parts[i]);
+ continue;
+ }
+
+ const lastIndex = normalizedParts.length - 1;
+ const lastPart = normalizedParts[lastIndex];
+
+ normalizedParts[lastIndex] = `${lastPart} / ${parts[i]}`;
}
- /**
- * Takes ParsedValue and, optionally an initial value, and runs
- * compute actions over it to return the final computed value.
- *
- * @param {ParsedValue} parsedValue
- * @returns {unknown}
- */
- private computeValue(parsedValue: ParsedValue): unknown {
- let value = parsedValue.value;
+ return normalizedParts;
+ }
- for (const ca of parsedValue.computeActions) {
- const fnInstance = this.functions.find((f) => f.name === ca.identifier);
+ /**
+ * Takes ParsedValue and, optionally an initial value, and runs
+ * compute actions over it to return the final computed value.
+ */
+ private computeValue(parsedValue: ParsedValue): unknown {
+ let value = parsedValue.value;
- if (!fnInstance) {
- continue;
- }
+ for (const ca of parsedValue.computeActions) {
+ const fnInstance = this.functions.find((f) => f.name === ca.identifier);
- if (value !== null) {
- ca.args = [value, ...ca.args];
- }
+ if (!fnInstance) {
+ continue;
+ }
- value = fnInstance.compute(...ca.args);
- }
+ if (value !== null) {
+ ca.args = [value, ...ca.args];
+ }
- return value;
+ value = fnInstance.compute(...ca.args);
}
- /**
- * Takes a Serializer and uses it to transform internal data
- * object to a desired output.
- *
- * @param {Serializer} serializer
- * @returns {unknown}
- */
- public serialize(serializer: Serializer): unknown {
- return serializer.serialize(this.parsedConfig);
- }
+ return value;
+ }
+
+ /**
+ * Takes a Serializer and uses it to transform internal data
+ * object to a desired output.
+ */
+ public serialize<T>(serializer: Serializer<T>): T {
+ return serializer.serialize(this.parsedConfig);
+ }
}
diff --git a/src/serializers/to_json.test.ts b/src/serializers/to_json.test.ts
index 22f7ba8..4184ab1 100644
--- a/src/serializers/to_json.test.ts
+++ b/src/serializers/to_json.test.ts
@@ -2,11 +2,11 @@ import FlatMatter from "../flatmatter.ts";
import ToJson from "./to_json.ts";
test("Single-level configuration", () => {
- const fm = new FlatMatter(
- 'a: true\nb: false\nc: 1\nd: 12.5\nf: "some string"'
- );
+ const fm = new FlatMatter(
+ 'a: true\nb: false\nc: 1\nd: 12.5\nf: "some string"'
+ );
- const equal = "{\"a\":true,\"b\":false,\"c\":1,\"d\":12.5,\"f\":\"some string\"}";
-
- expect(fm.serialize(new ToJson())).toStrictEqual(equal);
+ const equal = '{"a":true,"b":false,"c":1,"d":12.5,"f":"some string"}';
+
+ expect(fm.serialize(new ToJson())).toStrictEqual(equal);
});
diff --git a/src/serializers/to_json.ts b/src/serializers/to_json.ts
index 76148db..30cf417 100644
--- a/src/serializers/to_json.ts
+++ b/src/serializers/to_json.ts
@@ -1,7 +1,7 @@
import type { Serializer } from "../flatmatter.ts";
-export default class ToJson implements Serializer {
- serialize(parsedConfig: Record<string, unknown>): string {
- return JSON.stringify(parsedConfig);
+export default class ToJson implements Serializer<string> {
+ serialize(config: Record<string, unknown>): string {
+ return JSON.stringify(config);
}
}
diff --git a/src/serializers/to_object.test.ts b/src/serializers/to_object.test.ts
index 7059458..28cb78e 100644
--- a/src/serializers/to_object.test.ts
+++ b/src/serializers/to_object.test.ts
@@ -1,4 +1,4 @@
-import FlatMatter, {type FlatMatterFn} from "../flatmatter.ts";
+import FlatMatter, { type FlatMatterFn } from "../flatmatter.ts";
import ToObject from "./to_object.ts";
test("Single-level configuration", () => {
@@ -48,7 +48,7 @@ test("Simple function usage", () => {
}
}
- const fm = new FlatMatter('a: (to-upper "value")', [new ToUpper]);
+ const fm = new FlatMatter('a: (to-upper "value")', [new ToUpper()]);
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
@@ -65,7 +65,7 @@ test("Piped function by reference usage", () => {
}
}
- const fm = new FlatMatter('a: "value" / to-upper', [new ToUpper]);
+ const fm = new FlatMatter('a: "value" / to-upper', [new ToUpper()]);
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
@@ -82,7 +82,7 @@ test("Piped function by call usage", () => {
}
}
- const fm = new FlatMatter('a: "value" / (to-upper 123)', [new ToUpper]);
+ const fm = new FlatMatter('a: "value" / (to-upper 123)', [new ToUpper()]);
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
@@ -95,16 +95,16 @@ test("Invalid value in pipe", () => {
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({});
-})
+});
test("Invalid value in pipe, 2", () => {
const fm = new FlatMatter('a: "value" / asd');
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
- a: "value"
+ a: "value",
});
-})
+});
test("Only piped functions", () => {
class FirstFn implements FlatMatterFn {
@@ -123,11 +123,14 @@ test("Only piped functions", () => {
}
}
- const fm = new FlatMatter('a: (first-fn "value / here") / second-fn', [new FirstFn, new SecondFn]);
+ const fm = new FlatMatter('a: (first-fn "value / here") / second-fn', [
+ new FirstFn(),
+ new SecondFn(),
+ ]);
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
- a: "VALUE / HERE-passed-by-second"
+ a: "VALUE / HERE-passed-by-second",
});
});
@@ -140,13 +143,13 @@ test("Function call without any args", () => {
}
}
- const fm = new FlatMatter('a: "value" / (to-upper)', [new ToUpper]);
+ const fm = new FlatMatter('a: "value" / (to-upper)', [new ToUpper()]);
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
a: "VALUE",
});
-})
+});
test("Function call using multiple strings with spaces as arg", () => {
class ToUpper implements FlatMatterFn {
@@ -157,10 +160,12 @@ test("Function call using multiple strings with spaces as arg", () => {
}
}
- const fm = new FlatMatter('a: (to-upper "value goes here" "and here")', [new ToUpper]);
+ const fm = new FlatMatter('a: (to-upper "value goes here" "and here")', [
+ new ToUpper(),
+ ]);
const config = fm.serialize(new ToObject());
expect(config).toStrictEqual({
a: "VALUE GOES HERE",
});
-}) \ No newline at end of file
+});
diff --git a/src/serializers/to_object.ts b/src/serializers/to_object.ts
index a7161cc..79b2886 100644
--- a/src/serializers/to_object.ts
+++ b/src/serializers/to_object.ts
@@ -1,7 +1,7 @@
-import type {Serializer} from "../flatmatter.ts";
+import type { Serializer } from "../flatmatter.ts";
-export default class ToObject implements Serializer {
- serialize(parsedConfig: Record<string, unknown>): Record<string, unknown> {
- return parsedConfig;
+export default class ToObject implements Serializer<Record<string, unknown>> {
+ serialize(config: Record<string, unknown>): Record<string, unknown> {
+ return config;
}
}
diff --git a/src/utils.ts b/src/utils.ts
index 56995fc..b334380 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -1,9 +1,5 @@
/**
* Trims char(s) from both sides of the given input string.
-
- * @param {string} input
- * @param {string | string[]} char
- * @returns {string}
*/
export function trimChar(input: string, char: string | string[]): string {
if (typeof char === "string") {
diff --git a/tsconfig.json b/tsconfig.json
index a6ed0e4..21eeee6 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,15 +1,11 @@
{
- "include": [
- "src"
- ],
- "exclude": [
- "**/*.test.ts"
- ],
+ "include": ["src"],
"compilerOptions": {
"module": "NodeNext",
"target": "esnext",
"moduleResolution": "nodenext",
"allowImportingTsExtensions": true,
+ "noEmit": true,
"declaration": true,
"skipLibCheck": true,
"rootDir": "src",
@@ -17,11 +13,7 @@
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
- "lib": [
- "esnext",
- ],
- "types": [
- "vitest/globals"
- ]
+ "lib": ["esnext"],
+ "types": ["vitest/globals"]
}
}
diff --git a/vite.config.ts b/vite.config.ts
deleted file mode 100644
index 85abf5d..0000000
--- a/vite.config.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-/// <reference types="vitest" />
-import {defineConfig} from "vite";
-
-export default defineConfig({
- test: {
- globals: true,
- environment: "node",
- },
-});