-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add isGurmukhi function to check if string is Unicode Gurmukhi (#…
…141)
- Loading branch information
Showing
8 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const vishraams = Object.values( require( './vishraams.json' ) ) | ||
|
||
// Checks if Unicode Text is in Gurmukhi Block (U+0A00 - U+0A7F) | ||
const checkCharCode = text => ( text.charCodeAt( 0 ) >= 2560 && text.charCodeAt( 0 ) <= 2687 ) | ||
|
||
// Characters to filter from text if doing an exhaustive check | ||
const filteredChars = [ | ||
' ', | ||
'\u200B', | ||
'।', | ||
'॥', | ||
...vishraams, | ||
] | ||
|
||
/** | ||
* Checks if first char in string is part of the Gurmukhi Unicode block. | ||
* @param {String} text The text to check. | ||
* @param {boolean} [exhaustive] If `true`, checks if the whole string is Unicode Gurmukhi. | ||
* @return {boolean} True if Unicode Gurmukhi, false if other. | ||
* @example | ||
* isGurmukhi('ਗੁਰਮੁਖੀ') // => true | ||
* isGurmukhi('gurmuKI') // => false | ||
*/ | ||
const isGurmukhi = ( text, exhaustive ) => ( | ||
exhaustive | ||
? text.split( '' ) | ||
.filter( i => !filteredChars.includes( i ) ) | ||
.every( checkCharCode ) | ||
: checkCharCode( text ) | ||
) | ||
|
||
module.exports = isGurmukhi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { expect } = require( 'chai' ) | ||
|
||
const { isGurmukhi } = require( '../index' ) | ||
|
||
describe( 'isGurmukhi(line)', () => { | ||
const lines = [ | ||
[ 'ਗੁਰਮੁਖੀ', true ], | ||
[ 'ਮੈਂ ਗੁਰਮੁਖੀ ਵਿਚ ਲਿਖ ਰਿਹਾ ਹਾਂ।', true ], | ||
[ 'ਲੜੀਵਾਰਗੁਰਬਾਣੀ', true ], // Has U+200B, Zero Width Space | ||
[ 'मैं हिंदी में लिख रहा हूँ।', false ], | ||
[ 'میں شاہ رخ میں لکھ رہا ہوں۔', false ], | ||
[ 'ਗੁਰਮੁਖੀ & English', true ], | ||
[ 'English & ਗੁਰਮੁਖੀ', false ], | ||
] | ||
|
||
lines.map( ( [ string, result ] ) => it( `String '${string}' should return ${result}`, () => { | ||
expect( isGurmukhi( string ) ).to.equal( result ) | ||
} ) ) | ||
} ) | ||
|
||
describe( 'isGurmukhi(line, true)', () => { | ||
const lines = [ | ||
[ 'ਗੁਰਮੁਖੀ', true ], | ||
[ 'ਮੈਂ ਗੁਰਮੁਖੀ ਵਿਚ ਲਿਖ ਰਿਹਾ ਹਾਂ।', true ], | ||
[ 'ਲੜੀਵਾਰਗੁਰਬਾਣੀ', true ], // Has U+200B, Zero Width Space | ||
[ 'मैं हिंदी में लिख रहा हूँ।', false ], | ||
[ 'میں شاہ رخ میں لکھ رہا ہوں۔', false ], | ||
[ 'ਗੁਰਮੁਖੀ & English', false ], | ||
[ 'English & ਗੁਰਮੁਖੀ', false ], | ||
] | ||
|
||
lines.map( ( [ string, result ] ) => it( `String '${string}' should return ${result}`, () => { | ||
expect( isGurmukhi( string, true ) ).to.equal( result ) | ||
} ) ) | ||
} ) |