@gem-sdk/system 1.58.0-dev.143 → 1.58.0-dev.144

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 (32) hide show
  1. package/dist/cjs/component/template.js +89 -0
  2. package/dist/cjs/index.js +6 -0
  3. package/dist/esm/component/template.js +83 -0
  4. package/dist/esm/index.js +1 -0
  5. package/dist/types/index.d.ts +8 -1
  6. package/package.json +15 -6
  7. package/src/component/__tests__/ template.test.tsx +0 -76
  8. package/src/component/__tests__/createAttr.test.ts +0 -62
  9. package/src/component/__tests__/createClass.test.ts +0 -68
  10. package/src/component/__tests__/createContent.test.ts +0 -52
  11. package/src/component/__tests__/createStateOrContext.test.ts +0 -129
  12. package/src/component/__tests__/createStyle.test.ts +0 -63
  13. package/src/component/createAttr.ts +0 -44
  14. package/src/component/createClass.ts +0 -48
  15. package/src/component/createContent.ts +0 -20
  16. package/src/component/createStateOrContext.ts +0 -70
  17. package/src/component/createStyle.ts +0 -53
  18. package/src/component/template.ts +0 -119
  19. package/src/component/types.ts +0 -9
  20. package/src/component/utils/__tests__/toCamelCaseKeys.test.ts +0 -79
  21. package/src/component/utils/toCamelCaseKeys.ts +0 -20
  22. package/src/e2e-tests/README.md +0 -1
  23. package/src/examples/components/text/DemoText.liquid.ts +0 -49
  24. package/src/examples/components/text/DemoText.tsx +0 -50
  25. package/src/examples/components/text/common/__tests__/globalTypoClasses.test.ts +0 -11
  26. package/src/examples/components/text/common/getAttr.ts +0 -7
  27. package/src/examples/components/text/common/getStyle.ts +0 -5
  28. package/src/examples/components/text/common/globalTypoClasses.ts +0 -5
  29. package/src/examples/components/text/e2e-tests/DemoText.spec.tsx +0 -23
  30. package/src/examples/components/text/e2e-tests/DemoText.tsx +0 -23
  31. package/src/index.ts +0 -34
  32. package/src/validator/README.md +0 -1
@@ -1,63 +0,0 @@
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
- });
@@ -1,44 +0,0 @@
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
- };
@@ -1,48 +0,0 @@
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
- };
@@ -1,20 +0,0 @@
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
- };
@@ -1,70 +0,0 @@
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
- };
@@ -1,53 +0,0 @@
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
- };
@@ -1,119 +0,0 @@
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 %}`;
@@ -1,9 +0,0 @@
1
- export type E2ETestCase = {
2
- props: object;
3
- reactComponent: any;
4
- liquidComponent: any;
5
- };
6
-
7
- export type E2ETests = {
8
- [key: string]: () => Promise<E2ETestCase>;
9
- };
@@ -1,79 +0,0 @@
1
- import { describe, test, expect } from '@jest/globals';
2
-
3
- import { toCamelCaseKeys } from '../toCamelCaseKeys';
4
-
5
- describe('toCamelCaseKeys', () => {
6
- test('should convert hyphenated keys to camelCase', () => {
7
- const input = {
8
- 'background-color': 'blue',
9
- 'font-size': '12px',
10
- padding: 10,
11
- };
12
- const expectedOutput = {
13
- backgroundColor: 'blue',
14
- fontSize: '12px',
15
- padding: 10,
16
- };
17
- expect(toCamelCaseKeys(input)).toEqual(expectedOutput);
18
- });
19
-
20
- test('should keep keys with "--" prefix as-is', () => {
21
- const input = {
22
- '--custom-var': '20px',
23
- '--another-var': '15px',
24
- };
25
- const expectedOutput = {
26
- '--custom-var': '20px',
27
- '--another-var': '15px',
28
- };
29
- expect(toCamelCaseKeys(input)).toEqual(expectedOutput);
30
- });
31
-
32
- test('should convert hyphenated keys in nested objects to camelCase', () => {
33
- const input = {
34
- nested: {
35
- 'border-radius': '5px',
36
- 'text-align': 'center',
37
- },
38
- };
39
- const expectedOutput = {
40
- nested: {
41
- borderRadius: '5px',
42
- textAlign: 'center',
43
- },
44
- };
45
- expect(toCamelCaseKeys(input)).toEqual(expectedOutput);
46
- });
47
-
48
- test('should handle mixed keys with hyphens and "--" prefix correctly', () => {
49
- const input = {
50
- 'background-color': 'blue',
51
- '--custom-var': '20px',
52
- nested: {
53
- 'font-size': '12px',
54
- '--nested-var': '5px',
55
- },
56
- };
57
- const expectedOutput = {
58
- backgroundColor: 'blue',
59
- '--custom-var': '20px',
60
- nested: {
61
- fontSize: '12px',
62
- '--nested-var': '5px',
63
- },
64
- };
65
- expect(toCamelCaseKeys(input)).toEqual(expectedOutput);
66
- });
67
-
68
- test('should leave keys without hyphens or "--" prefix unchanged', () => {
69
- const input = {
70
- padding: 10,
71
- margin: '5px',
72
- };
73
- const expectedOutput = {
74
- padding: 10,
75
- margin: '5px',
76
- };
77
- expect(toCamelCaseKeys(input)).toEqual(expectedOutput);
78
- });
79
- });
@@ -1,20 +0,0 @@
1
- export const toCamelCaseKeys = (obj: { [key: string]: any }): { [key: string]: any } => {
2
- const newObj: { [key: string]: any } = {};
3
-
4
- for (const key in obj) {
5
- const value = obj[key];
6
-
7
- // If the key starts with "--", keep it as is
8
- const newKey = key.startsWith('--')
9
- ? key
10
- : key.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
11
-
12
- // Recursively apply to nested objects
13
- newObj[newKey] =
14
- typeof value === 'object' && value !== null && !Array.isArray(value)
15
- ? toCamelCaseKeys(value)
16
- : value;
17
- }
18
-
19
- return newObj;
20
- };
@@ -1 +0,0 @@
1
- Đây là folder chưa các e2e-tests compare react vs liquid
@@ -1,49 +0,0 @@
1
- import {
2
- createAttr,
3
- createClass,
4
- createContent,
5
- createStateOrContext,
6
- createStyle,
7
- If,
8
- } from '../../../index';
9
- import { getAttr } from './common/getAttr';
10
- import { getStyle } from './common/getStyle';
11
- import { globalTypoClasses } from './common/globalTypoClasses';
12
- import type { TextProps } from './DemoText';
13
- import { template } from '@gem-sdk/core';
14
-
15
- const Text = ({ product, text }: TextProps) => {
16
- const state = createStateOrContext({
17
- productId: product.id,
18
- });
19
-
20
- const attrs = createAttr({
21
- ...getAttr({ product }),
22
- });
23
- const styles = createStyle({
24
- ...getStyle(),
25
- });
26
- const classes = createClass({
27
- ...globalTypoClasses(),
28
- });
29
-
30
- const content = createContent(text);
31
-
32
- return template`
33
- <div ${attrs} gp-data="${state}">
34
- ${
35
- If(product.id != "", `
36
- <label class="${{ classes }}" style="${styles}">
37
- ${content}
38
- </label>
39
- `, `
40
- <label class="${{ classes }}" style="${styles}">
41
- ${content}
42
- </label>
43
- `)
44
- }
45
- </div>
46
- `;
47
- };
48
-
49
- export default Text;
@@ -1,50 +0,0 @@
1
- import {
2
- createAttrReact,
3
- createClassReact,
4
- createContentReact,
5
- createStyleReact,
6
- If,
7
- } from '../../../index';
8
- import { globalTypoClasses } from './common/globalTypoClasses';
9
-
10
- export type TextProps = {
11
- product: {
12
- id: string;
13
- };
14
- text: string;
15
- };
16
-
17
- const Text: React.FC<TextProps> = ({ product, text }) => {
18
- const attrs = createAttrReact({
19
- 'data-gp-product-id': product.id,
20
- });
21
- const styles = createStyleReact({
22
- 'font-size': '16px',
23
- });
24
-
25
- const typoClasses = globalTypoClasses();
26
- const classes = createClassReact({
27
- 'gp-text': true,
28
- ...typoClasses,
29
- });
30
-
31
- const content = createContentReact(text);
32
-
33
- return (
34
- <div {...attrs}>
35
- {
36
- If(product.id != "", (
37
- <label className={classes} style={styles}>
38
- {content}
39
- </label>
40
- ), (
41
- <label className={classes} style={styles}>
42
- {content}
43
- </label>
44
- ))
45
- }
46
- </div>
47
- );
48
- };
49
-
50
- export default Text;
@@ -1,11 +0,0 @@
1
- import { describe, test, expect } from '@jest/globals';
2
- import { globalTypoClasses } from '../globalTypoClasses';
3
-
4
- describe('globalTypoClasses', () => {
5
- test('Test case 1', () => {
6
- const classes = globalTypoClasses();
7
- expect(classes).toEqual({
8
- 'gp-global-h1': true,
9
- });
10
- });
11
- });
@@ -1,7 +0,0 @@
1
- import type { TextProps } from '../DemoText';
2
-
3
- export const getAttr = ({ product }: { product: TextProps['product'] }) => {
4
- return {
5
- 'data-gp-product-id': product.id,
6
- };
7
- };
@@ -1,5 +0,0 @@
1
- export const getStyle = () => {
2
- return {
3
- 'font-size': '16px',
4
- };
5
- };
@@ -1,5 +0,0 @@
1
- export const globalTypoClasses = (): Record<string, boolean> => {
2
- return {
3
- 'gp-global-h1': true,
4
- };
5
- };