1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import type {AnyParseNode} from "./parseNode";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const contains = function<T>(list: Array<T>, elem: T): boolean {
|
13 | return list.indexOf(elem) !== -1;
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const deflt = function<T>(setting: T | void, defaultIfUndefined: T): * {
|
21 | return setting === undefined ? defaultIfUndefined : setting;
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 | const uppercase = /([A-Z])/g;
|
27 | const hyphenate = function(str: string): string {
|
28 | return str.replace(uppercase, "-$1").toLowerCase();
|
29 | };
|
30 |
|
31 | const ESCAPE_LOOKUP = {
|
32 | "&": "&",
|
33 | ">": ">",
|
34 | "<": "<",
|
35 | "\"": """,
|
36 | "'": "'",
|
37 | };
|
38 |
|
39 | const ESCAPE_REGEX = /[&><"']/g;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function escape(text: mixed): string {
|
45 | return String(text).replace(ESCAPE_REGEX, match => ESCAPE_LOOKUP[match]);
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const getBaseElem = function(group: AnyParseNode): AnyParseNode {
|
54 | if (group.type === "ordgroup") {
|
55 | if (group.body.length === 1) {
|
56 | return getBaseElem(group.body[0]);
|
57 | } else {
|
58 | return group;
|
59 | }
|
60 | } else if (group.type === "color") {
|
61 | if (group.body.length === 1) {
|
62 | return getBaseElem(group.body[0]);
|
63 | } else {
|
64 | return group;
|
65 | }
|
66 | } else if (group.type === "font") {
|
67 | return getBaseElem(group.body);
|
68 | } else {
|
69 | return group;
|
70 | }
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const isCharacterBox = function(group: AnyParseNode): boolean {
|
79 | const baseElem = getBaseElem(group);
|
80 |
|
81 |
|
82 | return baseElem.type === "mathord" ||
|
83 | baseElem.type === "textord" ||
|
84 | baseElem.type === "atom";
|
85 | };
|
86 |
|
87 | export const assert = function<T>(value: ?T): T {
|
88 | if (!value) {
|
89 | throw new Error('Expected non-null, but got ' + String(value));
|
90 | }
|
91 | return value;
|
92 | };
|
93 |
|
94 | export default {
|
95 | contains,
|
96 | deflt,
|
97 | escape,
|
98 | hyphenate,
|
99 | getBaseElem,
|
100 | isCharacterBox,
|
101 | };
|