diff options
| -rw-r--r-- | LICENSE.txt | 7 | ||||
| -rw-r--r-- | README.md | 29 | ||||
| -rw-r--r-- | deno.json | 7 | ||||
| -rw-r--r-- | mod.ts | 8 | ||||
| -rw-r--r-- | src/flatmatter.ts | 17 | ||||
| -rw-r--r-- | src/serializers/to_object.test.ts | 58 | ||||
| -rw-r--r-- | src/utils.ts | 23 |
7 files changed, 108 insertions, 41 deletions
diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..220f410 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +Copyright 2025 Asko Nõmm + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..69bac52 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# FlatMatter + +A YAML-like data serialization language with support for functions. FlatMatter is considered feature-complete and is +intentionally kept simple. Bug fixes and performance improvements are still made, of course. + +Example FlatMatter: + +```yaml +title: "My Blog" +last_updated: (get-content "posts") / (limit 1) / (get "published_at") / (date "YYYY-mm-dd") +posts: "posts" / get-content +``` + +FlatMatter aims to be more-or-less syntactically compatible with YAML for the simple reason of not +needing new editor plugins to have syntax highlighting, but it differs in that there is no indentation, +but instead dots to indicate hierarchy, like `site.title` which would result in a `site` object that +contains the `title` key. + +FlatMatter also supports functions, allowing you to build your own data DSL, and functions can also be piped with the +forward slash `/` character, meaning that the result of the left operation will be passed as the first argument +of the next function, and so on, to produce an end result. + +## Install + +To be written ... + +## Usage + +To be written ... @@ -1,4 +1,7 @@ { - "fmt": { - } + "name": "@asko/flatmatter", + "version": "0.1.0", + "license": "MIT", + "exports": "./mod.ts", + "fmt": {} } @@ -0,0 +1,8 @@ +import FlatMatter, { + type FlatMatterFn, + type Matter, +} from "./src/flatmatter.ts"; + +export { FlatMatter }; +export type { FlatMatterFn }; +export type { Matter }; diff --git a/src/flatmatter.ts b/src/flatmatter.ts index 84c8b7b..4bd141d 100644 --- a/src/flatmatter.ts +++ b/src/flatmatter.ts @@ -70,8 +70,7 @@ export default class FlatMatter { this.parsedConfig = { ...this.parsedConfig, ...config }; } - private validateLineConformance(line: string): void { - } + private validateLineConformance(line: string): void {} private validateLineHasKeyVal(line: string): ConformanceResult { return { @@ -167,9 +166,7 @@ export default class FlatMatter { if (this.isFunctionValue(value)) { return { value: null, - computeActions: [ - this.parseFunctionValue(value), - ], + computeActions: [this.parseFunctionValue(value)], }; } @@ -200,7 +197,7 @@ export default class FlatMatter { return parseInt(value); } - return value.substring(1, value.length - 1); + return trimChar(value, '"'); } /** @@ -220,8 +217,7 @@ export default class FlatMatter { }; } - const fnName = trimChar(value, ["(", ")"]).split(" ")[0] - .trim(); + const fnName = trimChar(value, ["(", ")"]).split(" ")[0].trim(); const fnArgs = this.parseFunctionValueArgs(value); return { @@ -264,7 +260,10 @@ export default class FlatMatter { * @returns {unknown[]} */ private parseFunctionValueArgs(value: string): unknown[] { - const parts = value.substring(1, value.length - 1).split(" ").slice(1); + const parts = value + .substring(1, value.length - 1) + .split(" ") + .slice(1); if (!parts.length) { return []; diff --git a/src/serializers/to_object.test.ts b/src/serializers/to_object.test.ts index d3d2255..98b165a 100644 --- a/src/serializers/to_object.test.ts +++ b/src/serializers/to_object.test.ts @@ -3,35 +3,39 @@ import FlatMatter from "../flatmatter.ts"; import ToObject from "./to_object.ts"; Deno.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"', + ); - assertEquals(fm.serialize(new ToObject), { - a: true, - b: false, - c: 1, - d: 12.5, - f: "some string" - }); + assertEquals(fm.serialize(new ToObject()), { + a: true, + b: false, + c: 1, + d: 12.5, + f: "some string", + }); }); Deno.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\"") + const fm = new FlatMatter( + 'a.a: true\nb.b: false\nc.c: 1\nd.d: 12.5\nf.f: "some string"', + ); - assertEquals(fm.serialize(new ToObject), { - a: { - a: true, - }, - b: { - b: false, - }, - c: { - c: 1 - }, - d: { - d: 12.5 - }, - f: { - f: "some string" - } - }); -})
\ No newline at end of file + assertEquals(fm.serialize(new ToObject()), { + a: { + a: true, + }, + b: { + b: false, + }, + c: { + c: 1, + }, + d: { + d: 12.5, + }, + f: { + f: "some string", + }, + }); +}); diff --git a/src/utils.ts b/src/utils.ts index 444e9a0..56995fc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,23 @@ -export function trimChar(input: string, chars: string | string[]): string { - if (typeof chars === "string") { - return input; +/** + * 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") { + char = [char]; + } + + for (const c of char) { + if (input.charAt(0) === c) { + input = input.substring(1); + } + + if (input.charAt(input.length - 1) === c) { + input = input.substring(0, input.length - 1); + } } return input; |
