summaryrefslogtreecommitdiff
path: root/src/serializers
diff options
context:
space:
mode:
Diffstat (limited to 'src/serializers')
-rw-r--r--src/serializers/to_json.test.ts6
-rw-r--r--src/serializers/to_json.ts12
-rw-r--r--src/serializers/to_object.test.ts171
-rw-r--r--src/serializers/to_object.ts7
4 files changed, 10 insertions, 186 deletions
diff --git a/src/serializers/to_json.test.ts b/src/serializers/to_json.test.ts
index 4184ab1..78f704a 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 config = FlatMatter.config(
+ '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);
+ expect(FlatMatter.serialize(config, ToJson)).toStrictEqual(equal);
});
diff --git a/src/serializers/to_json.ts b/src/serializers/to_json.ts
index 30cf417..c61a98f 100644
--- a/src/serializers/to_json.ts
+++ b/src/serializers/to_json.ts
@@ -1,7 +1,9 @@
import type { Serializer } from "../flatmatter.ts";
-export default class ToJson implements Serializer<string> {
- serialize(config: Record<string, unknown>): string {
- return JSON.stringify(config);
- }
-}
+const ToJson: Serializer<string> = (
+ config: Record<string, unknown>,
+): string => {
+ return JSON.stringify(config);
+};
+
+export default ToJson;
diff --git a/src/serializers/to_object.test.ts b/src/serializers/to_object.test.ts
deleted file mode 100644
index 28cb78e..0000000
--- a/src/serializers/to_object.test.ts
+++ /dev/null
@@ -1,171 +0,0 @@
-import FlatMatter, { type FlatMatterFn } from "../flatmatter.ts";
-import ToObject from "./to_object.ts";
-
-test("Single-level configuration", () => {
- const fm = new FlatMatter(
- 'a: true\nb: false\nc: 1\nd: 12.5\nf: "some string"'
- );
-
- expect(fm.serialize(new ToObject())).toStrictEqual({
- a: true,
- b: false,
- c: 1,
- d: 12.5,
- f: "some string",
- });
-});
-
-test("Two-level configuration", () => {
- const fm = new FlatMatter(
- 'a.a: true\nb.b: false\nc.c: 1\nd.d: 12.5\nf.f: "some string"'
- );
-
- expect(fm.serialize(new ToObject())).toStrictEqual({
- a: {
- a: true,
- },
- b: {
- b: false,
- },
- c: {
- c: 1,
- },
- d: {
- d: 12.5,
- },
- f: {
- f: "some string",
- },
- });
-});
-
-test("Simple function usage", () => {
- class ToUpper implements FlatMatterFn {
- name = "to-upper";
-
- compute(input: string): unknown {
- return input.toUpperCase();
- }
- }
-
- const fm = new FlatMatter('a: (to-upper "value")', [new ToUpper()]);
- const config = fm.serialize(new ToObject());
-
- expect(config).toStrictEqual({
- a: "VALUE",
- });
-});
-
-test("Piped function by reference usage", () => {
- class ToUpper implements FlatMatterFn {
- name = "to-upper";
-
- compute(input: string): unknown {
- return input.toUpperCase();
- }
- }
-
- const fm = new FlatMatter('a: "value" / to-upper', [new ToUpper()]);
- const config = fm.serialize(new ToObject());
-
- expect(config).toStrictEqual({
- a: "VALUE",
- });
-});
-
-test("Piped function by call usage", () => {
- class ToUpper implements FlatMatterFn {
- name = "to-upper";
-
- compute(input: string, additional: number): unknown {
- return `${input.toUpperCase()}-${additional}`;
- }
- }
-
- const fm = new FlatMatter('a: "value" / (to-upper 123)', [new ToUpper()]);
- const config = fm.serialize(new ToObject());
-
- expect(config).toStrictEqual({
- a: "VALUE-123",
- });
-});
-
-test("Invalid value in pipe", () => {
- const fm = new FlatMatter('a: "value" / / asd');
- 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",
- });
-});
-
-test("Only piped functions", () => {
- class FirstFn implements FlatMatterFn {
- name = "first-fn";
-
- compute(input: string): unknown {
- return input.toUpperCase();
- }
- }
-
- class SecondFn implements FlatMatterFn {
- name = "second-fn";
-
- compute(input: string): unknown {
- return `${input}-passed-by-second`;
- }
- }
-
- 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",
- });
-});
-
-test("Function call without any args", () => {
- class ToUpper implements FlatMatterFn {
- name = "to-upper";
-
- compute(input: string): unknown {
- return input.toUpperCase();
- }
- }
-
- 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 {
- name = "to-upper";
-
- compute(input: string): unknown {
- return input.toUpperCase();
- }
- }
-
- 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",
- });
-});
diff --git a/src/serializers/to_object.ts b/src/serializers/to_object.ts
deleted file mode 100644
index 79b2886..0000000
--- a/src/serializers/to_object.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import type { Serializer } from "../flatmatter.ts";
-
-export default class ToObject implements Serializer<Record<string, unknown>> {
- serialize(config: Record<string, unknown>): Record<string, unknown> {
- return config;
- }
-}