UNPKG

1.51 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/**
17 * Measure width in pixels of a string displayed with styles provided by `className`.
18 * Should only be used if measuring can't be done with existing DOM elements.
19 */
20export function measureTextWidth(text, className = "", containerElement = document.body) {
21 if (containerElement == null) {
22 return 0;
23 }
24 const span = document.createElement("span");
25 span.classList.add(className);
26 span.textContent = text;
27 containerElement.appendChild(span);
28 const spanWidth = span.offsetWidth;
29 span.remove();
30 return spanWidth;
31}
32export function padWithZeroes(str, minLength) {
33 if (str.length < minLength) {
34 return `${stringRepeat("0", minLength - str.length)}${str}`;
35 }
36 else {
37 return str;
38 }
39}
40function stringRepeat(str, numTimes) {
41 return new Array(numTimes + 1).join(str);
42}
43//# sourceMappingURL=utils.js.map
\No newline at end of file