summaryrefslogtreecommitdiff
path: root/dist/index.d.ts
blob: 08f28d6723c2171230cd7ff3e23f93dc8a8e15e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
type Matter = {
    [key: string]: Matter | unknown;
};
interface Serializer {
    serialize(parsedConfig: Matter): unknown;
}
interface FlatMatterFn {
    name: string;
    compute(...args: unknown[]): unknown;
}
declare class FlatMatter {
    private content;
    private parsedConfig;
    private functions;
    constructor(content: string, functions?: FlatMatterFn[]);
    private parse;
    /**
     * Parses a given line of FlatMatter.
     *
     * @param {string} line
     * @returns {void}
     */
    private parseLine;
    private validateLineConformance;
    private validateLineHasKeyVal;
    private validateLineHasOnlyOneColonChar;
    /**
     * 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;
    /**
     * 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;
    /**
     * 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;
    /**
     * 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;
    /**
     * 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;
    /**
     * 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;
    /**
     * 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;
    /**
     * 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;
    /**
     * Takes an entire value of a line and composes it into a list
     * of piped parts.
     *
     * @param {string} value
     * @returns {string[]}
     */
    private composePipedValueParts;
    /**
     * 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;
    /**
     * Takes a Serializer and uses it to transform internal data
     * object to a desired output.
     *
     * @param {Serializer} serializer
     * @returns {unknown}
     */
    serialize(serializer: Serializer): unknown;
}

declare class ToObject implements Serializer {
    serialize(parsedConfig: Matter): Matter;
}

declare class ToJson implements Serializer {
    serialize(parsedConfig: Matter): string;
}

export { FlatMatter, type FlatMatterFn, type Matter, type Serializer, ToJson, ToObject };