/
punctuation.ts
55 lines (47 loc) · 1.68 KB
/
punctuation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { NodeKind } from './node-kind'
import { Node } from './node'
import { isNumeric, isStartCharacter, isEndCharacter, isPunctuationCharacter, isFullwidthPunctuation } from '../utils/char'
import { isAlphabetNumeric, isPunctuation } from './type-guards'
export class Punctuation implements Node {
readonly children: Node[] = []
readonly char: string
readonly kind = NodeKind.Punctuation
private static cache = new Map<string, Punctuation>()
private constructor (char: string) {
this.char = char
}
needPaddingAfter (next: Node) {
if (this.isFullSize()) return false
if (isPunctuation(next)) return false
if (isAlphabetNumeric(next) && isNumeric(next.text[0]) && ',.'.includes(this.char)) return false
if (isEndCharacter(this.char)) return true
if (isStartCharacter(this.char)) return false
if ('<>='.includes(this.char)) return true
return false
}
needPaddingBefore (prev: Node) {
if (this.isFullSize()) return false
if (isPunctuation(prev)) return false
if (isAlphabetNumeric(prev) && isNumeric(prev.text.slice(-1)) && ',.'.includes(this.char)) return false
if (isStartCharacter(this.char)) return true
if (isEndCharacter(this.char)) return false
if ('<>='.includes(this.char)) return true
return false
}
isFullSize () {
return isFullwidthPunctuation(this.char)
}
toMarkdown () {
return this.char
}
// create a flyweight punctuation
static create (char: string): Punctuation {
if (!Punctuation.cache.has(char)) {
Punctuation.cache.set(char, new Punctuation(char))
}
return Punctuation.cache.get(char)!
}
static is (char: any): char is string {
return isPunctuationCharacter(char)
}
}