summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/flatmatter.ts18
-rw-r--r--src/serializers/to_object.test.ts83
2 files changed, 86 insertions, 15 deletions
diff --git a/src/flatmatter.ts b/src/flatmatter.ts
index f152400..fc14699 100644
--- a/src/flatmatter.ts
+++ b/src/flatmatter.ts
@@ -138,13 +138,9 @@ export default class FlatMatter {
* @returns {boolean}
*/
private isPipedValue(value: string): boolean {
- for (const part of this.composePipedValueParts(value)) {
- if (!this.isSimpleValue(part) && !this.isFunctionValue(part)) {
- return false;
- }
- }
-
- return true;
+ return this.composePipedValueParts(value).every(part => {
+ return this.isSimpleValue(part) || this.isFunctionValue(part)
+ });
}
/**
@@ -188,12 +184,12 @@ export default class FlatMatter {
return value === "true";
}
- if (!Number.isNaN(parseFloat(value))) {
- return parseFloat(value);
+ if (!Number.isNaN(parseInt(value)) && value.indexOf('.') === -1) {
+ return parseInt(value);
}
- if (!Number.isNaN(parseInt(value))) {
- return parseInt(value);
+ if (!Number.isNaN(parseFloat(value)) && value.indexOf('.') !== -1) {
+ return parseFloat(value);
}
return trimChar(value, '"');
diff --git a/src/serializers/to_object.test.ts b/src/serializers/to_object.test.ts
index 24fc54f..7059458 100644
--- a/src/serializers/to_object.test.ts
+++ b/src/serializers/to_object.test.ts
@@ -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,10 +82,85 @@ 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({
a: "VALUE-123",
});
-}); \ No newline at end of file
+});
+
+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",
+ });
+}) \ No newline at end of file