-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (68 loc) · 1.72 KB
/
index.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
// Section 1.
// (a)
// (i)
// (A)
// (I)
var lowerAlpha = require('lower-alpha')
function upperAlpha (i) {
return lowerAlpha(i).toUpperCase()
}
var upperRoman = require('romanize')
function lowerRoman (i) {
return upperRoman(i).toLowerCase()
}
function arabic (i) {
return i.toString()
}
function compound (primary, secondary) {
return function (element, series) {
return (
(series ? secondary(series) + '-' : '') +
primary(element)
)
}
}
function inTheHole (primary, secondary) {
var compounded = compound(primary, secondary)
return function (element, series) {
return '(' + compounded(element, series) + ')'
}
}
var LEVEL_FORMATTERS = [null, compound(arabic, upperAlpha)]
var REPEATING_FORMATTERS = [
inTheHole(lowerAlpha, arabic),
inTheHole(lowerRoman, arabic),
inTheHole(upperAlpha, arabic),
inTheHole(upperRoman, arabic)
]
function formatterForLevel (level) {
if (level < LEVEL_FORMATTERS.length) {
return LEVEL_FORMATTERS[level]
} else {
var offset = level - LEVEL_FORMATTERS.length
var index = offset % REPEATING_FORMATTERS.length
return REPEATING_FORMATTERS[index]
}
}
function renderComponent (component, level) {
return formatterForLevel(level)(
component.element.number,
component.series.of > 1 ? component.series.number : null
)
}
module.exports = function (numbering, abbreviated) {
if (abbreviated) {
var length = numbering.length
return (
renderComponent(numbering[length - 1], length) +
(length === 1 ? '.' : '')
)
} else {
return (
'Section\u00a0' +
numbering.reduce(function (number, component, i) {
return number + renderComponent(component, i + 1)
}, '')
)
}
}