Skip to content

Commit 690d2b1

Browse files
committed
♻️ refactor(plugin): refactor gitmoji regex into a new repo for reuse
1 parent 065a93f commit 690d2b1

File tree

12 files changed

+133
-10
lines changed

12 files changed

+133
-10
lines changed

packages/commitlint-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"dependencies": {
3434
"@commitlint/types": "^17",
35-
"emoji-regex": "^10",
35+
"@gitmoji/gitmoji-regex": "0.0.0",
3636
"gitmojis": "^3"
3737
},
3838
"publishConfig": {

packages/commitlint-plugin/src/rule.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import type { Rule } from '@commitlint/types';
2-
import emojiRegex from 'emoji-regex';
2+
import { emojiRegex, gitmojiUnicodeRegex, gitmojiCodeRegex } from '@gitmoji/gitmoji-regex';
33

44
import { gitmojiCodes, gitmojiUnicode } from './gitmojiCode';
55

6-
const gitjimojiCodeStr = `:\\w*:`;
7-
const gitmojiUnicodeStr = gitmojiUnicode.filter((i) => i).join('|');
8-
const emojiStr = emojiRegex().source;
9-
106
const emoji: Rule = (parsed) => {
117
const { raw } = parsed;
128

139
// code regex test url: https://regex101.com/r/fSdOvB/1
14-
const gitmojiCodeResult = new RegExp(`(${gitjimojiCodeStr})\\s.*`, 'gm').exec(raw);
10+
const gitmojiCodeResult = new RegExp(`(${gitmojiCodeRegex.source})\\s.*`, 'gm').exec(raw);
1511
// unicode regex test url: https://regex101.com/r/shBTBg/2
16-
const gitmojiUnicodeResult = new RegExp(`(${gitmojiUnicodeStr})\\s.*`, 'gm').exec(raw);
17-
const emojiResult = new RegExp(`(${emojiStr})\\s.*`, 'gm').exec(raw);
12+
const gitmojiUnicodeResult = new RegExp(`(${gitmojiUnicodeRegex.source})\\s.*`, 'gm').exec(raw);
13+
const emojiResult = new RegExp(`(${emojiRegex.source})\\s.*`, 'gm').exec(raw);
1814

1915
let pass;
2016
let errorMsg = 'passed';

packages/commitlint-plugin/test/rule.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('commit start with gitmoji code', () => {
6969
expect(value).toEqual([true, 'passed']);
7070
});
7171

72-
it('every emoji in list past', () => {
72+
it('every emoji in gitmoji list should pass', () => {
7373
const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => gitmoji.emoji);
7474

7575
gitmojiUnicode.forEach((unicode) => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const base = require('../../.changelogrc');
2+
3+
module.exports = {
4+
...base,
5+
displayScopes: ['gitmoji-regex'],
6+
};

packages/gitmoji-regex/.fatherrc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import config from '../../.fatherrc';
2+
3+
export default config;

packages/gitmoji-regex/CHANGELOG.md

Whitespace-only changes.

packages/gitmoji-regex/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# @gitmoji/gitmoji-regex
2+
3+
[![NPM version][version-image]][version-url] [![NPM downloads][download-image]][download-url]
4+
5+
a gitmoji regex to for both gitmoji code and gitmoji unicode
6+
7+
this package is used in both [@gitmoji/parser-opts](../parser-opts) and [commitlint-plugin-gitmoji](../commitlint-plugin)
8+
9+
## emojiRegex
10+
11+
## gitmojiCodeRegex
12+
13+
## gitmojiUnicodeRegex
14+
15+
Header regex pattern test here : [Regex101](https://regex101.com/r/gYkG99/1)
16+
17+
```js
18+
module.exports = {
19+
headerPattern:
20+
/^(?::\w*:|(?:\ud83c[\udf00-\udfff])|(?:\ud83d[\udc00-\ude4f\ude80-\udeff])|[\u2600-\u2B55])\s(?<type>\w*)(?:\((?<scope>.*)\))?!?:\s(?<subject>(?:(?!#).)*(?:(?!\s).))(?:\s\(?(?<ticket>#\d*)\)?)?$/,
21+
headerCorrespondence: ['type', 'scope', 'subject', 'ticket'],
22+
};
23+
```
24+
25+
## License
26+
27+
[MIT](../../LICENSE) ® Arvin Xu
28+
29+
<!-- npm url -->
30+
31+
[version-image]: http://img.shields.io/npm/v/@gitmoji/gitmoji-regex.svg?color=deepgreen&label=latest
32+
[version-url]: http://npmjs.org/package/@gitmoji/gitmoji-regex
33+
[download-image]: https://img.shields.io/npm/dm/@gitmoji/gitmoji-regex.svg
34+
[download-url]: https://npmjs.org/package/@gitmoji/gitmoji-regex

packages/gitmoji-regex/jest.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import base from '../../jest.config.base';
2+
import { Config } from '@umijs/test';
3+
4+
const packageName = '@gitmoji/gitmoji-regex';
5+
6+
const root = '<rootDir>/packages/gitmoji-regex';
7+
8+
const config: Config.InitialOptions = {
9+
...base,
10+
rootDir: '../..',
11+
roots: [root],
12+
displayName: packageName,
13+
};
14+
15+
export default config;

packages/gitmoji-regex/package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@gitmoji/gitmoji-regex",
3+
"version": "0.0.0",
4+
"description": "a gitmoji regex to for both gitmoji code and gitmoji unicode",
5+
"homepage": "https://github.com/arvinxx/gitmoji-commit-workflow/tree/master/packages/gitmoji-regex",
6+
"bugs": {
7+
"url": "https://github.com/arvinxx/gitmoji-commit-workflow/issues"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/arvinxx/gitmoji-commit-workflow.git"
12+
},
13+
"license": "ISC",
14+
"author": "arvinxx <[email protected]>",
15+
"main": "lib/index.js",
16+
"files": [
17+
"lib"
18+
],
19+
"scripts": {
20+
"build": "father build",
21+
"clean": "rm -rf es lib dist build coverage .eslintcache",
22+
"cov": "jest --coverage",
23+
"test": "jest"
24+
},
25+
"dependencies": {
26+
"emoji-regex": "^10",
27+
"gitmojis": "^3"
28+
},
29+
"publishConfig": {
30+
"access": "public",
31+
"registry": "https://registry.npmjs.org/"
32+
}
33+
}

packages/gitmoji-regex/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { gitmojis } from 'gitmojis';
2+
import _emojiRegex from 'emoji-regex';
3+
4+
const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => gitmoji.emoji);
5+
6+
export const gitmojiCodeRegex = new RegExp(/:\w*:/);
7+
8+
export const gitmojiUnicodeRegex = new RegExp(gitmojiUnicode.filter((i) => i).join('|'));
9+
10+
export const emojiRegex = _emojiRegex();

0 commit comments

Comments
 (0)