@trustme24/flext 1.10.5 → 2.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.
Files changed (52) hide show
  1. package/README.md +4 -4
  2. package/bin/flext.mjs +24 -0
  3. package/dist/dialects/index.d.ts +3 -0
  4. package/dist/dialects/latest.d.ts +6 -0
  5. package/dist/dialects/legacy-1-0-beta3.d.ts +5 -0
  6. package/dist/dialects/legacy-1-0-beta4.d.ts +5 -0
  7. package/dist/index.cjs +257 -253
  8. package/dist/index.cjs.map +4 -4
  9. package/dist/index.d.ts +8 -7
  10. package/dist/index.js +29 -29
  11. package/dist/index.js.map +4 -4
  12. package/dist/types.d.ts +3 -74
  13. package/package.json +13 -11
  14. package/src/dialects/index.ts +3 -0
  15. package/src/dialects/latest.ts +8 -0
  16. package/src/dialects/legacy-1-0-beta3.ts +7 -0
  17. package/src/dialects/legacy-1-0-beta4.ts +7 -0
  18. package/src/index.ts +75 -16
  19. package/src/tsconfig.json +3 -3
  20. package/src/types.ts +2 -102
  21. package/dist/engine.d.ts +0 -66
  22. package/dist/errors.d.ts +0 -25
  23. package/dist/index.css +0 -2
  24. package/dist/lib/index.d.ts +0 -70
  25. package/dist/modules/array/index.d.ts +0 -5
  26. package/dist/modules/cond/index.d.ts +0 -10
  27. package/dist/modules/date/index.d.ts +0 -16
  28. package/dist/modules/index.d.ts +0 -9
  29. package/dist/modules/match/index.d.ts +0 -5
  30. package/dist/modules/math/index.d.ts +0 -17
  31. package/dist/modules/media/index.d.ts +0 -3
  32. package/dist/modules/number/index.d.ts +0 -8
  33. package/dist/modules/put/index.d.ts +0 -6
  34. package/dist/modules/string/index.d.ts +0 -5
  35. package/src/engine.ts +0 -414
  36. package/src/env.d.ts +0 -4
  37. package/src/errors.ts +0 -51
  38. package/src/index.css +0 -1
  39. package/src/lib/index.ts +0 -931
  40. package/src/lib/unocssShadowDomHack.css +0 -133
  41. package/src/modules/array/index.ts +0 -68
  42. package/src/modules/cond/index.ts +0 -123
  43. package/src/modules/date/index.ts +0 -254
  44. package/src/modules/index.ts +0 -9
  45. package/src/modules/match/index.ts +0 -54
  46. package/src/modules/math/index.ts +0 -198
  47. package/src/modules/media/index.ts +0 -27
  48. package/src/modules/number/index.ts +0 -73
  49. package/src/modules/number/localeNames.json +0 -16
  50. package/src/modules/number/locales/kkKz.json +0 -98
  51. package/src/modules/put/index.ts +0 -41
  52. package/src/modules/string/index.ts +0 -45
@@ -1,8 +0,0 @@
1
- import { Obj } from '../../types';
2
- export declare const DEFAULT_LANG = "en";
3
- export declare const LOCALES: Obj;
4
- export declare function op(state: any): number | string | boolean;
5
- export declare function text(state: any): string;
6
- export declare function check(state: any): boolean;
7
- declare const _default: any;
8
- export default _default;
@@ -1,6 +0,0 @@
1
- import { SafeString } from 'handlebars';
2
- export declare const DEFAULT_COLOR = "text-blue-500";
3
- export declare function put(state: any): string;
4
- export declare function putWithColor(state: any): SafeString;
5
- declare const _default: any;
6
- export default _default;
@@ -1,5 +0,0 @@
1
- export declare function op(state: any): any[] | boolean;
2
- export declare function json(state: any): any[];
3
- export declare function check(state: any): boolean;
4
- declare const _default: any;
5
- export default _default;
package/src/engine.ts DELETED
@@ -1,414 +0,0 @@
1
- import { AST } from '@handlebars/parser';
2
- import * as types from '@/types';
3
- import * as lib from '@/lib';
4
- import * as errors from '@/errors';
5
- import * as modules from './modules';
6
-
7
-
8
- // Constants
9
-
10
- export const DEFAULT_HELPER_NAME = '__default';
11
-
12
- export const DEFAULT_MODEL_DEPTH = 10;
13
-
14
-
15
- // Types
16
-
17
- export type MacrosData = {
18
- version?: string|null,
19
- lang?: string|null,
20
- timeZone?: string|null,
21
- title?: string|null,
22
- moduleNames?: string[] | null,
23
- lineHeight?: string|null,
24
- fields?: types.Field[] | null,
25
- };
26
-
27
-
28
- // Classes
29
-
30
- export class SimpleFlext {
31
- declare public ast: AST.Program;
32
- declare public data: types.Obj;
33
- declare public helpers: types.Obj;
34
- public onGetProcessed: types.GetProcessedTemplateHandler = defaultGetProcessed;
35
- public onGetAst: types.GetTemplateAstHandler = lib.getAst;
36
-
37
- constructor(val: string|null = null, data: types.Obj = {}, helpers: types.Obj = {}) {
38
- if (val) this.setTemplate(val);
39
- this.setData({ ...this.data, ...data });
40
- this.setHelpers({ ...this.helpers, ...helpers});
41
- }
42
-
43
- public setTemplate(val: string): this {
44
-
45
- // Clearing the data
46
-
47
- this.data = {};
48
- this.helpers = {};
49
-
50
-
51
- // Getting the AST
52
-
53
- const template = this.onGetProcessed(val);
54
-
55
- this.ast = this.onGetAst(template);
56
-
57
-
58
- return this;
59
- }
60
-
61
- public setData(val: types.Obj): this {
62
- this.data = val;
63
- return this;
64
- }
65
-
66
- public setHelpers(val: types.Obj): this {
67
- this.helpers = val;
68
- return this;
69
- }
70
-
71
- public addHelper(name: string, val: any): this {
72
- this.helpers[name] = val;
73
- return this;
74
- }
75
-
76
- public setOnGetProcessed(val: types.GetProcessedTemplateHandler): this {
77
- this.onGetProcessed = val;
78
- return this;
79
- }
80
-
81
- public setOnGetAst(val: types.GetTemplateAstHandler): this {
82
- this.onGetAst = val;
83
- return this;
84
- }
85
-
86
- public getHtml(data?: types.Obj | null, helpers?: types.Obj | null): string {
87
- const template = lib.getTemplate(this.ast);
88
-
89
-
90
- // Doing some checks
91
-
92
- if (!template)
93
- throw new errors.BaseError('Flext: Unable to get HTML: No template');
94
-
95
-
96
- return lib.getHtml(
97
- template,
98
- data ?? this.data,
99
- helpers ?? this.helpers,
100
- );
101
- }
102
-
103
- public async getCss(data?: types.Obj | null, options: types.Obj = {}): Promise<string> {
104
- const template = lib.getTemplate(this.ast);
105
- const helpersObj = options?.helpers ?? {};
106
- const helpers = { ...this.helpers, ...helpersObj };
107
-
108
-
109
- // Doing some checks
110
-
111
- if (!template)
112
- throw new errors.BaseError('Flext: Unable to get CSS: No template');
113
-
114
-
115
- return await lib.getCss(
116
- template,
117
- data ?? this.data,
118
- { ...options, helpers },
119
- );
120
- }
121
-
122
- public get html(): string {
123
- return this.getHtml();
124
- }
125
- }
126
-
127
- export class Flext extends SimpleFlext {
128
- declare public version: string;
129
- declare public lang: string;
130
- declare public title: string;
131
- declare public timeZone: string;
132
- declare public lineHeight: number;
133
- declare public assets: types.Obj<Blob>;
134
- declare public fields: types.Field[];
135
- public onGetTitle: types.GetTemplateTitleHandler = lib.getHtmlH1;
136
- public onGetMacro: types.GetTemplateMacroHandler = lib.getMacros;
137
-
138
- constructor(val: string|null = null, data: types.Obj = {}, helpers: types.Obj = {}) {
139
- super(null, data, helpers);
140
-
141
- if (val) this.setTemplate(val);
142
- this.setData({ ...this.data, ...data });
143
- this.setHelpers({ ...this.helpers, ...helpers});
144
- }
145
-
146
- public useModule(...val: string[]): this {
147
- for (const name of val)
148
- this.addModule(name, modules[name]);
149
-
150
- return this;
151
- }
152
-
153
- public setTemplate(val: string): this {
154
-
155
- // Setting the template
156
-
157
- super.setTemplate(val);
158
-
159
-
160
- // Defining the variables
161
-
162
- const [ titleStr ] = this.onGetTitle(this.ast);
163
-
164
- const macros = this.onGetMacro(this.ast);
165
-
166
-
167
- // Getting the data
168
-
169
- const { version, lang, timeZone, title, moduleNames, lineHeight, fields } = macrosToData(macros);
170
-
171
-
172
- // Setting the data
173
-
174
- if (version)
175
- this.setVersion(version);
176
-
177
- if (lang)
178
- this.setLang(lang);
179
-
180
- if (timeZone)
181
- this.setTimeZone(timeZone);
182
-
183
- if (title || titleStr)
184
- this.setTitle(title ?? lib.ensureTitle(titleStr));
185
-
186
- if (lineHeight)
187
- this.setLineHeight(Number(lineHeight));
188
-
189
- if (fields && fields?.length)
190
- this.setFields(fields);
191
-
192
-
193
- // Using the modules
194
-
195
- this.useModule(...moduleNames);
196
-
197
-
198
- return this;
199
- }
200
-
201
- public setVersion(val: string): this {
202
- this.version = val;
203
- return this;
204
- }
205
-
206
- public setLang(val: string): this {
207
- this.lang = val;
208
- return this;
209
- }
210
-
211
- public setTitle(val: string): this {
212
- this.title = val;
213
- return this;
214
- }
215
-
216
- public setTimeZone(val: string): this {
217
- this.timeZone = val;
218
- return this;
219
- }
220
-
221
- public setLineHeight(val: number): this {
222
- this.lineHeight = val;
223
- return this;
224
- }
225
-
226
- public setAssets(val: types.Obj<Blob>): this {
227
- this.assets = val;
228
- return this;
229
- }
230
-
231
- public addAsset(name: string, val: Blob): this {
232
- this.assets[name] = val;
233
- return this;
234
- }
235
-
236
- public setFields(val: types.Field[]): this {
237
- this.fields = val;
238
- return this;
239
- }
240
-
241
- public addModule(name: string, val: any): this {
242
- const helpers = val?.helpers ?? {};
243
-
244
-
245
- // Iterating for each helper
246
-
247
- for (const helperName in helpers) {
248
- if (!lib.has(helpers, helperName)) continue;
249
-
250
-
251
- // Getting the data
252
-
253
- const handle = helpers[helperName];
254
-
255
- const isDefault = helperName === DEFAULT_HELPER_NAME;
256
-
257
-
258
- // Adding the helper
259
-
260
- const flext = this;
261
-
262
- const helper = function (..._args: any[]): any {
263
- const args = _args?.slice(0, -1) ?? [];
264
- const options = _args[_args.length - 1] ?? {};
265
- const namedArgs = options?.hash ?? {};
266
- // @ts-ignore
267
- const self = this;
268
- const getContent = () => options?.fn(self) ?? null;
269
-
270
- return handle({ flext, args, options, namedArgs, self, getContent });
271
- }
272
-
273
- if (isDefault)
274
- this.addHelper(name, helper);
275
- else
276
- this.addHelper(name + ':' + helperName, helper);
277
- }
278
-
279
-
280
- return this;
281
- }
282
-
283
- public setOnGetTitle(val: types.GetTemplateTitleHandler): this {
284
- this.onGetTitle = val;
285
- return this;
286
- }
287
-
288
- public setOnGetMacro(val: types.GetTemplateMacroHandler): this {
289
- this.onGetMacro = val;
290
- return this;
291
- }
292
-
293
- public getDataModel(depth: number = DEFAULT_MODEL_DEPTH): types.MetadataModelNode[] {
294
-
295
- // Defining the functions
296
-
297
- /**
298
- * TODO: kr: Costyl: Detects if it is a helper call (like 'put:noColor')
299
- */
300
- const isHelper = (node: types.DataModelNode): boolean => {
301
- for (const helperName in this.helpers) {
302
- if (!lib.has(this.helpers, helperName))
303
- continue;
304
- else if (node?.name === helperName)
305
- return true;
306
- }
307
-
308
- return false;
309
- }
310
-
311
- /**
312
- * TODO: kr: Costyl: Filters the helper calls (like 'put:noColor')
313
- */
314
- const isValid = (node: types.DataModelNode): boolean => !isHelper(node);
315
-
316
- const dataModelNodeToMetadata = (node: types.DataModelNode): types.MetadataModelNode => lib.dataModelNodeToMetadata(node, this.fields, {}, depth);
317
-
318
-
319
- // Getting the nodes
320
-
321
- const model = lib.getDataModel(this.ast);
322
- const nodes: types.DataModelNode[] = model?.$ ?? [];
323
-
324
-
325
- return nodes.filter(isValid).map(dataModelNodeToMetadata);
326
- }
327
-
328
- public getValidationErrors(data?: types.Obj | null, depth: number = DEFAULT_MODEL_DEPTH): errors.TemplateDataValidationError[] {
329
- return lib.getTemplateValidationErrorsByMetadata(data ?? this.data, this.model, depth);
330
- }
331
-
332
- public getIsValid(data?: types.Obj | null, depth: number = DEFAULT_MODEL_DEPTH): boolean {
333
- const errors = this.getValidationErrors(data ?? this.data, depth);
334
- return !errors?.length;
335
- }
336
-
337
- public get model(): types.MetadataModelNode[] {
338
- return this.getDataModel();
339
- }
340
-
341
- public get validationErrors(): errors.TemplateDataValidationError[] {
342
- return this.getValidationErrors();
343
- }
344
-
345
- public get errors(): errors.BaseError[] {
346
- return this.validationErrors;
347
- }
348
-
349
- public get isValid(): boolean {
350
- return this.getIsValid();
351
- }
352
- }
353
-
354
-
355
- // Functions
356
-
357
- export function macrosToData(macros: types.Macro[]): MacrosData {
358
-
359
- // Defining the functions
360
-
361
- const getAll = (..._val: string[]): types.Macro[] | null => macros?.filter(m => lib.inarr(m?.name, ..._val)) ?? null;
362
-
363
- const get = (_val: string): string | null => {
364
- const [macro] = getAll(_val);
365
- const [param] = macro?.params ?? [];
366
-
367
- return param?.value ?? null;
368
- };
369
-
370
-
371
- // Getting the data
372
-
373
- const version = get('v');
374
- const lang = get('lang');
375
- const title = get('title');
376
- const timeZone = get('timeZone');
377
- const modulesMacros = getAll('use');
378
- const lineHeight = get('lineHeight');
379
- const optionMacros = getAll('option');
380
- const fieldMacros = getAll('group', 'field');
381
-
382
-
383
- // Getting the fields
384
-
385
- const fieldValueOptions = optionMacros?.map(lib.macroToFieldValueOption) ?? null;
386
- const fields = fieldMacros?.map(lib.macroToField) ?? [];
387
-
388
- lib.applyValueOptionsToFields(fieldValueOptions, fields);
389
-
390
- lib.applyAbsoluteOrderToFields(fields);
391
-
392
-
393
- // Getting the field groups
394
-
395
- const fieldGroups = fields.filter(f => f?.extra?.macroName === 'group');
396
-
397
- for (const fieldGroup of fieldGroups)
398
- fieldGroup.type = 'object';
399
-
400
-
401
- // Getting the modules
402
-
403
- const moduleNames = modulesMacros.map(lib.macroToModuleNames).flat();
404
-
405
-
406
- return { version, lang, timeZone, title, moduleNames, lineHeight, fields };
407
- }
408
-
409
- export function defaultGetProcessed(val: string): string {
410
- return val;
411
- }
412
-
413
-
414
- export default Flext;
package/src/env.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare module '*.css' {
2
- const content: string
3
- export default content
4
- }
package/src/errors.ts DELETED
@@ -1,51 +0,0 @@
1
-
2
- // Base Errors
3
-
4
- export class BaseThrowable extends Error {
5
- public name = 'BaseThrowable';
6
-
7
- constructor(message: string, stack?: string|null) {
8
- super(message);
9
- if (stack) this.stack = stack;
10
- }
11
- }
12
-
13
- export class BaseWarning extends BaseThrowable {
14
- public name = 'BaseWarning';
15
- }
16
-
17
- export class BaseError extends BaseThrowable {
18
- public name = 'BaseError';
19
- }
20
-
21
-
22
- // Specific Errors
23
-
24
- export class PotentialLoopError extends BaseError {
25
- public name = 'PotentialLoopError';
26
-
27
- constructor(message: string = 'Potential loop detected: This might me an internal issue', stack?: string|null) {
28
- super(message, stack);
29
- }
30
- }
31
-
32
-
33
- // Template Errors
34
-
35
- export class TemplateError extends BaseError {
36
- public name = 'TemplateError';
37
- }
38
-
39
- export class TemplateSyntaxError extends TemplateError {
40
- public name = 'TemplateSyntaxError';
41
- }
42
-
43
- export class TemplateDataValidationError extends TemplateError {
44
- public name = 'TemplateDataValidationError';
45
- declare public fieldName: string;
46
-
47
- constructor(message: string = 'The entered data is invalid: This might me an internal issue', fieldName?: string|null) {
48
- super(message);
49
- if (fieldName) this.fieldName = fieldName;
50
- }
51
- }
package/src/index.css DELETED
@@ -1 +0,0 @@
1
- @import "tailwindcss" source(".");