-
Notifications
You must be signed in to change notification settings - Fork 653
/
api.js
85 lines (82 loc) · 2.06 KB
/
api.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import find from './find.js'
import parse from './parse.js'
import toCardinal from './convert/toCardinal.js'
import toOrdinal from './convert/toOrdinal.js'
const plugin = function (View) {
/**
*/
class Fractions extends View {
constructor(document, pointer, groups) {
super(document, pointer, groups)
this.viewType = 'Fractions'
}
parse(n) {
return this.getNth(n).map(parse)
}
get(n) {
return this.getNth(n).map(parse)
}
json(n) {
return this.getNth(n).map(p => {
let json = p.toView().json(n)[0]
let parsed = parse(p)
json.fraction = parsed
return json
}, [])
}
// become 0.5
toDecimal(n) {
this.getNth(n).forEach(m => {
let { decimal } = parse(m)
m = m.replaceWith(String(decimal), true)
m.tag('NumericValue')
m.unTag('Fraction')
})
return this
}
toFraction(n) {
this.getNth(n).forEach(m => {
let obj = parse(m)
if (obj && typeof obj.numerator === 'number' && typeof obj.denominator === 'number') {
let str = `${obj.numerator}/${obj.denominator}`
this.replace(m, str)
}
})
return this
}
toOrdinal(n) {
this.getNth(n).forEach(m => {
let obj = parse(m)
let str = toOrdinal(obj)
if (m.after('^#Noun').found) {
str += ' of' // three fifths of dentists
}
m.replaceWith(str)
})
return this
}
toCardinal(n) {
this.getNth(n).forEach(m => {
let obj = parse(m)
let str = toCardinal(obj)
m.replaceWith(str)
})
return this
}
toPercentage(n) {
this.getNth(n).forEach(m => {
let { decimal } = parse(m)
let percent = decimal * 100
percent = Math.round(percent * 100) / 100 // round it
m.replaceWith(`${percent}%`)
})
return this
}
}
View.prototype.fractions = function (n) {
let m = find(this)
m = m.getNth(n)
return new Fractions(this.document, m.pointer)
}
}
export default plugin