@retorquere/bibtex-parser 6.4.17 → 6.4.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -1
- package/sentence-case.d.ts +8 -0
- package/sentence-case.js +96 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@retorquere/bibtex-parser",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.18",
|
|
4
4
|
"description": "BibTeX parser",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"typings": "./index.d.ts",
|
|
@@ -75,6 +75,8 @@
|
|
|
75
75
|
"jabref.js",
|
|
76
76
|
"README.md",
|
|
77
77
|
"crossref.json",
|
|
78
|
+
"sentence-case.js",
|
|
79
|
+
"sentence-case.d.ts",
|
|
78
80
|
"fields.json",
|
|
79
81
|
"strings.bib",
|
|
80
82
|
"unabbrev.json"
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type TextRange = {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
description?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function restore(text: string, orig: string, preserve: TextRange[]): string;
|
|
7
|
+
export declare function toSentenceCase(text: string, ignoreHTML?: boolean): string;
|
|
8
|
+
//# sourceMappingURL=sentence-case.d.ts.map
|
package/sentence-case.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.toSentenceCase = exports.restore = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
8
|
+
const xregexp_1 = __importDefault(require("xregexp"));
|
|
9
|
+
function restore(text, orig, preserve) {
|
|
10
|
+
for (const { start, end } of preserve) {
|
|
11
|
+
text = text.substring(0, start) + orig.substring(start, end) + text.substring(end);
|
|
12
|
+
}
|
|
13
|
+
return text;
|
|
14
|
+
}
|
|
15
|
+
exports.restore = restore;
|
|
16
|
+
class SentenceCaser {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.acronym = (0, xregexp_1.default)('^(\\p{Lu}[.])+(?=$|[\\P{L}])');
|
|
19
|
+
this.quoted = (0, xregexp_1.default)('^"[^"]+"(?=$|[\\P{L}])');
|
|
20
|
+
this.innerCaps = (0, xregexp_1.default)('\\p{Ll}\\p{Lu}');
|
|
21
|
+
this.allCaps = (0, xregexp_1.default)('^\\p{Lu}+$');
|
|
22
|
+
this.aint = (0, xregexp_1.default)("^\\p{L}n't(?=$|[\\P{L}])"); // isn't
|
|
23
|
+
this.word = (0, xregexp_1.default)('^\\p{L}+(-\\p{L}+)*'); // also match gallium-nitride as one word
|
|
24
|
+
this.and = (0, xregexp_1.default)('^\\p{Lu}&\\p{Lu}(?=$|[\\P{L}])'); // Q&A
|
|
25
|
+
}
|
|
26
|
+
convert(text, ignoreHTML = false) {
|
|
27
|
+
this.input = text;
|
|
28
|
+
this.result = '';
|
|
29
|
+
this.sentenceStart = true;
|
|
30
|
+
const preserve = [];
|
|
31
|
+
if (ignoreHTML) {
|
|
32
|
+
this.input.replace(/[^<>]<[^>]+>/g, (match, i) => {
|
|
33
|
+
preserve.push({ start: i + 1, end: i + match.length });
|
|
34
|
+
// replace markup by the preceding char
|
|
35
|
+
return match[0].repeat(match.length);
|
|
36
|
+
});
|
|
37
|
+
this.input = this.input.replace(/<[^>]+>[^<>]/g, (match, i) => {
|
|
38
|
+
preserve.push({ start: i, end: i + match.length - 1 });
|
|
39
|
+
// replace markup by the following char
|
|
40
|
+
return match[match.length - 1].repeat(match.length);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
this.input = this.input.replace(/[;:]\s+A\s/g, match => match.toLowerCase());
|
|
44
|
+
this.input = this.input.replace(/[–—]\s*A\s/g, match => match.toLowerCase());
|
|
45
|
+
let m;
|
|
46
|
+
while (this.input) {
|
|
47
|
+
if (m = xregexp_1.default.exec(this.input, this.quoted)) { // "Hello There"
|
|
48
|
+
this.add(m[0], undefined, true);
|
|
49
|
+
}
|
|
50
|
+
else if (m = xregexp_1.default.exec(this.input, this.acronym)) { // U.S.
|
|
51
|
+
this.add(m[0], undefined, true);
|
|
52
|
+
}
|
|
53
|
+
else if (m = xregexp_1.default.exec(this.input, this.aint)) { // isn't
|
|
54
|
+
this.add(m[0], undefined, false);
|
|
55
|
+
}
|
|
56
|
+
else if (m = xregexp_1.default.exec(this.input, this.word)) {
|
|
57
|
+
this.add(m[0], '-', false);
|
|
58
|
+
}
|
|
59
|
+
else if (m = xregexp_1.default.exec(this.input, this.and)) {
|
|
60
|
+
this.add(m[0], undefined, true);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.add(this.input[0], undefined, false);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return restore(this.result, text, preserve);
|
|
67
|
+
}
|
|
68
|
+
add(word, splitter, keep) {
|
|
69
|
+
if (splitter) {
|
|
70
|
+
word = word.split(splitter).map((part, i) => {
|
|
71
|
+
if ((keep || this.sentenceStart) && i === 0)
|
|
72
|
+
return part;
|
|
73
|
+
if (xregexp_1.default.exec(part, this.innerCaps))
|
|
74
|
+
return part;
|
|
75
|
+
if (xregexp_1.default.exec(part, this.allCaps))
|
|
76
|
+
return part;
|
|
77
|
+
return part.toLowerCase();
|
|
78
|
+
}).join(splitter);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
if (!keep)
|
|
82
|
+
word = word.toLowerCase();
|
|
83
|
+
}
|
|
84
|
+
this.result += word;
|
|
85
|
+
this.input = this.input.substr(word.length);
|
|
86
|
+
if (!word.match(/^\s+$/)) {
|
|
87
|
+
this.sentenceStart = !!word.match(/^[.?!]$/) || (word.length === 2 && word[1] === '.'); // Vitamin A. Vitamin B.
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const sentenceCaser = new SentenceCaser;
|
|
92
|
+
function toSentenceCase(text, ignoreHTML = false) {
|
|
93
|
+
return sentenceCaser.convert(text, ignoreHTML);
|
|
94
|
+
}
|
|
95
|
+
exports.toSentenceCase = toSentenceCase;
|
|
96
|
+
//# sourceMappingURL=sentence-case.js.map
|