@lbaz/tetr 0.0.2 → 0.0.4

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.
@@ -0,0 +1,9 @@
1
+ export declare const DEFINITION_TYPES: {
2
+ readonly Array: "Array";
3
+ readonly String: "String";
4
+ readonly Number: "Number";
5
+ };
6
+ export declare const DATA_TYPES: {
7
+ DICT: string;
8
+ LIST: string;
9
+ };
@@ -0,0 +1,10 @@
1
+ export const DEFINITION_TYPES = {
2
+ 'Array': 'Array',
3
+ 'String': 'String',
4
+ 'Number': 'Number',
5
+ };
6
+ export const DATA_TYPES = {
7
+ DICT: "DICT",
8
+ LIST: "LIST"
9
+ };
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/constants/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,QAAQ;CACV,CAAC;AAEX,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,IAAI,EAAE,MAAM;IACZ,IAAI,EAAE,MAAM;CACb,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './transpiler';
@@ -0,0 +1,2 @@
1
+ export * from './transpiler';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { Block, PossibleDataPayload, CreateBlockOutput, Data, BaseChunk } from "../types";
2
+ import { Chunk, PatchBlock } from "../types";
3
+ export declare class TemplateTranspiler {
4
+ generate(block: Block, model: PatchBlock): CreateBlockOutput | null;
5
+ generateChunkData(chunks: {
6
+ [key: string]: Chunk;
7
+ } | undefined | null, model: PatchBlock): {
8
+ chunks: Array<{
9
+ name: string;
10
+ markup: string;
11
+ }>;
12
+ styles: string;
13
+ };
14
+ extractStyles(chunk: BaseChunk | undefined | null): string;
15
+ replaceData(template: string, data: Data, delimiters?: string[]): string;
16
+ replaceTemplateData(template: string, data: Data): string;
17
+ replaceChunksData(template: string, data: Data): string;
18
+ createChunkMarkup(template: string, data: PossibleDataPayload): string;
19
+ }
@@ -0,0 +1,72 @@
1
+ import { DATA_TYPES } from "../constants";
2
+ export class TemplateTranspiler {
3
+ generate(block, model) {
4
+ var _a, _b;
5
+ if (!((_a = block === null || block === void 0 ? void 0 : block.template) === null || _a === void 0 ? void 0 : _a.markup)) {
6
+ console.error(`Block template ${block === null || block === void 0 ? void 0 : block.id} not found`);
7
+ return null;
8
+ }
9
+ let styles = (block.styles || '');
10
+ const _block = this.replaceTemplateData((((_b = block === null || block === void 0 ? void 0 : block.template) === null || _b === void 0 ? void 0 : _b.markup) || ''), (model === null || model === void 0 ? void 0 : model.data) || {});
11
+ const _chunks = this.generateChunkData(block === null || block === void 0 ? void 0 : block.chunks, model);
12
+ //@ts-ignore
13
+ const _processingChunks = _chunks.chunks.reduce((acc, { name, markup }) => {
14
+ acc[name] = markup;
15
+ return acc;
16
+ }, {});
17
+ //@ts-ignore
18
+ const _styles = `${styles} ${_chunks.styles}`.trim();
19
+ return {
20
+ markup: this.replaceChunksData(_block, _processingChunks),
21
+ styles: _styles
22
+ };
23
+ }
24
+ generateChunkData(chunks, model) {
25
+ if (!chunks) {
26
+ return { chunks: [], styles: '' };
27
+ }
28
+ //@ts-ignore
29
+ const chunksMap = Object.entries(chunks);
30
+ return chunksMap.reduce((acc, [chunkName, payload]) => {
31
+ var _a, _b, _c, _d, _e, _f;
32
+ const dataType = (_a = payload === null || payload === void 0 ? void 0 : payload.data) === null || _a === void 0 ? void 0 : _a.type;
33
+ const emptyData = dataType === DATA_TYPES.DICT ? {} : [];
34
+ const modelChunkData = (_d = (_c = (_b = model === null || model === void 0 ? void 0 : model.chunks) === null || _b === void 0 ? void 0 : _b[chunkName]) === null || _c === void 0 ? void 0 : _c.data) !== null && _d !== void 0 ? _d : emptyData;
35
+ const template = ((_e = payload === null || payload === void 0 ? void 0 : payload.template) === null || _e === void 0 ? void 0 : _e.markup) || '';
36
+ const styles = (payload === null || payload === void 0 ? void 0 : payload.styles) || '';
37
+ const markup = this.createChunkMarkup(template, modelChunkData);
38
+ const modelStyles = this.extractStyles((_f = model === null || model === void 0 ? void 0 : model.chunks) === null || _f === void 0 ? void 0 : _f[chunkName]);
39
+ acc['styles'] += (styles + modelStyles);
40
+ acc['chunks'].push({ name: chunkName, markup: markup });
41
+ return acc;
42
+ }, { chunks: [], styles: '' });
43
+ }
44
+ extractStyles(chunk) {
45
+ return (chunk === null || chunk === void 0 ? void 0 : chunk.styles) || '';
46
+ }
47
+ replaceData(template, data, delimiters = ['{{', '}}']) {
48
+ //@ts-ignore
49
+ const reg = new RegExp(`${delimiters[0]}[\\s]*\\w+[\\s]*${delimiters[1]}`, 'gi');
50
+ //@ts-ignore
51
+ return template.replace(reg, (m) => {
52
+ //@ts-ignore
53
+ const key = (m || '').trim().slice(2, -2);
54
+ return data[key] || "";
55
+ });
56
+ }
57
+ replaceTemplateData(template, data) {
58
+ return this.replaceData(template, data);
59
+ }
60
+ replaceChunksData(template, data) {
61
+ return this.replaceData(template, data, ['[[', ']]']);
62
+ }
63
+ createChunkMarkup(template, data) {
64
+ //@ts-ignore
65
+ return Array.isArray(data)
66
+ ? data.map((item) => {
67
+ return this.replaceTemplateData(template, item);
68
+ }).join('')
69
+ : this.replaceTemplateData(template, data);
70
+ }
71
+ }
72
+ //# sourceMappingURL=transpiler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transpiler.js","sourceRoot":"","sources":["../../src/core/transpiler.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AAGxC,MAAM,OAAO,kBAAkB;IAC7B,QAAQ,CAAC,KAAY,EAAE,KAAiB;;QAEtC,IAAG,CAA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ClD,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;YACX,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC;CACF"}
@@ -1,3 +1,3 @@
1
- export * from './core';
2
- export * from './types';
3
- export * from './constants';
1
+ export * from './core';
2
+ export * from './types';
3
+ export * from './constants';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './core';
2
+ export * from './types';
3
+ export * from './constants';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { Template } from "./template";
2
+ import { Chunk, PatchChunk } from "./chunk";
3
+ import { Data, DataDefinition } from "./data";
4
+ export type BlockId = string;
5
+ export type BlockPreview = string;
6
+ export interface BaseBlock<T, D> {
7
+ id: BlockId;
8
+ previewName: BlockPreview;
9
+ customName?: BlockPreview;
10
+ chunks?: {
11
+ [key: string]: T;
12
+ };
13
+ data?: D;
14
+ styles?: string;
15
+ }
16
+ export interface Block extends BaseBlock<Chunk, DataDefinition> {
17
+ template: Template;
18
+ }
19
+ export interface PatchBlock extends BaseBlock<PatchChunk, Data> {
20
+ }
21
+ export interface CreateBlockOutput {
22
+ markup: string;
23
+ styles: string;
24
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=block.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block.js","sourceRoot":"","sources":["../../src/types/block.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ import { Template } from "./template";
2
+ import { DataDefinition, PossibleDataPayload } from "./data";
3
+ export interface BaseChunk {
4
+ styles?: string;
5
+ }
6
+ export interface Chunk extends BaseChunk {
7
+ template: Template;
8
+ data?: DataDefinition;
9
+ }
10
+ export interface PatchChunk extends BaseChunk {
11
+ data?: PossibleDataPayload;
12
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=chunk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk.js","sourceRoot":"","sources":["../../src/types/chunk.ts"],"names":[],"mappings":""}
@@ -0,0 +1,51 @@
1
+ import { DATA_TYPES, DEFINITION_TYPES } from "../constants";
2
+ export type Data<T = unknown> = {
3
+ [key: string]: T;
4
+ };
5
+ export type DefinitionType = keyof typeof DEFINITION_TYPES;
6
+ export type DataType = keyof typeof DATA_TYPES;
7
+ export interface ItemDefinition {
8
+ type: DefinitionType;
9
+ defaultValue: string;
10
+ possibleVariants?: Array<unknown>;
11
+ }
12
+ export interface DataDefinition {
13
+ type: DataType;
14
+ defaultValue?: unknown;
15
+ definitions: Data<ItemDefinition>;
16
+ }
17
+ export type PossibleDataPayload<T = any> = Data<T> | Array<Data<T>>;
18
+ export interface Meta {
19
+ name?: string;
20
+ property?: string;
21
+ content: string;
22
+ }
23
+ export interface PageBody {
24
+ id: string;
25
+ path: string;
26
+ chunks: {
27
+ [key: string]: {
28
+ data: {
29
+ [key: string]: unknown;
30
+ };
31
+ };
32
+ };
33
+ }
34
+ export interface Page {
35
+ id: string;
36
+ head: {
37
+ title: string;
38
+ meta: Array<Meta>;
39
+ styles: Array<string>;
40
+ scripts: Array<string>;
41
+ includes: {
42
+ styles: Array<string>;
43
+ scripts: Array<string>;
44
+ };
45
+ };
46
+ body: Array<PageBody>;
47
+ }
48
+ export interface SiteSourceData {
49
+ robots?: string;
50
+ pages: Array<Page>;
51
+ }
@@ -0,0 +1,3 @@
1
+ ;
2
+ export {};
3
+ //# sourceMappingURL=data.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data.js","sourceRoot":"","sources":["../../src/types/data.ts"],"names":[],"mappings":"AAmBC,CAAC"}
@@ -1,4 +1,4 @@
1
- export * from './template';
2
- export * from './block';
3
- export * from './chunk';
4
- export * from './data';
1
+ export * from './template';
2
+ export * from './block';
3
+ export * from './chunk';
4
+ export * from './data';
@@ -0,0 +1,5 @@
1
+ export * from './template';
2
+ export * from './block';
3
+ export * from './chunk';
4
+ export * from './data';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC"}
@@ -1,3 +1,3 @@
1
- export interface Template {
2
- markup: string;
3
- }
1
+ export interface Template {
2
+ markup: string;
3
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=template.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template.js","sourceRoot":"","sources":["../../src/types/template.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@lbaz/tetr",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Template transpiler",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "types": "dist/index.d.ts",
7
8
  "scripts": {
8
9
  "build": "tsc",
9
10
  "test": "jest"
10
11
  },
12
+ "files": ["dist", "package.json"],
11
13
  "author": "",
12
14
  "license": "ISC",
13
15
  "devDependencies": {
package/jest.config.js DELETED
@@ -1,198 +0,0 @@
1
- /**
2
- * For a detailed explanation regarding each configuration property, visit:
3
- * https://jestjs.io/docs/configuration
4
- */
5
-
6
- /** @type {import('jest').Config} */
7
- const config = {
8
- // All imported modules in your tests should be mocked automatically
9
- // automock: false,
10
-
11
- // Stop running tests after `n` failures
12
- // bail: 0,
13
-
14
- // The directory where Jest should store its cached dependency information
15
- // cacheDirectory: "C:\\Users\\vl\\AppData\\Local\\Temp\\jest",
16
-
17
- // Automatically clear mock calls, instances, contexts and results before every test
18
- clearMocks: true,
19
-
20
- // Indicates whether the coverage information should be collected while executing the test
21
- collectCoverage: true,
22
-
23
- // An array of glob patterns indicating a set of files for which coverage information should be collected
24
- // collectCoverageFrom: undefined,
25
-
26
- // The directory where Jest should output its coverage files
27
- coverageDirectory: "coverage",
28
-
29
- // An array of regexp pattern strings used to skip coverage collection
30
- // coveragePathIgnorePatterns: [
31
- // "\\\\node_modules\\\\"
32
- // ],
33
-
34
- // Indicates which provider should be used to instrument code for coverage
35
- coverageProvider: "v8",
36
-
37
- // A list of reporter names that Jest uses when writing coverage reports
38
- // coverageReporters: [
39
- // "json",
40
- // "text",
41
- // "lcov",
42
- // "clover"
43
- // ],
44
-
45
- // An object that configures minimum threshold enforcement for coverage results
46
- // coverageThreshold: undefined,
47
-
48
- // A path to a custom dependency extractor
49
- // dependencyExtractor: undefined,
50
-
51
- // Make calling deprecated APIs throw helpful error messages
52
- // errorOnDeprecated: false,
53
-
54
- // The default configuration for fake timers
55
- // fakeTimers: {
56
- // "enableGlobally": false
57
- // },
58
-
59
- // Force coverage collection from ignored files using an array of glob patterns
60
- // forceCoverageMatch: [],
61
-
62
- // A path to a module which exports an async function that is triggered once before all test suites
63
- // globalSetup: undefined,
64
-
65
- // A path to a module which exports an async function that is triggered once after all test suites
66
- // globalTeardown: undefined,
67
-
68
- // A set of global variables that need to be available in all test environments
69
- // globals: {},
70
-
71
- // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
72
- // maxWorkers: "50%",
73
-
74
- // An array of directory names to be searched recursively up from the requiring module's location
75
- // moduleDirectories: [
76
- // "node_modules"
77
- // ],
78
-
79
- // An array of file extensions your modules use
80
- // moduleFileExtensions: [
81
- // "js",
82
- // "mjs",
83
- // "cjs",
84
- // "jsx",
85
- // "ts",
86
- // "tsx",
87
- // "json",
88
- // "node"
89
- // ],
90
-
91
- // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
92
- // moduleNameMapper: {},
93
-
94
- // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
95
- // modulePathIgnorePatterns: [],
96
-
97
- // Activates notifications for test results
98
- // notify: false,
99
-
100
- // An enum that specifies notification mode. Requires { notify: true }
101
- // notifyMode: "failure-change",
102
-
103
- // A preset that is used as a base for Jest's configuration
104
- preset: "ts-jest",
105
-
106
- // Run tests from one or more projects
107
- // projects: undefined,
108
-
109
- // Use this configuration option to add custom reporters to Jest
110
- // reporters: undefined,
111
-
112
- // Automatically reset mock state before every test
113
- // resetMocks: false,
114
-
115
- // Reset the module registry before running each individual test
116
- // resetModules: false,
117
-
118
- // A path to a custom resolver
119
- // resolver: undefined,
120
-
121
- // Automatically restore mock state and implementation before every test
122
- // restoreMocks: false,
123
-
124
- // The root directory that Jest should scan for tests and modules within
125
- // rootDir: undefined,
126
-
127
- // A list of paths to directories that Jest should use to search for files in
128
- // roots: [
129
- // "<rootDir>"
130
- // ],
131
-
132
- // Allows you to use a custom runner instead of Jest's default test runner
133
- // runner: "jest-runner",
134
-
135
- // The paths to modules that run some code to configure or set up the testing environment before each test
136
- // setupFiles: [],
137
-
138
- // A list of paths to modules that run some code to configure or set up the testing framework before each test
139
- // setupFilesAfterEnv: [],
140
-
141
- // The number of seconds after which a test is considered as slow and reported as such in the results.
142
- // slowTestThreshold: 5,
143
-
144
- // A list of paths to snapshot serializer modules Jest should use for snapshot testing
145
- // snapshotSerializers: [],
146
-
147
- // The test environment that will be used for testing
148
- // testEnvironment: "jest-environment-node",
149
-
150
- // Options that will be passed to the testEnvironment
151
- // testEnvironmentOptions: {},
152
-
153
- // Adds a location field to test results
154
- // testLocationInResults: false,
155
-
156
- // The glob patterns Jest uses to detect test files
157
- // testMatch: [
158
- // "**/__tests__/**/*.[jt]s?(x)",
159
- // "**/?(*.)+(spec|test).[tj]s?(x)"
160
- // ],
161
-
162
- // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
163
- // testPathIgnorePatterns: [
164
- // "\\\\node_modules\\\\"
165
- // ],
166
-
167
- // The regexp pattern or array of patterns that Jest uses to detect test files
168
- // testRegex: [],
169
-
170
- // This option allows the use of a custom results processor
171
- // testResultsProcessor: undefined,
172
-
173
- // This option allows use of a custom test runner
174
- // testRunner: "jest-circus/runner",
175
-
176
- // A map from regular expressions to paths to transformers
177
- // transform: undefined,
178
-
179
- // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
180
- // transformIgnorePatterns: [
181
- // "\\\\node_modules\\\\",
182
- // "\\.pnp\\.[^\\\\]+$"
183
- // ],
184
-
185
- // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
186
- // unmockedModulePathPatterns: undefined,
187
-
188
- // Indicates whether each individual test should be reported during the run
189
- // verbose: undefined,
190
-
191
- // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
192
- // watchPathIgnorePatterns: [],
193
-
194
- // Whether to use watchman for file crawling
195
- // watchman: true,
196
- };
197
-
198
- module.exports = config;
@@ -1,10 +0,0 @@
1
- export const DEFINITION_TYPES = {
2
- 'Array': 'Array',
3
- 'String': 'String',
4
- 'Number': 'Number',
5
- } as const;
6
-
7
- export const DATA_TYPES = {
8
- DICT: "DICT",
9
- LIST: "LIST"
10
- };
package/src/core/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './transpiler';
@@ -1,89 +0,0 @@
1
- import {Block, PossibleDataPayload, CreateBlockOutput, Data, DataDefinition, BaseChunk} from "../types";
2
- import {Chunk, PatchBlock} from "../types";
3
- import {DATA_TYPES} from "../constants";
4
-
5
-
6
- export class TemplateTranspiler {
7
- generate(block: Block, model: PatchBlock): CreateBlockOutput | null {
8
-
9
- if(!block?.template?.markup) {
10
- console.error(`Block template ${block?.id} not found`);
11
- return null;
12
- }
13
-
14
- let styles = (block.styles || '');
15
-
16
- const _block = this.replaceTemplateData(
17
- (block?.template?.markup || ''),
18
- model?.data || {}
19
- );
20
-
21
- const _chunks = this.generateChunkData(block?.chunks, model);
22
- //@ts-ignore
23
- const _processingChunks = _chunks.chunks.reduce((acc, {name, markup}) => {
24
- acc[name] = markup;
25
- return acc;
26
- }, {} as {[key: string]: string});
27
-
28
- //@ts-ignore
29
- const _styles = `${styles} ${_chunks.styles}`.trim();
30
-
31
- return {
32
- markup: this.replaceChunksData(_block, _processingChunks),
33
- styles: _styles
34
- };
35
- }
36
-
37
- generateChunkData(chunks: {[key: string]: Chunk} | undefined | null, model: PatchBlock):
38
- {chunks: Array<{name: string, markup: string}>, styles: string} {
39
- if(!chunks) {
40
- return {chunks: [], styles: ''};
41
- }
42
- //@ts-ignore
43
- const chunksMap = Object.entries(chunks);
44
- return chunksMap.reduce((acc, [chunkName, payload]) => {
45
- const dataType = payload?.data?.type;
46
- const emptyData = dataType === DATA_TYPES.DICT ? {} : [];
47
- const modelChunkData = model?.chunks?.[chunkName]?.data ?? emptyData;
48
- const template = payload?.template?.markup || '';
49
- const styles = payload?.styles || '';
50
- const markup = this.createChunkMarkup(template, modelChunkData);
51
- const modelStyles = this.extractStyles(model?.chunks?.[chunkName]);
52
- acc['styles'] += (styles + modelStyles);
53
- acc['chunks'].push({name: chunkName, markup: markup});
54
- return acc;
55
- }, {chunks: [] as Array<{name: string, markup: string}>, styles: ''});
56
- }
57
-
58
- extractStyles(chunk: BaseChunk | undefined | null): string {
59
- return chunk?.styles || '';
60
- }
61
-
62
- replaceData(template: string, data: Data, delimiters = ['{{', '}}']): string {
63
- //@ts-ignore
64
- const reg = new RegExp(`${delimiters[0]}[\\s]*\\w+[\\s]*${delimiters[1]}`, 'gi');
65
- //@ts-ignore
66
- return template.replace(reg, (m) => {
67
- //@ts-ignore
68
- const key = (m || '').trim().slice(2, -2);
69
- return data[key] || "";
70
- })
71
- }
72
-
73
- replaceTemplateData(template: string, data: Data): string {
74
- return this.replaceData(template, data);
75
- }
76
-
77
- replaceChunksData(template: string, data: Data): string {
78
- return this.replaceData(template, data, ['[[', ']]']);
79
- }
80
-
81
- createChunkMarkup(template: string, data: PossibleDataPayload): string {
82
- //@ts-ignore
83
- return Array.isArray(data)
84
- ? data.map((item) => {
85
- return this.replaceTemplateData(template, item);
86
- }).join('')
87
- : this.replaceTemplateData(template, data);
88
- }
89
- }
@@ -1,126 +0,0 @@
1
- import {TemplateTranspiler} from '../core/transpiler';
2
- import {Block, Chunk, PatchBlock, PossibleDataPayload} from "../types";
3
- import {
4
- chunksTest01, chunksTest02, chunksTest03, chunksTest04, chunksTest05, chunkTestModel00,
5
- chunkTestModel01, chunkTestModel02, chunkTestModel03, chunkTestModel04, chunkTestModel05,
6
- test01,
7
- test01ModelData,
8
- test02,
9
- test02ModelData
10
- } from "./templates-compiler-mocks";
11
-
12
- describe('REPLACER', () => {
13
- test('Data replacer 1', () => {
14
- const tt = new TemplateTranspiler();
15
- expect(tt.replaceData('{{test}}', {test: 'value01'})).toBe('value01');
16
- });
17
-
18
- test('Data replacer 2', () => {
19
- const tt = new TemplateTranspiler();
20
- expect(tt.replaceData('{{{test}}}', {test: 'value01'})).toBe('{value01}');
21
- });
22
-
23
- test('Data replacer 3', () => {
24
- const tt = new TemplateTranspiler();
25
- expect(tt.replaceData('__{test}__', {test: 'value01'})).toBe('__{test}__');
26
- });
27
-
28
-
29
- test('Data replacer 5', () => {
30
- const tt = new TemplateTranspiler();
31
- expect(tt.replaceTemplateData('{{test}}', {test: 'value01'})).toBe('value01');
32
- });
33
-
34
- test('Data replacer 5', () => {
35
- const tt = new TemplateTranspiler();
36
- expect(tt.replaceChunksData('[[test]]', {test: 'value01'})).toBe('value01');
37
- });
38
-
39
- test('Data replacer 6', () => {
40
- const tt = new TemplateTranspiler();
41
- expect(tt.createChunkMarkup('<div>{{test}}</div>', {test: 'value01'}))
42
- .toBe('<div>value01</div>');
43
- });
44
-
45
- test('Data replacer 7', () => {
46
- const tt = new TemplateTranspiler();
47
- expect(tt.createChunkMarkup('<div>{{test}}</div>', [{test: 'value01'}, {test: 'value02'}] as PossibleDataPayload))
48
- .toBe('<div>value01</div><div>value02</div>');
49
- })
50
-
51
- });
52
-
53
- describe('TRANSPILER replace root data', () => {
54
- test('Simple replace (model data exist)', () => {
55
- const tt = new TemplateTranspiler();
56
- const blockCopy: Block = JSON.parse(JSON.stringify(test01));
57
- blockCopy.template.markup = null;
58
- expect(tt.generate(blockCopy, test01ModelData)).toBeNull();
59
- });
60
- });
61
-
62
- describe('TRANSPILER replace root data & exist chunks', () => {
63
- test('Simple replace (model data exist & chunks)', () => {
64
- const tt = new TemplateTranspiler();
65
- expect(tt.generate(test02, test02ModelData)).toEqual({
66
- markup: '<section><div>patch</div><p>Patch paragraph value</p></section>',
67
- styles: ''
68
- });
69
- });
70
- });
71
-
72
- describe('CHUNKS generated', () => {
73
- test('0', () => {
74
- const tt = new TemplateTranspiler();
75
- expect(tt.generateChunkData(null, chunkTestModel00)).toEqual({
76
- chunks: [],
77
- styles: ''
78
- });
79
- });
80
-
81
- test('1', () => {
82
- const tt = new TemplateTranspiler();
83
- expect(tt.generateChunkData(chunksTest01, chunkTestModel01)).toEqual({
84
- chunks: [{name: 'test', markup: '<div>patched</div>'}],
85
- styles: ''
86
- });
87
- });
88
-
89
- test('2', () => {
90
- const tt = new TemplateTranspiler();
91
- expect(tt.generateChunkData(chunksTest02, chunkTestModel02)).toEqual({
92
- chunks: [{name: 'test', markup: '<div>patched01</div><div>patched02</div>'}],
93
- styles: ''
94
- });
95
- });
96
-
97
- test('3', () => {
98
- const tt = new TemplateTranspiler();
99
- expect(tt.generateChunkData(chunksTest03, chunkTestModel03)).toEqual({
100
- chunks: [{name: 'test', markup: '<div>patched01</div><div>patched02</div>'}],
101
- styles: 'div{color: red};div.test{color: blue};'
102
- });
103
- });
104
-
105
- test('4', () => {
106
- const tt = new TemplateTranspiler();
107
- expect(tt.generateChunkData(chunksTest04, chunkTestModel04)).toEqual({
108
- chunks: [
109
- {name: 'test', markup: '<div>patched01</div><div>patched02</div>'},
110
- {name: 'label', markup: '<label>_label_</label>'}
111
- ],
112
- styles: 'div{color: red};div.test{color: blue};label{color: red};'
113
- });
114
- });
115
-
116
- test('5', () => {
117
- const tt = new TemplateTranspiler();
118
- expect(tt.generateChunkData(chunksTest05, chunkTestModel05)).toEqual({
119
- chunks: [
120
- {name: 'test', markup: '<div>patched01</div><div>patched02</div>'},
121
- {name: 'label', markup: '<label>_label_</label>'}
122
- ],
123
- styles: 'div{color: red};div.test{color: blue};.modelStylesTEST_PROP{color: red};label{color: red};.modelStylesLABEL_PROP{color: green};'
124
- });
125
- })
126
- });
@@ -1,261 +0,0 @@
1
- import {Block, Chunk, DataDefinition, DataType, DefinitionType, ItemDefinition, PatchBlock} from "../../types";
2
- import {DATA_TYPES, DEFINITION_TYPES} from "../../constants";
3
-
4
- export const test01: Block = {
5
- id: 'test01',
6
- previewName: 'Test 01',
7
- template: {
8
- markup: `<div>{{name}}</div>`
9
- },
10
- data: {
11
- type: DATA_TYPES.DICT as DataType,
12
- definitions: {
13
- name: {
14
- type: DEFINITION_TYPES.String as DefinitionType,
15
- defaultValue: 'test'
16
- } as ItemDefinition
17
- }
18
- } as DataDefinition
19
- }
20
-
21
- export const test01ModelData: PatchBlock = {
22
- id: 'test01',
23
- previewName: 'Test 01',
24
- data: {
25
- name: 'patch'
26
- }
27
- }
28
-
29
- export const test02: Block = {
30
- id: 'test02',
31
- previewName: 'Test 02',
32
- template: {
33
- markup: `<section><div>{{name}}</div>[[paragraph]]</section>`
34
- },
35
- data: {
36
- type: DATA_TYPES.DICT as DataType,
37
- definitions: {
38
- name: {
39
- type: DEFINITION_TYPES.String as DefinitionType,
40
- defaultValue: 'test'
41
- } as ItemDefinition
42
- }
43
- } as DataDefinition,
44
- chunks: {
45
- paragraph: {
46
- template: {
47
- markup: `<p>{{descriptionValue}}</p>`
48
- },
49
- data: {
50
- type: DATA_TYPES.DICT as DataType,
51
- definitions: {
52
- descriptionValue: {
53
- type: DEFINITION_TYPES.String as DefinitionType,
54
- defaultValue: 'Test paragraph'
55
- }
56
- }
57
- }
58
- }
59
- }
60
- }
61
-
62
- export const test02ModelData: PatchBlock = {
63
- id: 'test02',
64
- previewName: 'Test 02',
65
- data: {
66
- name: 'patch'
67
- },
68
- chunks: {
69
- paragraph: {
70
- data: {
71
- descriptionValue: 'Patch paragraph value'
72
- }
73
- }
74
- }
75
- }
76
-
77
-
78
-
79
- export const chunkTestModel00: PatchBlock = {
80
- id: 'tst01',
81
- previewName: 'qweq',
82
- chunks: {
83
- test: {
84
- data: [{value: 'patched01'}, {value: 'patched02'}]
85
- }
86
- }
87
- }
88
-
89
-
90
- export const chunksTest01: {[key: string]: Chunk} = {
91
- test: {
92
- template: {
93
- markup: '<div>{{value}}</div>'
94
- },
95
- data: {
96
- type: 'DICT',
97
- definitions: {
98
- value: {
99
- type: "String",
100
- defaultValue: '__test'
101
- }
102
- }
103
- }
104
- }
105
- };
106
- export const chunkTestModel01: PatchBlock = {
107
- id: 'tst01',
108
- previewName: 'qweq',
109
- chunks: {
110
- test: {
111
- data: {
112
- value: 'patched'
113
- }
114
- }
115
- }
116
- }
117
-
118
- export const chunksTest02: {[key: string]: Chunk} = {
119
- test: {
120
- template: {
121
- markup: '<div>{{value}}</div>'
122
- },
123
- data: {
124
- type: 'LIST',
125
- definitions: {
126
- value: {
127
- type: "String",
128
- defaultValue: '__test'
129
- }
130
- }
131
- }
132
- }
133
- };
134
- export const chunkTestModel02: PatchBlock = {
135
- id: 'tst01',
136
- previewName: 'qweq',
137
- chunks: {
138
- test: {
139
- data: [{value: 'patched01'}, {value: 'patched02'}]
140
- }
141
- }
142
- }
143
-
144
- export const chunksTest03: {[key: string]: Chunk} = {
145
- test: {
146
- template: {
147
- markup: '<div>{{value}}</div>'
148
- },
149
- data: {
150
- type: 'LIST',
151
- definitions: {
152
- value: {
153
- type: "String",
154
- defaultValue: '__test'
155
- }
156
- }
157
- },
158
- styles: `div{color: red};div.test{color: blue};`
159
- }
160
- };
161
- export const chunkTestModel03: PatchBlock = {
162
- id: 'tst01',
163
- previewName: 'qweq',
164
- chunks: {
165
- test: {
166
- data: [{value: 'patched01'}, {value: 'patched02'}]
167
- }
168
- }
169
- }
170
-
171
- export const chunksTest04: {[key: string]: Chunk} = {
172
- test: {
173
- template: {
174
- markup: '<div>{{value}}</div>'
175
- },
176
- data: {
177
- type: 'LIST',
178
- definitions: {
179
- value: {
180
- type: "String",
181
- defaultValue: '__test'
182
- }
183
- }
184
- },
185
- styles: `div{color: red};div.test{color: blue};`
186
- },
187
- label: {
188
- template: {
189
- markup: `<label>{{label}}</label>`
190
- },
191
- data: {
192
- type: 'DICT',
193
- definitions: {
194
- label: {
195
- type: 'String',
196
- defaultValue: 'label'
197
- }
198
- }
199
- },
200
- styles: `label{color: red};`
201
- }
202
- };
203
- export const chunkTestModel04: PatchBlock = {
204
- id: 'tst01',
205
- previewName: 'qweq',
206
- chunks: {
207
- test: {
208
- data: [{value: 'patched01'}, {value: 'patched02'}]
209
- },
210
- label: {
211
- data: {label: '_label_'}
212
- }
213
- }
214
- }
215
-
216
- export const chunksTest05: {[key: string]: Chunk} = {
217
- test: {
218
- template: {
219
- markup: '<div>{{value}}</div>'
220
- },
221
- data: {
222
- type: 'LIST',
223
- definitions: {
224
- value: {
225
- type: "String",
226
- defaultValue: '__test'
227
- }
228
- }
229
- },
230
- styles: `div{color: red};div.test{color: blue};`
231
- },
232
- label: {
233
- template: {
234
- markup: `<label>{{label}}</label>`
235
- },
236
- data: {
237
- type: 'DICT',
238
- definitions: {
239
- label: {
240
- type: 'String',
241
- defaultValue: 'label'
242
- }
243
- }
244
- },
245
- styles: `label{color: red};`
246
- }
247
- };
248
- export const chunkTestModel05: PatchBlock = {
249
- id: 'tst01',
250
- previewName: 'qweq',
251
- chunks: {
252
- test: {
253
- data: [{value: 'patched01'}, {value: 'patched02'}],
254
- styles: '.modelStylesTEST_PROP{color: red};'
255
- },
256
- label: {
257
- data: {label: '_label_'},
258
- styles: '.modelStylesLABEL_PROP{color: green};'
259
- }
260
- }
261
- }
@@ -1,26 +0,0 @@
1
- import {Template} from "./template";
2
- import {Chunk, PatchChunk} from "./chunk";
3
- import {Data, DataDefinition, PossibleDataPayload} from "./data";
4
-
5
- export type BlockId = string;
6
- export type BlockPreview = string;
7
-
8
- export interface BaseBlock<T, D> {
9
- id: BlockId;
10
- previewName: BlockPreview;
11
- customName?: BlockPreview;
12
- chunks?: {[key: string]: T};
13
- data?: D,
14
- styles?: string;
15
- }
16
-
17
- export interface Block extends BaseBlock<Chunk, DataDefinition> {
18
- template: Template;
19
- }
20
-
21
- export interface PatchBlock extends BaseBlock<PatchChunk, Data> {}
22
-
23
- export interface CreateBlockOutput {
24
- markup: string;
25
- styles: string
26
- }
@@ -1,15 +0,0 @@
1
- import {Template} from "./template";
2
- import {DataDefinition, PossibleDataPayload} from "./data";
3
-
4
- export interface BaseChunk {
5
- styles?: string;
6
- }
7
-
8
- export interface Chunk extends BaseChunk {
9
- template: Template;
10
- data?: DataDefinition
11
- }
12
-
13
- export interface PatchChunk extends BaseChunk {
14
- data?: PossibleDataPayload;
15
- }
package/src/types/data.ts DELETED
@@ -1,57 +0,0 @@
1
- import {DATA_TYPES, DEFINITION_TYPES} from "../constants";
2
-
3
- export type Data<T = unknown> = {[key: string]: T};
4
-
5
- export type DefinitionType = keyof typeof DEFINITION_TYPES;
6
-
7
- export type DataType = keyof typeof DATA_TYPES;
8
-
9
-
10
- export interface ItemDefinition {
11
- type: DefinitionType;
12
- defaultValue: string;
13
- possibleVariants?: Array<unknown>;
14
- }
15
-
16
- export interface DataDefinition {
17
- type: DataType;
18
- defaultValue?: unknown;
19
- definitions: Data<ItemDefinition>;
20
- };
21
-
22
- export type PossibleDataPayload<T = any> = Data<T> | Array<Data<T>>;
23
-
24
- export interface Meta {
25
- name?: string;
26
- property?: string;
27
- content: string;
28
- }
29
-
30
- export interface PageBody {
31
- id: string;
32
- path: string;
33
- chunks: {
34
- [key: string]: {data: {[key: string]: unknown}
35
- }
36
- }
37
- }
38
-
39
- export interface Page {
40
- id: string;
41
- head: {
42
- title: string;
43
- meta: Array<Meta>;
44
- styles: Array<string>;
45
- scripts: Array<string>;
46
- includes: {
47
- styles: Array<string>;
48
- scripts: Array<string>;
49
- }
50
- },
51
- body: Array<PageBody>
52
- }
53
-
54
- export interface SiteSourceData {
55
- robots?: string;
56
- pages: Array<Page>
57
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "files": ["src/index.ts"],
3
- "compilerOptions": {
4
- "outDir": "./dist/",
5
- "noImplicitAny": true,
6
- "sourceMap": true,
7
- "module": "es2015",
8
- "target": "es2015",
9
- "allowJs": true,
10
- "moduleResolution": "node",
11
- "declaration": true,
12
- "esModuleInterop": true,
13
- "lib": [
14
- "es2015.promise"
15
- ]
16
- }
17
- }