Skip to content

Commit

Permalink
✨ feat: support multiple aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rettend committed Nov 25, 2023
1 parent e9a845b commit 1f8cc58
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ Hi, read the `README.md` first (starting with [Install](#-install)). This emoji
| `feat` | || introduced a new feature |
| `test` | | 🧪 | worked on tests |
| `refactor` | | ♻️ | refactored code, achieved the same with less |
| `init` | | 🎉 | started a new project! |
| `initial` | | 🎉 | this is for the default GitHub commit message (untested) |
| `init` | `initial` | | 🎉 | started a new project! |
| `perf` | || improved performance, achieve the same faster |
| `config` | | ⚙️ | changed configuration files |
| `style` | | 🎨 | design changes, style changes |
Expand Down Expand Up @@ -208,31 +207,37 @@ Commit message:

#### Nested emojis

You can also nest emojis to create subtypes. After finding the type, `eemoji` will look for subtypes in the commit message.
You can also nest emojis to create subtypes.

Either using conventional commit scopes or just including the subtype in the commit message will work.
After finding the type, `eemoji` will look for subtypes in the commit message.

You can specify multiple emojis by separating them with commas and a **random** one will be chosen.
This is useful for conventional commit scopes, but you can include the subtype anywhere in the commit message.

Notes:

- the `'.'` is the fallback subtype
- specify multiple aliases for a type by separating them with pipes: `feat|feature`
- specify multiple emojis by separating them with commas and a **random** one will be chosen: `💎,💲,💸,💰`

```ts
import { defineConfig } from 'eemoji'

export default defineConfig({
emojis: {
fix: {
'fix': {
'.': '🔧',
'typo': '✏️',
'bug': '🐛'
},
chore: {
'chore': {
'.': '🗑️',
'release': '🔖',
'cleanup': '🧹',
'license': '📜',
'deps': '📦'
},
feat: '',
bounty: '💎,💲,💸,💰'
'feat|feature': '',
'bounty': '💎,💲,💸,💰'
}
})
```
Expand Down
12 changes: 7 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import process from 'node:process'
import { name } from '../package.json'
import emojis from './emojis.json'

type EmojiConfig = Partial<typeof emojis> & {
[key: string]: string | Record<string, string>
}

export interface Config {
format: string
emojis: {
[key: string]: string | Record<string, string>
}
emojis: EmojiConfig
}

export const configTypes = [
Expand All @@ -21,10 +23,10 @@ export type JsFiles = typeof ConfigObject.prototype.jsFiles[number]
export type JsonFiles = typeof ConfigObject.prototype.jsonFiles[number]

export class ConfigObject {
defaultConfig: Config = {
defaultConfig = {
format: '{emoji} {type}: {subject}',
emojis,
}
} satisfies Config

defaultTsConfig = `import { defineConfig } from 'eemoji'
Expand Down
3 changes: 1 addition & 2 deletions src/emojis.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"feat": "",
"test": "🧪",
"refactor": "♻️",
"init": "🎉",
"initial": "🎉",
"init|initial": "🎉",
"up": "⬆️",
"down": "⬇️",
"perf": "",
Expand Down
52 changes: 30 additions & 22 deletions src/utils/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,10 @@ export function eemojify(text: string, config: Config, DEBUG?: number): string {
if (!type || !subject)
throw new Error('Invalid commit message.')

let emoji: string | Record<string, string> | undefined
let emoji = getEmoji(type, text, config, DEBUG)

// if there is an exclamatory mark, then it's a breaking change
if (text.includes('!') && config.emojis.breaking)
emoji = config.emojis.breaking
else
emoji = config.emojis[type.toLowerCase().replace(/\(.*\)/, '')]

// if the emoji is an object, then it's a nested emoji
if (typeof emoji === 'object') {
emoji = getNestedEmoji(text, emoji)
if (DEBUG)
consola.log(`nested emoji: "${emoji}"`)
}
else if (DEBUG) {
if (DEBUG)
consola.log(`emoji: "${emoji}"`)
}

if (!emoji)
throw new Error(`Emoji for type "${type}" not found.`)
Expand All @@ -51,13 +38,34 @@ export function eemojify(text: string, config: Config, DEBUG?: number): string {
.replace('{subject}', subject)
}

function getNestedEmoji(text: string, emoji: Record<string, string>): string | undefined {
const entries = Object.entries(emoji).filter(([key]) => key !== '.')
function getEmoji(type: string, text: string, config: Config, DEBUG?: number): string | undefined {
if (text.includes('!') && config.emojis.breaking)
return config.emojis.breaking

const emojiKey = Object.keys(config.emojis).find(key => key.split('|').includes(type.toLowerCase().replace(/\(.*\)/, '').trim()))

for (const [key, value] of entries) {
if (text.includes(key))
return value
}
if (!emojiKey)
return undefined

return emoji['.']
const emoji = config.emojis[emojiKey]

if (typeof emoji === 'object') {
if (DEBUG)
consola.log(`nested emoji: ${JSON.stringify(emoji)}`)

const entries = Object.entries(emoji).filter(([key]) => key !== '.')

for (const [key, value] of entries) {
if (key.split('|').some(k => text.toLowerCase().includes(k.toLowerCase())))
return value
}

return emoji['.']
}
else if (typeof emoji === 'string') {
return emoji
}
else {
return undefined
}
}

0 comments on commit 1f8cc58

Please sign in to comment.