Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regexs #75

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions specs/creators/rule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ describe('Create Rule', () => {
expect(rule(':pseudo-class')).to.have.property('selector', '&:pseudo-class');
});

it('create an object with args (selector: RegExp, [\\[[^\\[\\]]+\\]])', () => {
expect(rule('\\[[^\\[\\]]+\\]')).to.have.property(
'selector',
'&\\[[^\\[\\]]+\\]',
);
it('create an object with args (selector: RegExp, [?\\[(.*)\\]])', () => {
expect(rule('?\\[(.*)\\]')).to.have.property('selector', '&?\\[(.*)\\]');
});

it('create an object with args (selector: RegExp, [\\..+])', () => {
expect(rule('\\..+')).to.have.property('selector', '&\\..+');
it('create an object with args (selector: RegExp, [?\\.(.*)])', () => {
expect(rule('?\\.(.*)')).to.have.property('selector', '&?\\.(.*)');
});

it('create an object with args (selector: RegExp, [--.+])', () => {
expect(rule('--.+')).to.have.property('selector', '&--.+');
it('create an object with args (selector: RegExp, [--])', () => {
expect(rule('--')).to.have.property('selector', '&--');
});

it('create an object with args (selector: RegExp, [?.* &])', () => {
expect(rule('?.* &')).to.have.property('selector', '&?.* &');
it('create an object with args (selector: RegExp, [__])', () => {
expect(rule('__')).to.have.property('selector', '&__');
});
});
44 changes: 44 additions & 0 deletions specs/regex/bem-pattern.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,59 @@ describe('BEM Class Pattern', () => {
expect('block').to.match(BEMClassPattern);
});

it('block (String, [block-name])', () => {
expect('block-name').to.match(BEMClassPattern);
});

it('block with modifier (String, [block--modifier])', () => {
expect('block--modifier').to.match(BEMClassPattern);
});

it('block with modifier (String, [block-name--modifier-name])', () => {
expect('block-name--modifier-name').to.match(BEMClassPattern);
});

it('block with modifier (String, [block-name--modifier])', () => {
expect('block-name--modifier').to.match(BEMClassPattern);
});

it('block with modifier (String, [block--modifier-name])', () => {
expect('block--modifier-name').to.match(BEMClassPattern);
});

it('block and element (String, [block__element])', () => {
expect('block__element').to.match(BEMClassPattern);
});

it('block and element (String, [block-name__element-name])', () => {
expect('block-name__element-name').to.match(BEMClassPattern);
});

it('block and element (String, [block-name__element])', () => {
expect('block-name__element').to.match(BEMClassPattern);
});

it('block and element (String, [block__element-name])', () => {
expect('block__element-name').to.match(BEMClassPattern);
});

it('block and element with modifier (String, [block__element--modifier])', () => {
expect('block__element--modifier').to.match(BEMClassPattern);
});

it('block and element with modifier (String, [block-name__element-name--modifier-name])', () => {
expect('block-name__element-name--modifier-name').to.match(BEMClassPattern);
});

it('block and element with modifier (String, [block-name__element-name--modifier])', () => {
expect('block-name__element-name--modifier').to.match(BEMClassPattern);
});

it('block and element with modifier (String, [block-name__element--modifier-name])', () => {
expect('block-name__element--modifier-name').to.match(BEMClassPattern);
});

it('block and element with modifier (String, [block__element-name--modifier-name])', () => {
expect('block__element-name--modifier-name').to.match(BEMClassPattern);
});
});
98 changes: 96 additions & 2 deletions specs/regex/regex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,109 @@ describe('Nested Selectors Pattern', () => {
expect('&[attribute]').to.match(new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`));
});

it('nested (String, [&[attribute] {}])', () => {
expect('&[attribute] {}').to.match(new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`));
});

it('nested (String, [&[attribute=value]])', () => {
expect('&[attribute=value]').to.match(new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`));
});

it('nested (String, [&[attribute*=value]])', () => {
expect('&[attribute*=value]').to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&[attribute=value] {}])', () => {
expect('&[attribute=value] {}').to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&[attribute*=value] {}])', () => {
expect('&[attribute*=value] {}').to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&[attribute="value"]])', () => {
expect("&[attribute='value']").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&[attribute*="value"]])', () => {
expect("&[attribute*='value']").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&[attribute="value"] {}])', () => {
expect("&[attribute='value'] {}").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&[attribute*="value"] {}])', () => {
expect("&[attribute*='value'] {}").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it("nested (String, [&[attribute='value']])", () => {
expect("&[attribute='value']").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it("nested (String, [&[attribute*='value']])", () => {
expect("&[attribute*='value']").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it("nested (String, [&[attribute='value'] {}])", () => {
expect("&[attribute='value'] {}").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it("nested (String, [&[attribute*='value'] {}])", () => {
expect("&[attribute*='value'] {}").to.match(
new RegExp(`&${REGEXP.NESTED.ATTRIBUTE}`),
);
});

it('nested (String, [&.class])', () => {
expect('&.class').to.match(new RegExp(`&${REGEXP.NESTED.CLASS}`));
});

it('nested (String, [&.class {}])', () => {
expect('&.class {}').to.match(new RegExp(`&${REGEXP.NESTED.CLASS}`));
});

it('nested (String, [&--modifier])', () => {
expect('&--modifier').to.match(new RegExp(`&${REGEXP.NESTED.MODIFIER}`));
});

it('nested (String, [.class &])', () => {
expect('.class &').to.match(new RegExp(`&${REGEXP.NESTED.PARENT}`));
it('nested (String, [&--modifier {}])', () => {
expect('&--modifier {}').to.match(new RegExp(`&${REGEXP.NESTED.MODIFIER}`));
});

it('child (String, [[attribute] &])', () => {
expect('[attribute] &').to.match(new RegExp(`&${REGEXP.CHILD.ATTRIBUTE}`));
});

it('child (String, [[attribute] & {}])', () => {
expect('[attribute] & {}').to.match(new RegExp(`&${REGEXP.CHILD.ATTRIBUTE}`));
});

it('child (String, [.class &])', () => {
expect('.class &').to.match(new RegExp(`&${REGEXP.CHILD.CLASS}`));
});

it('child (String, [.class & {}])', () => {
expect('.class & {}').to.match(new RegExp(`&${REGEXP.CHILD.CLASS}`));
});
});
10 changes: 10 additions & 0 deletions src/creators/at-rule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* The `createAtRule` function in TypeScript creates an object representing
* an at-rule with a specified name and block presence.
* @param {string} name - The `name` parameter is a string that represents
* the name of the at-rule being created.
* @param {boolean} hasBlock - The `hasBlock` parameter in the `createAtRule`
* function is a boolean value that indicates whether the at-rule being created
* will have a block of nested rules or declarations inside it. If `hasBlock`
* is `true`, it means that the at-rule will have a block.
*/
const createAtRule = (name: string, hasBlock: boolean) => ({
name,
hasBlock,
Expand Down
11 changes: 11 additions & 0 deletions src/creators/logical-group.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* The function `createLogicalGroup` creates a logical group with specified
* properties and formatting rules.
* @param {string} groupName - The `groupName` parameter is a string that
* represents the name of the logical group being created.
* @param {any[]} properties - The `properties` parameter in the
* `createLogicalGroup` function is an array that contains the properties
* or items that you want to group together under a logical group
* with the specified `groupName`. These properties can be of any type or
* structure, depending on your specific use case.
*/
const createLogicalGroup = (groupName: string, properties: any[]) => ({
groupName,
properties,
Expand Down
7 changes: 7 additions & 0 deletions src/creators/rule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* The function `createRule` in TypeScript creates
* a CSS rule with a specified selector.
* @param {string} selector - The `selector` parameter in the `createRule`
* function is a string that
* represents the CSS selector for which a rule is being created.
*/
const createRule = (selector: string) => ({
selector: `&${selector}`,
type: 'rule',
Expand Down
4 changes: 3 additions & 1 deletion src/properties/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ const propertiesOrder = {
createRule(REGEXP.NESTED.ATTRIBUTE),
createRule(REGEXP.NESTED.CLASS),
createRule(REGEXP.NESTED.MODIFIER),
createRule(REGEXP.NESTED.PARENT),
createRule(REGEXP.NESTED.ELEMENT),
createRule(REGEXP.CHILD.ATTRIBUTE),
createRule(REGEXP.CHILD.CLASS),
'rules',
createAtRule('include', true),
'at-rules',
Expand Down
24 changes: 16 additions & 8 deletions src/utils/regexps.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const PATTERN = '[a-z0-9]';
// The '?' is used to define the '&' sign at the beginning,
// which is added in createRule()
const ATTRIBUTE = '?\\[(.*)\\]';
const CLASS = '?\\.(.*)';
const MODIFIER = '--';
const ELEMENT = '__';
const REGEXP = {
BEM: {
BLOCK: `[a-z]${PATTERN}*(-${PATTERN}+)`,
ELEMENT: `(__${PATTERN}+(-${PATTERN}+)*)`,
MODIFIER: `(--${PATTERN}+(-${PATTERN}+)*)`,
ELEMENT: `(${ELEMENT}${PATTERN}+(-${PATTERN}+)*)`,
MODIFIER: `(${MODIFIER}${PATTERN}+(-${PATTERN}+)*)`,
},
NESTED: {
ATTRIBUTE: '\\[[^\\[\\]]+\\]',
CLASS: '\\..+',
MODIFIER: '--.+',
// The '?' is used to define the '&' sign at the beginning,
// which is added in createRule()
PARENT: '?.* &',
ATTRIBUTE,
CLASS,
ELEMENT,
MODIFIER,
},
CHILD: {
ATTRIBUTE: `${ATTRIBUTE} &`,
CLASS: `${CLASS} &`,
},
};

Expand Down