janja 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.cjs.js +1948 -0
- package/dist/index.d.ts +227 -0
- package/dist/index.esm.js +1940 -0
- package/dist/index.js +1951 -0
- package/package.json +52 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
declare class Context {
|
|
2
|
+
options: Config;
|
|
3
|
+
context: string;
|
|
4
|
+
private index;
|
|
5
|
+
private contexts;
|
|
6
|
+
private expected;
|
|
7
|
+
constructor(options: Config);
|
|
8
|
+
in(): string;
|
|
9
|
+
out(): void;
|
|
10
|
+
expect(name: string): void;
|
|
11
|
+
matchAny(name: string): boolean;
|
|
12
|
+
match(name: string): boolean;
|
|
13
|
+
consume(name: string): boolean;
|
|
14
|
+
validate(): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare class OutScript {
|
|
18
|
+
options: Config;
|
|
19
|
+
private content;
|
|
20
|
+
private strOffset;
|
|
21
|
+
private varOffset;
|
|
22
|
+
constructor(options: Config);
|
|
23
|
+
get value(): string;
|
|
24
|
+
get script(): Script;
|
|
25
|
+
start(): void;
|
|
26
|
+
end(): void;
|
|
27
|
+
pushLine(...lines: string[]): Range;
|
|
28
|
+
pushStr(s: string, o?: {
|
|
29
|
+
trimStart?: boolean;
|
|
30
|
+
trimEnd?: boolean;
|
|
31
|
+
}): Range | void;
|
|
32
|
+
pushVar(v: string): Range;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Contains the source map information.
|
|
37
|
+
* @example
|
|
38
|
+
* - template:
|
|
39
|
+
* {{ foo }}
|
|
40
|
+
*
|
|
41
|
+
* - compiled:
|
|
42
|
+
* s+=e(foo);
|
|
43
|
+
*
|
|
44
|
+
* - sourceMap:
|
|
45
|
+
* Mapping {
|
|
46
|
+
* source: {
|
|
47
|
+
* start: 3,
|
|
48
|
+
* end: 6,
|
|
49
|
+
* },
|
|
50
|
+
* target: {
|
|
51
|
+
* start: 3,
|
|
52
|
+
* end: 8,
|
|
53
|
+
* }
|
|
54
|
+
* }
|
|
55
|
+
*/
|
|
56
|
+
declare class SourceMap {
|
|
57
|
+
options: Config;
|
|
58
|
+
mappings: Mapping[];
|
|
59
|
+
constructor(options: Config);
|
|
60
|
+
addMapping(source: Range, target: Range): void;
|
|
61
|
+
getRanges(offset: number): Range[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
65
|
+
type ObjectType = Record<string, any>;
|
|
66
|
+
interface Range {
|
|
67
|
+
start: number;
|
|
68
|
+
end: number;
|
|
69
|
+
}
|
|
70
|
+
interface Mapping {
|
|
71
|
+
source: Range;
|
|
72
|
+
target: Range;
|
|
73
|
+
}
|
|
74
|
+
type ExpTokenType = 'AND' | 'OR' | 'NOT' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'IF' | 'ELSE' | 'SET' | 'LP' | 'RP' | 'COMMA' | 'DOT' | 'STR' | 'NUM' | 'BOOL' | 'ID';
|
|
75
|
+
interface ExpToken<T = ExpTokenType> extends Range {
|
|
76
|
+
type: T;
|
|
77
|
+
value: string | number | boolean | null | undefined;
|
|
78
|
+
raw?: string;
|
|
79
|
+
}
|
|
80
|
+
type Checker = (token: ExpToken) => 'BACK' | 'BREAK' | undefined;
|
|
81
|
+
type ExpType = 'AND' | 'OR' | 'NOT' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'PIPE' | 'IF' | 'SET' | 'SEQ' | 'STR' | 'NUM' | 'BOOL' | 'ID';
|
|
82
|
+
interface BaseExp extends Range {
|
|
83
|
+
type: ExpType;
|
|
84
|
+
}
|
|
85
|
+
interface IdExp extends BaseExp {
|
|
86
|
+
type: 'ID';
|
|
87
|
+
value: string;
|
|
88
|
+
path?: IdExp[];
|
|
89
|
+
args?: Exp[];
|
|
90
|
+
}
|
|
91
|
+
interface StrExp extends BaseExp {
|
|
92
|
+
type: 'STR';
|
|
93
|
+
value: string;
|
|
94
|
+
}
|
|
95
|
+
interface NumExp extends BaseExp {
|
|
96
|
+
type: 'NUM';
|
|
97
|
+
value: number;
|
|
98
|
+
}
|
|
99
|
+
interface BoolExp extends BaseExp {
|
|
100
|
+
type: 'BOOL';
|
|
101
|
+
value: boolean;
|
|
102
|
+
}
|
|
103
|
+
interface NotExp extends BaseExp {
|
|
104
|
+
type: 'NOT';
|
|
105
|
+
argument: Exp;
|
|
106
|
+
}
|
|
107
|
+
interface BinaryExp extends BaseExp {
|
|
108
|
+
type: 'AND' | 'OR' | 'EQ' | 'NE' | 'GT' | 'LT' | 'GE' | 'LE' | 'IN' | 'NI' | 'OF' | 'ADD' | 'SUB' | 'MUL' | 'DIV' | 'MOD' | 'SET';
|
|
109
|
+
left: Exp;
|
|
110
|
+
right: Exp;
|
|
111
|
+
}
|
|
112
|
+
interface IfExp extends BaseExp {
|
|
113
|
+
type: 'IF';
|
|
114
|
+
test: Exp;
|
|
115
|
+
consequent: Exp;
|
|
116
|
+
alternative?: Exp;
|
|
117
|
+
}
|
|
118
|
+
interface PipeExp extends BaseExp {
|
|
119
|
+
type: 'PIPE';
|
|
120
|
+
left: Exp;
|
|
121
|
+
right: Exp;
|
|
122
|
+
}
|
|
123
|
+
interface SeqExp extends BaseExp {
|
|
124
|
+
type: 'SEQ';
|
|
125
|
+
elements: Exp[];
|
|
126
|
+
}
|
|
127
|
+
type Exp = IdExp | StrExp | NumExp | BoolExp | NotExp | BinaryExp | IfExp | PipeExp | SeqExp;
|
|
128
|
+
interface Globals {
|
|
129
|
+
translations?: Record<string, string>;
|
|
130
|
+
[key: string]: any;
|
|
131
|
+
}
|
|
132
|
+
type Filters = Record<string, (value: any, ...args: any[]) => any>;
|
|
133
|
+
type Script = (globals: Globals, filters: Filters, escape: (v: unknown) => unknown) => Promise<string>;
|
|
134
|
+
interface TagToken extends Range {
|
|
135
|
+
name: string;
|
|
136
|
+
value?: Exp | null;
|
|
137
|
+
raw: string;
|
|
138
|
+
previous: TagToken | null;
|
|
139
|
+
next: TagToken | null;
|
|
140
|
+
stripBefore?: boolean;
|
|
141
|
+
stripAfter?: boolean;
|
|
142
|
+
}
|
|
143
|
+
interface TagCompiler {
|
|
144
|
+
names: string[];
|
|
145
|
+
/**
|
|
146
|
+
* - Return `Range` to indicate the range of the tag.
|
|
147
|
+
* - Return `false` to pass the control to the next tag.
|
|
148
|
+
* - Return `void` to continue compiling.
|
|
149
|
+
*/
|
|
150
|
+
compile: (arg: {
|
|
151
|
+
token: TagToken;
|
|
152
|
+
ctx: Context;
|
|
153
|
+
out: OutScript;
|
|
154
|
+
}) => MaybePromise<Range | void | false>;
|
|
155
|
+
}
|
|
156
|
+
interface CacheValue {
|
|
157
|
+
template: string;
|
|
158
|
+
script: Script;
|
|
159
|
+
sourcemap: SourceMap;
|
|
160
|
+
}
|
|
161
|
+
interface Config {
|
|
162
|
+
globals?: Globals;
|
|
163
|
+
filters?: Filters;
|
|
164
|
+
compilers?: Record<string, TagCompiler[]>;
|
|
165
|
+
strictMode?: boolean;
|
|
166
|
+
autoEscape?: boolean;
|
|
167
|
+
stripComments?: boolean;
|
|
168
|
+
trimWhitespace?: boolean;
|
|
169
|
+
loader?: (path: string) => Promise<string>;
|
|
170
|
+
cache?: {
|
|
171
|
+
has: (path: string) => boolean;
|
|
172
|
+
get: (path: string) => CacheValue | undefined;
|
|
173
|
+
set: (path: string, content: CacheValue) => void;
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
declare class ExpCompiler {
|
|
178
|
+
private context;
|
|
179
|
+
private filters;
|
|
180
|
+
compile(expression: Exp | null, context: string, filters: string): string;
|
|
181
|
+
private compileExp;
|
|
182
|
+
private compileNot;
|
|
183
|
+
private compileId;
|
|
184
|
+
private compileStr;
|
|
185
|
+
private compileNum;
|
|
186
|
+
private compileBool;
|
|
187
|
+
private compileSeq;
|
|
188
|
+
private compilePipe;
|
|
189
|
+
private compileOf;
|
|
190
|
+
private compileSet;
|
|
191
|
+
private compileBinary;
|
|
192
|
+
private compileTernary;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
declare class ExpParser {
|
|
196
|
+
template: string;
|
|
197
|
+
tokens: ExpToken[];
|
|
198
|
+
length: number;
|
|
199
|
+
cursor: number;
|
|
200
|
+
parse(template: string): Exp | null;
|
|
201
|
+
private parseExp;
|
|
202
|
+
private parsePath;
|
|
203
|
+
private parseSequence;
|
|
204
|
+
private parsePipe;
|
|
205
|
+
private next;
|
|
206
|
+
private back;
|
|
207
|
+
private peek;
|
|
208
|
+
private check;
|
|
209
|
+
private consume;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
declare const compiler: ExpCompiler;
|
|
213
|
+
declare const parser: ExpParser;
|
|
214
|
+
|
|
215
|
+
declare class Engine {
|
|
216
|
+
protected options: Config;
|
|
217
|
+
constructor({ globals, filters, compilers, ...options }?: Config);
|
|
218
|
+
render(template: string, globals: Globals): Promise<string>;
|
|
219
|
+
renderFile(filepath: string, globals: Globals): Promise<string>;
|
|
220
|
+
private _compile;
|
|
221
|
+
private _render;
|
|
222
|
+
}
|
|
223
|
+
declare function render(template: string, data: ObjectType, options?: Config): Promise<string>;
|
|
224
|
+
declare function renderFile(filepath: string, data: ObjectType, options?: Config): Promise<string>;
|
|
225
|
+
|
|
226
|
+
export { Engine, ExpCompiler, ExpParser, compiler, parser, render, renderFile };
|
|
227
|
+
export type { BaseExp, BinaryExp, BoolExp, CacheValue, Checker, Config, Exp, ExpToken, ExpTokenType, ExpType, Filters, Globals, IdExp, IfExp, Mapping, MaybePromise, NotExp, NumExp, ObjectType, PipeExp, Range, Script, SeqExp, StrExp, TagCompiler, TagToken };
|