diff --git a/packages/gitmoji-changelog-core/src/parser.js b/packages/gitmoji-changelog-core/src/parser.js index cc299ae..cc04c4f 100644 --- a/packages/gitmoji-changelog-core/src/parser.js +++ b/packages/gitmoji-changelog-core/src/parser.js @@ -1,14 +1,28 @@ const splitLines = require('split-lines') +const { invert } = require('lodash') const groupMapping = require('./groupMapping') const emojiMapping = require('./emojiMapping') +const emojiMappingInvert = invert(emojiMapping) + function parseSubject(subject) { if (!subject) return {} - const matches = subject.match(/:(\w*):(.*)/) - if (!matches) return { message: subject } + let emojiCode + let message = subject - const [, emojiCode, message] = matches + const matches = subject.match(/:(\w*):(.*)/) + if (matches) { + // extract textual emoji + [, emojiCode, message] = matches + } else { + // extract unicode emoji + const emoji = subject.substr(0, 1) + emojiCode = emojiMappingInvert[emoji] + if (emojiCode) { + message = subject.substr(1, subject.length) + } + } return { emojiCode, diff --git a/packages/gitmoji-changelog-core/src/parser.spec.js b/packages/gitmoji-changelog-core/src/parser.spec.js index 789706d..7c0988f 100644 --- a/packages/gitmoji-changelog-core/src/parser.spec.js +++ b/packages/gitmoji-changelog-core/src/parser.spec.js @@ -21,6 +21,19 @@ describe('commits parser', () => { expect(parseCommit(commit)).toEqual(expect.objectContaining(sparklesCommit)) }) + it('should parse a unicode emoji', () => { + const { + hash, + date, + body, + } = sparklesCommit + const commit = `\n${hash}\n${date}\n✨ Upgrade brand new feature\n${body}\n` + const parsed = parseCommit(commit) + expect(parsed.emoji).toEqual('✨') + expect(parsed.emojiCode).toEqual('sparkles') + expect(parsed.message).toEqual('Upgrade brand new feature') + }) + it('should parse a single commit without a body', () => { const { hash,