Skip to content

Commit

Permalink
🐛 Parse unicode emoji in commit (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored Oct 27, 2018
1 parent 64ac5a7 commit 4d52747
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/gitmoji-changelog-core/src/parser.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
13 changes: 13 additions & 0 deletions packages/gitmoji-changelog-core/src/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4d52747

Please sign in to comment.