@gem-sdk/system 1.56.2
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/dist/cjs/component/createAttr.js +34 -0
- package/dist/cjs/component/createClass.js +38 -0
- package/dist/cjs/component/createContent.js +19 -0
- package/dist/cjs/component/createStateOrContext.js +55 -0
- package/dist/cjs/component/createStyle.js +42 -0
- package/dist/cjs/component/utils/toCamelCaseKeys.js +15 -0
- package/dist/cjs/index.js +21 -0
- package/dist/esm/component/createAttr.js +32 -0
- package/dist/esm/component/createClass.js +36 -0
- package/dist/esm/component/createContent.js +17 -0
- package/dist/esm/component/createStateOrContext.js +53 -0
- package/dist/esm/component/createStyle.js +39 -0
- package/dist/esm/component/utils/toCamelCaseKeys.js +13 -0
- package/dist/esm/index.js +11 -0
- package/dist/types/index.d.ts +42 -0
- package/package.json +27 -0
- package/src/component/__tests__/ template.test.tsx +76 -0
- package/src/component/__tests__/createAttr.test.ts +62 -0
- package/src/component/__tests__/createClass.test.ts +68 -0
- package/src/component/__tests__/createContent.test.ts +52 -0
- package/src/component/__tests__/createStateOrContext.test.ts +129 -0
- package/src/component/__tests__/createStyle.test.ts +63 -0
- package/src/component/createAttr.ts +44 -0
- package/src/component/createClass.ts +48 -0
- package/src/component/createContent.ts +20 -0
- package/src/component/createStateOrContext.ts +70 -0
- package/src/component/createStyle.ts +53 -0
- package/src/component/template.ts +119 -0
- package/src/component/types.ts +9 -0
- package/src/component/utils/__tests__/toCamelCaseKeys.test.ts +79 -0
- package/src/component/utils/toCamelCaseKeys.ts +20 -0
- package/src/e2e-tests/README.md +1 -0
- package/src/examples/components/text/DemoText.liquid.ts +49 -0
- package/src/examples/components/text/DemoText.tsx +50 -0
- package/src/examples/components/text/common/__tests__/globalTypoClasses.test.ts +11 -0
- package/src/examples/components/text/common/getAttr.ts +7 -0
- package/src/examples/components/text/common/getStyle.ts +5 -0
- package/src/examples/components/text/common/globalTypoClasses.ts +5 -0
- package/src/examples/components/text/e2e-tests/DemoText.spec.tsx +23 -0
- package/src/examples/components/text/e2e-tests/DemoText.tsx +23 -0
- package/src/index.ts +34 -0
- package/src/validator/README.md +1 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, test, expect, beforeAll, afterEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
// Import the function
|
|
4
|
+
import { createClass } from '../createClass';
|
|
5
|
+
|
|
6
|
+
// Declare console.error as a Jest mock
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
global.console.error = jest.fn();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
// Clear the console.error mock after each test
|
|
13
|
+
(console.error as jest.Mock).mockClear();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('createClass', () => {
|
|
17
|
+
test('should return empty string and log error for invalid input', () => {
|
|
18
|
+
expect(createClass(null as any)).toBe('');
|
|
19
|
+
expect(console.error).toHaveBeenCalledWith('Expected an object as input.');
|
|
20
|
+
|
|
21
|
+
expect(createClass(123 as any)).toBe('');
|
|
22
|
+
expect(console.error).toHaveBeenCalledWith('Expected an object as input.');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should log an error for class names exceeding 30 characters', () => {
|
|
26
|
+
createClass({ averyverylongclassnameexceeding30characters: true });
|
|
27
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
28
|
+
'Class name "averyverylongclassnameexceeding30characters" exceeds the maximum length of 30 characters.',
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('should log an error for class names with spaces', () => {
|
|
33
|
+
createClass({ 'class with space': true });
|
|
34
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
35
|
+
'Class name "class with space" should not contain spaces.',
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('should log an error for class names with uppercase letters', () => {
|
|
40
|
+
createClass({ ClassWithUpperCase: true });
|
|
41
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
42
|
+
'Class name "ClassWithUpperCase" should be in lowercase.',
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('should not log any errors for valid class names', () => {
|
|
47
|
+
createClass({ validclassname: true, anotherclass: true });
|
|
48
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('should handle multiple errors in one call', () => {
|
|
52
|
+
createClass({
|
|
53
|
+
ValidClassWithUpper: true,
|
|
54
|
+
'class with space': true,
|
|
55
|
+
averyverylongclassnameexceeding30characters: true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
59
|
+
'Class name "ValidClassWithUpper" should be in lowercase.',
|
|
60
|
+
);
|
|
61
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
62
|
+
'Class name "class with space" should not contain spaces.',
|
|
63
|
+
);
|
|
64
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
65
|
+
'Class name "averyverylongclassnameexceeding30characters" exceeds the maximum length of 30 characters.',
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import { createContent } from '../createContent';
|
|
4
|
+
|
|
5
|
+
// Mock console.error to capture error messages
|
|
6
|
+
global.console.error = jest.fn();
|
|
7
|
+
|
|
8
|
+
describe('createContent', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// Clear the console.error mock before each test
|
|
11
|
+
(console.error as jest.Mock).mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should return empty string and log error if content is not a string', () => {
|
|
15
|
+
expect(createContent(12345 as any)).toBe('');
|
|
16
|
+
expect(console.error).toHaveBeenCalledWith('Invalid content type: Content must be a string.');
|
|
17
|
+
|
|
18
|
+
expect(createContent({} as any)).toBe('');
|
|
19
|
+
expect(console.error).toHaveBeenCalledWith('Invalid content type: Content must be a string.');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('should return empty string and log error for content with invalid {{}} pattern', () => {
|
|
23
|
+
expect(createContent('{{Hello}} there')).toBe('');
|
|
24
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
25
|
+
'Invalid content format: "{{}}" placeholders must not contain only letters, e.g., "{{Hello}}".',
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(createContent('Welcome {{World}}')).toBe('');
|
|
29
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
30
|
+
'Invalid content format: "{{}}" placeholders must not contain only letters, e.g., "{{Hello}}".',
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should return valid content without errors', () => {
|
|
35
|
+
expect(createContent('This is valid content')).toBe('This is valid content');
|
|
36
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
37
|
+
|
|
38
|
+
expect(createContent('Content with {{123}} is valid')).toBe('Content with {{123}} is valid');
|
|
39
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
40
|
+
|
|
41
|
+
expect(createContent('Mix of {{valid1}} and {{valid2}}')).toBe(
|
|
42
|
+
'Mix of {{valid1}} and {{valid2}}',
|
|
43
|
+
);
|
|
44
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('should pass with valid {{}} pattern containing more than just letters', () => {
|
|
48
|
+
const content = '{{ product.title | t }}';
|
|
49
|
+
expect(createContent(content)).toBe(content);
|
|
50
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import { createStateOrContext } from '../createStateOrContext';
|
|
4
|
+
|
|
5
|
+
// Mock console.error to capture error messages
|
|
6
|
+
global.console.error = jest.fn();
|
|
7
|
+
|
|
8
|
+
describe('createStateOrContext', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// Clear the console.error mock before each test
|
|
11
|
+
(console.error as jest.Mock).mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should log error for key length exceeding 20 characters', () => {
|
|
15
|
+
createStateOrContext({ thisKeyIsWayTooLongForOurRequirements: 'value' });
|
|
16
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
17
|
+
'Invalid key "thisKeyIsWayTooLongForOurRequirements": Key length must not exceed 20 characters.',
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should log error for key containing special characters', () => {
|
|
22
|
+
createStateOrContext({ invalid_key: 'value' });
|
|
23
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
24
|
+
'Invalid key "invalid_key": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should log error for key containing numbers', () => {
|
|
29
|
+
createStateOrContext({ invalidKey1: 'value' });
|
|
30
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
31
|
+
'Invalid key "invalidKey1": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('should log error for key not in camelCase format', () => {
|
|
36
|
+
createStateOrContext({ Invalidkey: 'value' });
|
|
37
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
38
|
+
'Invalid key "Invalidkey": Key must be in camelCase format.',
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('should log error for nested object deeper than 3 levels', () => {
|
|
43
|
+
createStateOrContext({
|
|
44
|
+
levelOne: {
|
|
45
|
+
levelTwo: {
|
|
46
|
+
levelThree: {
|
|
47
|
+
tooDeepKey: 'too deep',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
53
|
+
'Invalid structure: Data must not be nested deeper than 3 levels.',
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should log error for invalid values (undefined, null, blank, false)', () => {
|
|
58
|
+
createStateOrContext({
|
|
59
|
+
validKey: 'value', // Valid
|
|
60
|
+
undefinedKey: undefined, // Invalid
|
|
61
|
+
nullKey: null, // Invalid
|
|
62
|
+
emptyStringKey: '', // Invalid
|
|
63
|
+
falseKey: false, // Invalid
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
67
|
+
'Invalid value for key "undefinedKey": Value must not be undefined, null, blank, or false.',
|
|
68
|
+
);
|
|
69
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
70
|
+
'Invalid value for key "nullKey": Value must not be undefined, null, blank, or false.',
|
|
71
|
+
);
|
|
72
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
73
|
+
'Invalid value for key "emptyStringKey": Value must not be undefined, null, blank, or false.',
|
|
74
|
+
);
|
|
75
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
76
|
+
'Invalid value for key "falseKey": Value must not be undefined, null, blank, or false.',
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('should not log any errors for valid keys and values', () => {
|
|
81
|
+
createStateOrContext({
|
|
82
|
+
validKey: 'value',
|
|
83
|
+
anotherValidKey: 123,
|
|
84
|
+
nestedObject: {
|
|
85
|
+
validNestedKey: 'nestedValue',
|
|
86
|
+
anotherLevel: {
|
|
87
|
+
validKey: 'value',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('should handle multiple errors in one call', () => {
|
|
95
|
+
createStateOrContext({
|
|
96
|
+
thisKeyIsWayTooLongForOurRequirements: 'value', // Key length
|
|
97
|
+
invalid_key: 'value', // Special character
|
|
98
|
+
invalidKey1: 'value', // Contains number
|
|
99
|
+
InvalidCamelCase: 'value', // Not camelCase
|
|
100
|
+
validKey: {
|
|
101
|
+
nestedTooDeep: {
|
|
102
|
+
anotherLevel: {
|
|
103
|
+
tooDeepKey: 'too deep', // Exceeds depth
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
undefinedValue: undefined, // Invalid value
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
111
|
+
'Invalid key "thisKeyIsWayTooLongForOurRequirements": Key length must not exceed 20 characters.',
|
|
112
|
+
);
|
|
113
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
114
|
+
'Invalid key "invalid_key": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
115
|
+
);
|
|
116
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
117
|
+
'Invalid key "invalidKey1": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
118
|
+
);
|
|
119
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
120
|
+
'Invalid key "InvalidCamelCase": Key must be in camelCase format.',
|
|
121
|
+
);
|
|
122
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
123
|
+
'Invalid structure: Data must not be nested deeper than 3 levels.',
|
|
124
|
+
);
|
|
125
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
126
|
+
'Invalid value for key "undefinedValue": Value must not be undefined, null, blank, or false.',
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
// Import the function
|
|
4
|
+
import { createStyle } from '../createStyle';
|
|
5
|
+
|
|
6
|
+
global.console.error = jest.fn();
|
|
7
|
+
|
|
8
|
+
describe('createStyle', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// Clear the mock for console.error
|
|
11
|
+
(console.error as jest.Mock).mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should log error for ignored property', () => {
|
|
15
|
+
createStyle({ ignore: 'some value' });
|
|
16
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
17
|
+
'Ignored property detected: "ignore". This property is not supported in the current styling setup.',
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should log error for invalid property type', () => {
|
|
22
|
+
createStyle({ 'valid-property': true as any });
|
|
23
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
24
|
+
'Invalid style value for "valid-property": true. Must be a string or number.',
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should not log any errors for valid keys and values', () => {
|
|
29
|
+
createStyle({
|
|
30
|
+
'background-color': 'blue',
|
|
31
|
+
padding: 10,
|
|
32
|
+
});
|
|
33
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('should log error for key containing uppercase letters', () => {
|
|
37
|
+
createStyle({ fontSize: '14px' });
|
|
38
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
39
|
+
'Invalid key "fontSize": Keys must be lowercase letters and may only contain "-".',
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('should log error for key containing numbers', () => {
|
|
44
|
+
createStyle({ border1: 'solid' });
|
|
45
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
46
|
+
'Invalid key "border1": Keys must be lowercase letters and may only contain "-".',
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('should log error for key containing special characters other than "-"', () => {
|
|
51
|
+
createStyle({ invalid_key: 'value' });
|
|
52
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
53
|
+
'Invalid key "invalid_key": Keys must be lowercase letters and may only contain "-".',
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should log error for nested objects', () => {
|
|
58
|
+
createStyle({ nestedProp: { width: '100px' } } as any);
|
|
59
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
60
|
+
'Invalid nested object for property "nestedProp". Nested objects are not supported.',
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const createAttr = (obj: { [key: string]: string | number }) => {
|
|
2
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
3
|
+
console.error('Expected an object as input.');
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const isDevOrStaging =
|
|
8
|
+
!process.env.APP_ENV ||
|
|
9
|
+
process.env.APP_ENV === 'development' ||
|
|
10
|
+
process.env.APP_ENV === 'staging';
|
|
11
|
+
|
|
12
|
+
if (isDevOrStaging) {
|
|
13
|
+
for (const key in obj) {
|
|
14
|
+
const value = obj[key];
|
|
15
|
+
|
|
16
|
+
// 1. Check if the key starts with "data-gp-"
|
|
17
|
+
if (!key.startsWith('data-gp-')) {
|
|
18
|
+
console.error(`Invalid attribute key: "${key}". Must start with "data-gp-".`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 2. Check if the key contains uppercase letters
|
|
22
|
+
if (key !== key.toLowerCase()) {
|
|
23
|
+
console.error(`Invalid attribute key: "${key}". Must not contain uppercase letters.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 3. Check if the value is an object (nested object check)
|
|
27
|
+
if (typeof value === 'object' && value !== null) {
|
|
28
|
+
console.error(
|
|
29
|
+
`Invalid nested attribute for key "${key}". Nested objects are not supported.`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 4. Check if the value is a valid type (string or number)
|
|
34
|
+
const isValidType = typeof value === 'string' || typeof value === 'number';
|
|
35
|
+
if (!isValidType) {
|
|
36
|
+
console.error(
|
|
37
|
+
`Invalid attribute value for key "${key}": ${value}. Must be a string or number.`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return obj;
|
|
44
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
type ClassValue = Record<string, boolean | undefined | null> | string | null | undefined;
|
|
2
|
+
|
|
3
|
+
function toVal(mix: ClassValue) {
|
|
4
|
+
if (typeof mix === 'string') {
|
|
5
|
+
return mix;
|
|
6
|
+
} else if (typeof mix === 'object' && mix !== null) {
|
|
7
|
+
return Object.keys(mix)
|
|
8
|
+
.filter((key) => mix[key])
|
|
9
|
+
.join(' ');
|
|
10
|
+
} else {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function cls(...classes: ClassValue[]) {
|
|
16
|
+
return classes.map(toVal).filter(Boolean).join(' ');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const createClass = (obj: { [key: string]: boolean | undefined }): string => {
|
|
20
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
21
|
+
console.error('Expected an object as input.');
|
|
22
|
+
return '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const isDevOrStaging =
|
|
26
|
+
!process.env.APP_ENV ||
|
|
27
|
+
process.env.APP_ENV === 'development' ||
|
|
28
|
+
process.env.APP_ENV === 'staging';
|
|
29
|
+
|
|
30
|
+
if (isDevOrStaging) {
|
|
31
|
+
// Validate each class name length
|
|
32
|
+
for (const className in obj) {
|
|
33
|
+
if (className.length > 30) {
|
|
34
|
+
console.error(`Class name "${className}" exceeds the maximum length of 30 characters.`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (className.includes(' ')) {
|
|
38
|
+
console.error(`Class name "${className}" should not contain spaces.`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (className !== className.toLowerCase()) {
|
|
42
|
+
console.error(`Class name "${className}" should be in lowercase.`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return cls(obj);
|
|
48
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const createContent = (content: string): string => {
|
|
2
|
+
// Check if content is a string
|
|
3
|
+
if (typeof content !== 'string') {
|
|
4
|
+
console.error('Invalid content type: Content must be a string.');
|
|
5
|
+
return '';
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Regex to match {{}} pattern with only letters inside, e.g., {{Hello}}
|
|
9
|
+
const invalidPattern = /\{\{[A-Za-z]+\}\}/g;
|
|
10
|
+
|
|
11
|
+
// Check if content contains any invalid patterns
|
|
12
|
+
if (invalidPattern.test(content)) {
|
|
13
|
+
console.error(
|
|
14
|
+
'Invalid content format: "{{}}" placeholders must not contain only letters, e.g., "{{Hello}}".',
|
|
15
|
+
);
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return content;
|
|
20
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export const createStateOrContext = (obj: { [key: string]: any }) => {
|
|
2
|
+
const isDevOrStaging =
|
|
3
|
+
!process.env.APP_ENV ||
|
|
4
|
+
process.env.APP_ENV === 'development' ||
|
|
5
|
+
process.env.APP_ENV === 'staging';
|
|
6
|
+
|
|
7
|
+
const isValid = (value: any) => {
|
|
8
|
+
// Ensure value is neither undefined, null, empty string, nor false (except 0)
|
|
9
|
+
return value !== undefined && value !== null && value !== '' && value !== false;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const validateKey = (key: string) => {
|
|
13
|
+
// Check key length
|
|
14
|
+
if (key.length > 20) {
|
|
15
|
+
console.error(`Invalid key "${key}": Key length must not exceed 20 characters.`);
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Ensure no special characters or numbers
|
|
20
|
+
const validKeyRegex = /^[a-zA-Z]+$/;
|
|
21
|
+
if (!validKeyRegex.test(key)) {
|
|
22
|
+
console.error(
|
|
23
|
+
`Invalid key "${key}": Key must contain only alphabetic characters (no numbers or special characters).`,
|
|
24
|
+
);
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Check camelCase format (should start with lowercase)
|
|
29
|
+
const camelCaseRegex = /^[a-z][a-zA-Z]*$/;
|
|
30
|
+
if (!camelCaseRegex.test(key)) {
|
|
31
|
+
console.error(`Invalid key "${key}": Key must be in camelCase format.`);
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return true;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const validateObject = (data: any, depth: number) => {
|
|
39
|
+
if (depth > 3) {
|
|
40
|
+
console.error('Invalid structure: Data must not be nested deeper than 3 levels.');
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
for (const key in data) {
|
|
45
|
+
const value = data[key];
|
|
46
|
+
|
|
47
|
+
// Key validation
|
|
48
|
+
if (!validateKey(key)) continue;
|
|
49
|
+
|
|
50
|
+
// Value validation
|
|
51
|
+
if (!isValid(value)) {
|
|
52
|
+
console.error(
|
|
53
|
+
`Invalid value for key "${key}": Value must not be undefined, null, blank, or false.`,
|
|
54
|
+
);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Recursive check if the value is an object
|
|
59
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
60
|
+
validateObject(value, depth + 1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (isDevOrStaging) {
|
|
66
|
+
validateObject(obj, 1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return obj;
|
|
70
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { toCamelCaseKeys } from './utils/toCamelCaseKeys';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Properties to ignore with explanations for each
|
|
5
|
+
*/
|
|
6
|
+
const ignoredProperties: { [key: string]: string } = {
|
|
7
|
+
ignore: 'This property is not supported in the current styling setup.',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const createStyle = (obj: { [key: string]: string | number }) => {
|
|
11
|
+
const isDevOrStaging =
|
|
12
|
+
!process.env.APP_ENV ||
|
|
13
|
+
process.env.APP_ENV === 'development' ||
|
|
14
|
+
process.env.APP_ENV === 'staging';
|
|
15
|
+
|
|
16
|
+
if (isDevOrStaging) {
|
|
17
|
+
for (const key in obj) {
|
|
18
|
+
const value = obj[key];
|
|
19
|
+
|
|
20
|
+
// Check if the property is in the ignored list and log the explanation
|
|
21
|
+
if (Object.prototype.hasOwnProperty.call(ignoredProperties, key)) {
|
|
22
|
+
console.error(`Ignored property detected: "${key}". ${ignoredProperties[key]}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Check for uppercase letters, numbers, and special characters (only allow lowercase letters and "-"
|
|
26
|
+
const isValidKey = /^[a-z-]+$/.test(key);
|
|
27
|
+
if (!isValidKey) {
|
|
28
|
+
console.error(
|
|
29
|
+
`Invalid key "${key}": Keys must be lowercase letters and may only contain "-".`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check for nested objects (only support single-level properties)
|
|
34
|
+
if (typeof value === 'object' && value !== null) {
|
|
35
|
+
console.error(
|
|
36
|
+
`Invalid nested object for property "${key}". Nested objects are not supported.`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check if the value is a valid type
|
|
41
|
+
const isValidType = typeof value === 'string' || typeof value === 'number';
|
|
42
|
+
if (!isValidType) {
|
|
43
|
+
console.error(`Invalid style value for "${key}": ${value}. Must be a string or number.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return obj;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const createStyleReact = (obj: { [key: string]: string | number }) => {
|
|
52
|
+
return toCamelCaseKeys(createStyle(obj));
|
|
53
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
Liquid in liquid.ts
|
|
4
|
+
<div>
|
|
5
|
+
Liquid(`
|
|
6
|
+
{%-if productSelectedVariant == empty or productSelectedVariant == null -%}
|
|
7
|
+
{%- assign productSelectedVariant = product.selected_or_first_available_variant -%}
|
|
8
|
+
{%- endif -%}
|
|
9
|
+
{%-if variant == empty or variant == null -%}
|
|
10
|
+
{%- assign variant = product.selected_or_first_available_variant -%}
|
|
11
|
+
{%- endif -%}
|
|
12
|
+
`)
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
IF in tsx & liquid.ts
|
|
16
|
+
<div {...attrs}>
|
|
17
|
+
{
|
|
18
|
+
If(product.id != "", (
|
|
19
|
+
<label className={classes} style={styles}>
|
|
20
|
+
{content}
|
|
21
|
+
</label>
|
|
22
|
+
), (
|
|
23
|
+
<label className={classes} style={styles}>
|
|
24
|
+
{content}
|
|
25
|
+
</label>
|
|
26
|
+
))
|
|
27
|
+
}
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
LiquidIF in liquid.ts
|
|
31
|
+
<div {...attrs}>
|
|
32
|
+
{
|
|
33
|
+
LiquidIf("product.quanity > 0", `
|
|
34
|
+
<label className={classes} style={styles}>
|
|
35
|
+
{content}
|
|
36
|
+
</label>
|
|
37
|
+
`, `
|
|
38
|
+
<label className={classes} style={styles}>
|
|
39
|
+
{content}
|
|
40
|
+
</label>
|
|
41
|
+
`)
|
|
42
|
+
}
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
For in tsx & liquid.ts
|
|
46
|
+
{
|
|
47
|
+
For(numbers, (item, index) => (
|
|
48
|
+
<div key={index}>
|
|
49
|
+
{index + 1}: Số {item}
|
|
50
|
+
</div>
|
|
51
|
+
))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
LiquidFor in tsx & liquid.ts
|
|
55
|
+
{
|
|
56
|
+
LiquidFor('(item, index) in items', `
|
|
57
|
+
<div key="{{ forloop.index }}">
|
|
58
|
+
{{ forloop.index + 1}}: Số {{item}}
|
|
59
|
+
</div>
|
|
60
|
+
`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
*/
|
|
64
|
+
type CallbackCondition = () => JSX.Element | string;
|
|
65
|
+
|
|
66
|
+
export const Liquid = (code: string)=> {
|
|
67
|
+
return code
|
|
68
|
+
}
|
|
69
|
+
export const For = <T,>(
|
|
70
|
+
items: T[],
|
|
71
|
+
renderFn: (item: T, index: number) => JSX.Element
|
|
72
|
+
): JSX.Element[] => {
|
|
73
|
+
return items.map((item, index) => renderFn(item, index));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const LiquidFor = (
|
|
77
|
+
c: string,
|
|
78
|
+
t: string | CallbackCondition
|
|
79
|
+
): string => {
|
|
80
|
+
return `{% for ${c} %}${(typeof t === 'string' ? t : t())}{% endfor %}`
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const If = (
|
|
84
|
+
condition: boolean | null | undefined,
|
|
85
|
+
trueResult: string | JSX.Element | CallbackCondition,
|
|
86
|
+
falseResult?: string | JSX.Element | CallbackCondition
|
|
87
|
+
): JSX.Element | string | null => {
|
|
88
|
+
if (condition) {
|
|
89
|
+
// Trả về kết quả đúng nếu điều kiện là true
|
|
90
|
+
return typeof trueResult === 'function' ? trueResult() : trueResult;
|
|
91
|
+
} else {
|
|
92
|
+
// Trả về kết quả sai nếu điều kiện là false
|
|
93
|
+
return falseResult
|
|
94
|
+
? typeof falseResult === 'function'
|
|
95
|
+
? falseResult()
|
|
96
|
+
: falseResult
|
|
97
|
+
: null; // Nếu không có falseResult, trả về null
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
export const LiquidIf = (
|
|
103
|
+
c: string,
|
|
104
|
+
t: string | CallbackCondition,
|
|
105
|
+
f?: string | CallbackCondition,
|
|
106
|
+
) => `{% if ${c} %}${(typeof t === 'string' ? t : t())}${f ? `{% else %}${(typeof f === 'string' ? f : f?.())}` : ''}{% endif %}`;
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
export const Unless = (
|
|
110
|
+
condition: boolean | null | undefined,
|
|
111
|
+
trueResult: string | JSX.Element | CallbackCondition,
|
|
112
|
+
falseResult?: string | JSX.Element | CallbackCondition
|
|
113
|
+
) => If(!condition, trueResult, falseResult);
|
|
114
|
+
|
|
115
|
+
export const LiquidUnless = (
|
|
116
|
+
c: string,
|
|
117
|
+
t: string | CallbackCondition,
|
|
118
|
+
f?: string | CallbackCondition,
|
|
119
|
+
) => `{% unless ${c} %}${(typeof t === 'string' ? t : t())}${f ? `{% else %}${(typeof f === 'string' ? f : f?.())}` : ''}{% endunless %}`;
|