1 | ;
|
2 | // Copyright (c) Jupyter Development Team.
|
3 | // Distributed under the terms of the Modified BSD License.
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.Text = void 0;
|
6 | /**
|
7 | * The namespace for text-related functions.
|
8 | */
|
9 | var Text;
|
10 | (function (Text) {
|
11 | // javascript stores text as utf16 and string indices use "code units",
|
12 | // which stores high-codepoint characters as "surrogate pairs",
|
13 | // which occupy two indices in the javascript string.
|
14 | // We need to translate cursor_pos in the Jupyter protocol (in characters)
|
15 | // to js offset (with surrogate pairs taking two spots).
|
16 | const HAS_SURROGATES = '𝐚'.length > 1;
|
17 | /**
|
18 | * Convert a javascript string index into a unicode character offset
|
19 | *
|
20 | * @param jsIdx - The javascript string index (counting surrogate pairs)
|
21 | *
|
22 | * @param text - The text in which the offset is calculated
|
23 | *
|
24 | * @returns The unicode character offset
|
25 | */
|
26 | function jsIndexToCharIndex(jsIdx, text) {
|
27 | if (HAS_SURROGATES) {
|
28 | // not using surrogates, nothing to do
|
29 | return jsIdx;
|
30 | }
|
31 | let charIdx = jsIdx;
|
32 | for (let i = 0; i + 1 < text.length && i < jsIdx; i++) {
|
33 | const charCode = text.charCodeAt(i);
|
34 | // check for surrogate pair
|
35 | if (charCode >= 0xd800 && charCode <= 0xdbff) {
|
36 | const nextCharCode = text.charCodeAt(i + 1);
|
37 | if (nextCharCode >= 0xdc00 && nextCharCode <= 0xdfff) {
|
38 | charIdx--;
|
39 | i++;
|
40 | }
|
41 | }
|
42 | }
|
43 | return charIdx;
|
44 | }
|
45 | Text.jsIndexToCharIndex = jsIndexToCharIndex;
|
46 | /**
|
47 | * Convert a unicode character offset to a javascript string index.
|
48 | *
|
49 | * @param charIdx - The index in unicode characters
|
50 | *
|
51 | * @param text - The text in which the offset is calculated
|
52 | *
|
53 | * @returns The js-native index
|
54 | */
|
55 | function charIndexToJsIndex(charIdx, text) {
|
56 | if (HAS_SURROGATES) {
|
57 | // not using surrogates, nothing to do
|
58 | return charIdx;
|
59 | }
|
60 | let jsIdx = charIdx;
|
61 | for (let i = 0; i + 1 < text.length && i < jsIdx; i++) {
|
62 | const charCode = text.charCodeAt(i);
|
63 | // check for surrogate pair
|
64 | if (charCode >= 0xd800 && charCode <= 0xdbff) {
|
65 | const nextCharCode = text.charCodeAt(i + 1);
|
66 | if (nextCharCode >= 0xdc00 && nextCharCode <= 0xdfff) {
|
67 | jsIdx++;
|
68 | i++;
|
69 | }
|
70 | }
|
71 | }
|
72 | return jsIdx;
|
73 | }
|
74 | Text.charIndexToJsIndex = charIndexToJsIndex;
|
75 | /**
|
76 | * Given a 'snake-case', 'snake_case', 'snake:case', or
|
77 | * 'snake case' string, will return the camel case version: 'snakeCase'.
|
78 | *
|
79 | * @param str the snake-case input string.
|
80 | *
|
81 | * @param upper default = false. If true, the first letter of the
|
82 | * returned string will be capitalized.
|
83 | *
|
84 | * @returns the camel case version of the input string.
|
85 | */
|
86 | function camelCase(str, upper = false) {
|
87 | return str.replace(/^(\w)|[\s-_:]+(\w)/g, function (match, p1, p2) {
|
88 | if (p2) {
|
89 | return p2.toUpperCase();
|
90 | }
|
91 | else {
|
92 | return upper ? p1.toUpperCase() : p1.toLowerCase();
|
93 | }
|
94 | });
|
95 | }
|
96 | Text.camelCase = camelCase;
|
97 | /**
|
98 | * Given a string, title case the words in the string.
|
99 | *
|
100 | * @param str the string to title case.
|
101 | *
|
102 | * @returns the same string, but with each word capitalized.
|
103 | */
|
104 | function titleCase(str) {
|
105 | return (str || '')
|
106 | .toLowerCase()
|
107 | .split(' ')
|
108 | .map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
109 | .join(' ');
|
110 | }
|
111 | Text.titleCase = titleCase;
|
112 | })(Text || (exports.Text = Text = {}));
|
113 | //# sourceMappingURL=text.js.map |
\ | No newline at end of file |