UNPKG

1.62 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) {
21 if (className === void 0) { className = ""; }
22 if (containerElement === void 0) { containerElement = document.body; }
23 if (containerElement == null) {
24 return 0;
25 }
26 var span = document.createElement("span");
27 span.classList.add(className);
28 span.textContent = text;
29 containerElement.appendChild(span);
30 var spanWidth = span.offsetWidth;
31 span.remove();
32 return spanWidth;
33}
34export function padWithZeroes(str, minLength) {
35 if (str.length < minLength) {
36 return "".concat(stringRepeat("0", minLength - str.length)).concat(str);
37 }
38 else {
39 return str;
40 }
41}
42function stringRepeat(str, numTimes) {
43 return new Array(numTimes + 1).join(str);
44}
45//# sourceMappingURL=utils.js.map
\No newline at end of file