Skip to content

Commit

Permalink
Add option to return 한글 for ruby annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mooniker committed Nov 9, 2020
1 parent f8d933a commit 3447195
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/hangul/unicode/hangulPattern.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff]+/g
const hangulPattern = /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff]+/g

const splitPattern = /([\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff]+|[^\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff]+)/

module.exports = { hangulPattern, splitPattern }
2 changes: 1 addition & 1 deletion src/hangul/unicode/hangulPattern.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hangulPattern = require('./hangulPattern')
const { hangulPattern } = require('./hangulPattern')
const HANGUL_BLOCKS = Object.entries(require('./blocks')).filter(
([blockName]) => blockName.startsWith('HANGUL')
)
Expand Down
13 changes: 2 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
const hangulPattern = require('./hangul/unicode/hangulPattern')
const { romanizeWord } = require('./romanize')
const { romanize, romanizeWord } = require('./romanize')

/**
* Transforms a given string by replacing each Hangul character-containing substring with romaja
* @param {string} text
* @param {Object} [options]
*/
const romanize = (text, options) =>
text.replace(hangulPattern, word => romanizeWord(word, options))

module.exports = { romanize }
module.exports = { romanize, romanizeWord }
25 changes: 24 additions & 1 deletion src/romanize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
const {
hangulPattern,
splitPattern
} = require('./hangul/unicode/hangulPattern')
const { decomposeHangul } = require('./hangul/unicode/decompose')
const { syllableParser } = require('./syllableParser')
const isHangul = require('./hangul/isHangul')

/**
* Transforms a given string by replacing each Hangul character-containing substring with romaja
* @param {string} text
* @param {Object} [options]
* @param {boolean} [ruby]
*/
const romanize = (text, options = {}) => {
if (options.ruby) {
return text.split(splitPattern).map(str => {
if (isHangul(str[0])) {
return { text: romanizeWord(str), ruby: str }
}
return str
})
}
return text.replace(hangulPattern, word => romanizeWord(word, options))
}

/**
* Transform a Hangul encoded string to Roman equivalent
Expand Down Expand Up @@ -28,4 +51,4 @@ function romanizeWord (word, options = {}) {
: mappedToRoman.replace(/-$/, '')
}

module.exports = { romanizeWord }
module.exports = { romanize, romanizeWord }

0 comments on commit 3447195

Please sign in to comment.