Skip to content

Instantly share code, notes, and snippets.

@sungchuni
Last active December 22, 2020 06:16
Show Gist options
  • Save sungchuni/d23746c036a64e0d8cf9d981e707426b to your computer and use it in GitHub Desktop.
Save sungchuni/d23746c036a64e0d8cf9d981e707426b to your computer and use it in GitHub Desktop.
for Angular
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