Last active
December 22, 2020 06:16
-
-
Save sungchuni/d23746c036a64e0d8cf9d981e707426b to your computer and use it in GitHub Desktop.
for Angular
This file contains hidden or 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
import { Pipe, PipeTransform } from "@angular/core"; | |
@Pipe({name: "postPositionPipe"}) | |
export class PostPositionPipe implements PipeTransform { | |
private readonly CHAR_CODE_가 = 44032; | |
private readonly CHAR_CODE_힣 = 55203; | |
private isKoreanCharCode(charCode: number): boolean { | |
return charCode >= this.CHAR_CODE_가 && charCode <= this.CHAR_CODE_힣; | |
} | |
transform(chracters: string, withFinalConsonant: string, withoutFinalConsonant: string): string { | |
const lastCharCode = chracters.slice(-1).charCodeAt(0); | |
if (!this.isKoreanCharCode(lastCharCode)) { | |
return `${chracters}${withFinalConsonant}`; | |
} else { | |
return `${chracters}${(lastCharCode - this.CHAR_CODE_가) % 28 === 0 ? withoutFinalConsonant : withFinalConsonant}`; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment