Skip to content

Commit a403a3f

Browse files
authored
feat(convert): Added whitespace options
Merge pull request #4 from itpropro/feat/whitespace
2 parents 1945f0b + 142dcd9 commit a403a3f

6 files changed

Lines changed: 169 additions & 23 deletions

File tree

.eslintrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55
},
66
"env": {
77
"es6": true
8-
}
8+
},
9+
"extends": [
10+
"eslint:recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
13+
"plugins": [
14+
"@typescript-eslint"
15+
]
916
}

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ yarn add convert-gitmoji
2020

2121
## Usage
2222

23-
### `convert(content: string, withSpace?: boolean)`
23+
### `convert(content: string, withSpace?: boolean | "leading" | "trailing" | "both")`
2424

2525
Convert all gitmojis in a string
26-
`withSpace` is default `false`, if `true` its sets a " " after the converted gitmoji
26+
`withSpace` is default `false`, if `true` its sets a trailing (at the end of the string) whitespace after the converted gitmoji.
27+
28+
For more control, `withSpace` can also be set to `leading` for a whitespace *before the string*, `trailing` for a whitespace *at the end of the string* (same as `true`) or `both` to have the string surrounded by whitespaces.
2729

2830
```js
2931
// CommonJS
@@ -35,6 +37,14 @@ import { convert } from "convert-gitmoji";
3537
convert(':arrow_up: bump qs from 6.10.3 to 6.10.4 (xxx) - **helper:** :pencil: Updated TSDoc (xxx)', true);
3638

3739
--> "⬆️ bump qs from 6.10.3 to 6.10.4 (xxx) - **helper:** ✏️ Updated TSDoc (xxx)"
40+
41+
convert("This:art:is on:fire:!")
42+
43+
--> "This🎨is on🔥!"
44+
45+
convert("This:art:is on:fire:!", "both")
46+
47+
--> "This 🎨 is on 🔥 !"
3848
```
3949

4050
## License

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
},
2727
"devDependencies": {
2828
"@types/node": "^17.0.23",
29+
"@typescript-eslint/eslint-plugin": "^5.30.5",
30+
"@typescript-eslint/parser": "^5.30.5",
2931
"eslint": "^8.12.0",
3032
"standard-version": "^9.3.2",
3133
"typescript": "^4.6.3",

src/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const gitmojis = {
1+
const gitmojis: Record<string, string> = {
22
":art:": "🎨",
33
":zap:": "⚡️",
44
":fire:": "🔥",
@@ -15,7 +15,7 @@ const gitmojis = {
1515
":bookmark:": "🔖",
1616
":rotating_light:": "🚨",
1717
":construction:": "🚧",
18-
":green_heart": "💚",
18+
":green_heart:": "💚",
1919
":arrow_down:": "⬇️",
2020
":arrow_up:": "⬆️",
2121
":pushpin:": "📌",
@@ -73,15 +73,19 @@ const gitmojis = {
7373
":money_with_wings:": "💸",
7474
};
7575

76-
export function convert(content: string, withSpace?: boolean) {
77-
var re = new RegExp(Object.keys(gitmojis).join("|"), "gi");
78-
return content.replace(re, function(matched) {
79-
if (withSpace) {
80-
// @ts-ignore
81-
return gitmojis[matched.toLowerCase()] + " ";
82-
} else {
83-
// @ts-ignore
84-
return gitmojis[matched.toLowerCase()];
85-
}
86-
})
76+
export function convert(content: string, withSpace?: boolean | "leading" | "trailing" | "both") {
77+
const re = new RegExp(Object.keys(gitmojis).join("|"), "gi");
78+
return content.replace(re, function (matched) {
79+
switch (withSpace) {
80+
case true:
81+
case "trailing":
82+
return `${gitmojis[matched.toLowerCase()]} `
83+
case "leading":
84+
return ` ${gitmojis[matched.toLowerCase()]}`
85+
case "both":
86+
return ` ${gitmojis[matched.toLowerCase()]} `
87+
default:
88+
return gitmojis[matched.toLowerCase()]
89+
}
90+
});
8791
}

test/convert.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ const gitmojis = [
291291
const content = ":arrow_up: bump qs from 6.10.3 to 6.10.4 (xxx) - **helper:** :zap: Updated TSDoc (xxx) ";
292292
const contentWithGitmoji = "⬆️ bump qs from 6.10.3 to 6.10.4 (xxx) - **helper:** ⚡️ Updated TSDoc (xxx) ";
293293

294-
const pencil = "this is :pencil: 1 and this is :pencil2:"
295-
const pencilWithGitmoji = "this is ✏️ 1 and this is ✏️"
294+
const pencil = "this is :pencil: 1 and this is :pencil2:";
295+
const pencilWithGitmoji = "this is ✏️ 1 and this is ✏️";
296296

297297
describe('convert', () => {
298298
it('should be defined', () => {
@@ -302,14 +302,27 @@ describe('convert', () => {
302302
it('should return the correct gitmoji for every string', () => {
303303
let gitmojisEqual = true;
304304
for (const gitmoji of gitmojis) {
305-
gitmojisEqual = convert(gitmoji.code) === gitmoji.emoji || gitmojisEqual;
305+
gitmojisEqual = convert(gitmoji.code) === gitmoji.emoji && gitmojisEqual;
306306
}
307307
expect(gitmojisEqual).toBe(true);
308308
});
309309

310-
it('should generate space', () => {
311-
const gitmojiWithSpace = convert(gitmojis[0].code, true);
310+
it('should generate leading space', () => {
311+
const gitmojiWithSpace = convert(gitmojis[0].code, "leading");
312+
expect(gitmojiWithSpace).toBe(` ${gitmojis[0].emoji}`);
313+
});
314+
315+
it('should generate trailing space', () => {
316+
const gitmojiWithSpace = convert(gitmojis[0].code, "trailing");
312317
expect(gitmojiWithSpace).toBe(`${gitmojis[0].emoji} `);
318+
// Backwards compatibility
319+
const gitmojiWithSpace2 = convert(gitmojis[0].code, true);
320+
expect(gitmojiWithSpace2).toBe(`${gitmojis[0].emoji} `);
321+
});
322+
323+
it('should generate both spaces', () => {
324+
const gitmojiWithSpace = convert(gitmojis[0].code, "both");
325+
expect(gitmojiWithSpace).toBe(` ${gitmojis[0].emoji} `);
313326
});
314327

315328
it('should not generate space', () => {

yarn.lock

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@
369369
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
370370
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
371371

372+
"@types/json-schema@^7.0.9":
373+
version "7.0.11"
374+
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
375+
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
376+
372377
"@types/minimist@^1.2.0":
373378
version "1.2.2"
374379
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
@@ -396,6 +401,86 @@
396401
dependencies:
397402
"@types/node" "*"
398403

404+
"@typescript-eslint/eslint-plugin@^5.30.5":
405+
version "5.30.5"
406+
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.30.5.tgz#e9a0afd6eb3b1d663db91cf1e7bc7584d394503d"
407+
integrity sha512-lftkqRoBvc28VFXEoRgyZuztyVUQ04JvUnATSPtIRFAccbXTWL6DEtXGYMcbg998kXw1NLUJm7rTQ9eUt+q6Ig==
408+
dependencies:
409+
"@typescript-eslint/scope-manager" "5.30.5"
410+
"@typescript-eslint/type-utils" "5.30.5"
411+
"@typescript-eslint/utils" "5.30.5"
412+
debug "^4.3.4"
413+
functional-red-black-tree "^1.0.1"
414+
ignore "^5.2.0"
415+
regexpp "^3.2.0"
416+
semver "^7.3.7"
417+
tsutils "^3.21.0"
418+
419+
"@typescript-eslint/parser@^5.30.5":
420+
version "5.30.5"
421+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.30.5.tgz#f667c34e4e4c299d98281246c9b1e68c03a92522"
422+
integrity sha512-zj251pcPXI8GO9NDKWWmygP6+UjwWmrdf9qMW/L/uQJBM/0XbU2inxe5io/234y/RCvwpKEYjZ6c1YrXERkK4Q==
423+
dependencies:
424+
"@typescript-eslint/scope-manager" "5.30.5"
425+
"@typescript-eslint/types" "5.30.5"
426+
"@typescript-eslint/typescript-estree" "5.30.5"
427+
debug "^4.3.4"
428+
429+
"@typescript-eslint/scope-manager@5.30.5":
430+
version "5.30.5"
431+
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.30.5.tgz#7f90b9d6800552c856a5f3644f5e55dd1469d964"
432+
integrity sha512-NJ6F+YHHFT/30isRe2UTmIGGAiXKckCyMnIV58cE3JkHmaD6e5zyEYm5hBDv0Wbin+IC0T1FWJpD3YqHUG/Ydg==
433+
dependencies:
434+
"@typescript-eslint/types" "5.30.5"
435+
"@typescript-eslint/visitor-keys" "5.30.5"
436+
437+
"@typescript-eslint/type-utils@5.30.5":
438+
version "5.30.5"
439+
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.30.5.tgz#7a9656f360b4b1daea635c4621dab053d08bf8a9"
440+
integrity sha512-k9+ejlv1GgwN1nN7XjVtyCgE0BTzhzT1YsQF0rv4Vfj2U9xnslBgMYYvcEYAFVdvhuEscELJsB7lDkN7WusErw==
441+
dependencies:
442+
"@typescript-eslint/utils" "5.30.5"
443+
debug "^4.3.4"
444+
tsutils "^3.21.0"
445+
446+
"@typescript-eslint/types@5.30.5":
447+
version "5.30.5"
448+
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.30.5.tgz#36a0c05a72af3623cdf9ee8b81ea743b7de75a98"
449+
integrity sha512-kZ80w/M2AvsbRvOr3PjaNh6qEW1LFqs2pLdo2s5R38B2HYXG8Z0PP48/4+j1QHJFL3ssHIbJ4odPRS8PlHrFfw==
450+
451+
"@typescript-eslint/typescript-estree@5.30.5":
452+
version "5.30.5"
453+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.30.5.tgz#c520e4eba20551c4ec76af8d344a42eb6c9767bb"
454+
integrity sha512-qGTc7QZC801kbYjAr4AgdOfnokpwStqyhSbiQvqGBLixniAKyH+ib2qXIVo4P9NgGzwyfD9I0nlJN7D91E1VpQ==
455+
dependencies:
456+
"@typescript-eslint/types" "5.30.5"
457+
"@typescript-eslint/visitor-keys" "5.30.5"
458+
debug "^4.3.4"
459+
globby "^11.1.0"
460+
is-glob "^4.0.3"
461+
semver "^7.3.7"
462+
tsutils "^3.21.0"
463+
464+
"@typescript-eslint/utils@5.30.5":
465+
version "5.30.5"
466+
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.30.5.tgz#3999cbd06baad31b9e60d084f20714d1b2776765"
467+
integrity sha512-o4SSUH9IkuA7AYIfAvatldovurqTAHrfzPApOZvdUq01hHojZojCFXx06D/aFpKCgWbMPRdJBWAC3sWp3itwTA==
468+
dependencies:
469+
"@types/json-schema" "^7.0.9"
470+
"@typescript-eslint/scope-manager" "5.30.5"
471+
"@typescript-eslint/types" "5.30.5"
472+
"@typescript-eslint/typescript-estree" "5.30.5"
473+
eslint-scope "^5.1.1"
474+
eslint-utils "^3.0.0"
475+
476+
"@typescript-eslint/visitor-keys@5.30.5":
477+
version "5.30.5"
478+
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.30.5.tgz#d4bb969202019d5d5d849a0aaedc7370cc044b14"
479+
integrity sha512-D+xtGo9HUMELzWIUqcQc0p2PO4NyvTrgIOK/VnSH083+8sq0tiLozNRKuLarwHYGRuA6TVBQSuuLwJUDWd3aaA==
480+
dependencies:
481+
"@typescript-eslint/types" "5.30.5"
482+
eslint-visitor-keys "^3.3.0"
483+
399484
JSONStream@^1.0.4:
400485
version "1.3.5"
401486
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
@@ -1207,6 +1292,14 @@ escape-string-regexp@^4.0.0:
12071292
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
12081293
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==
12091294

1295+
eslint-scope@^5.1.1:
1296+
version "5.1.1"
1297+
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
1298+
integrity sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==
1299+
dependencies:
1300+
esrecurse "^4.3.0"
1301+
estraverse "^4.1.1"
1302+
12101303
eslint-scope@^7.1.1:
12111304
version "7.1.1"
12121305
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-7.1.1.tgz#fff34894c2f65e5226d3041ac480b4513a163642"
@@ -1296,6 +1389,11 @@ esrecurse@^4.3.0:
12961389
dependencies:
12971390
estraverse "^5.2.0"
12981391

1392+
estraverse@^4.1.1:
1393+
version "4.3.0"
1394+
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d"
1395+
integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw==
1396+
12991397
estraverse@^5.1.0, estraverse@^5.2.0:
13001398
version "5.3.0"
13011399
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.3.0.tgz#2eea5290702f26ab8fe5370370ff86c965d21123"
@@ -1539,7 +1637,7 @@ globals@^13.15.0:
15391637
dependencies:
15401638
type-fest "^0.20.2"
15411639

1542-
globby@^11.0.3:
1640+
globby@^11.0.3, globby@^11.1.0:
15431641
version "11.1.0"
15441642
resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
15451643
integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==
@@ -2446,7 +2544,7 @@ semver@^6.0.0, semver@^6.3.0:
24462544
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
24472545
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
24482546

2449-
semver@^7.1.1, semver@^7.3.4:
2547+
semver@^7.1.1, semver@^7.3.4, semver@^7.3.7:
24502548
version "7.3.7"
24512549
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
24522550
integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
@@ -2673,6 +2771,18 @@ trim-newlines@^3.0.0:
26732771
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
26742772
integrity sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw==
26752773

2774+
tslib@^1.8.1:
2775+
version "1.14.1"
2776+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
2777+
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
2778+
2779+
tsutils@^3.21.0:
2780+
version "3.21.0"
2781+
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
2782+
integrity sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==
2783+
dependencies:
2784+
tslib "^1.8.1"
2785+
26762786
type-check@^0.4.0, type-check@~0.4.0:
26772787
version "0.4.0"
26782788
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"

0 commit comments

Comments
 (0)