Skip to content

Commit

Permalink
fix: rectify conditional logic for toEnglish (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhajneet committed Jun 12, 2021
1 parent b98b4b6 commit 5367f17
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/toEnglish.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const spaceCharsRegex = `([${spaceChars.join( '' ).replace( /[.*+?^${}()|[\]\\]/
// Characters that should be supressed into empty characters
const supressions = [ '@', '`', '~', '¤', 'ª', '°', 'Ç', 'Ó', 'Ô', 'Ø', 'æ', 'Œ', '‰' ]

// Characters that should be left as is
const ignore = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', ',', ';', '₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉' ]

// The transliteration mappings from ASCII Gurmukhi to english
const transliterationMap = {
' ': ' ',
Expand Down Expand Up @@ -98,6 +101,8 @@ const transliterationMap = {
'†': 'tt',
// Expand out supressions as char: ''
...supressions.reduce( ( chars, char ) => ( { ...chars, [ char ]: '' } ), {} ),
// Expand out ignore as self
...ignore.reduce( ( chars, char ) => ( { ...chars, [ char ]: char } ), {} ),
}

// Replacements for the initial input
Expand Down Expand Up @@ -147,6 +152,7 @@ const finalReplacements = Object.entries( {
'\\(': '',
'\\)': '',
aaa: 'aa',
aai: 'ai',
eee: 'ee',
nn: 'n',
eeaa: 'eea',
Expand Down Expand Up @@ -190,7 +196,8 @@ const toEnglish = ( line ) => {
const nextNextLetter = line[ index + 2 ] || ''

// Map letter using transliteration map
let mappedLetter = transliterationMap[ letter ] || letter
// If letter is unmappable, then suppress it
let mappedLetter = transliterationMap[ letter ] || ''

// Add in extra `a` if every rule is met
if ( extraARules.every( ( fn ) => fn( mappedLetter, nextLetter, nextNextLetter ) ) ) { mappedLetter += 'a' }
Expand Down
1 change: 1 addition & 0 deletions test/toEnglish.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const transliterations = [
[ 'ਭਾਉ', 'bhaau' ],
[ 'ਸਤਿਗੁਰੁ ਸਤਿਗੁਰੁ ਸਚੁ; ਸਚੁ ਹਰਿ ਹਰਿ ਹਿੰਙੁ', 'satigur satigur sach; sach har har hing' ],
[ 'ਸੁ ਉ ਜੁ', 'su u ju' ],
[ 'ਆਪੀਨੑੈ', 'aapeenai' ],
]

describe( 'toEnglish()', () => {
Expand Down

0 comments on commit 5367f17

Please sign in to comment.