mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-17 17:22:42 -05:00
Move helper functions into DOM (#10051)
* Move helper functions into DOM * Add tests and comments
This commit is contained in:
@@ -3,6 +3,44 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { endsWith } from 'vs/base/common/strings';
|
||||
import * as types from 'vs/base/common/types';
|
||||
|
||||
export function isHidden(element: HTMLElement): boolean {
|
||||
return element.style.display === 'none';
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a size value into its string representation. This will add px to the end unless
|
||||
* it already ends with %. If the size value is undefined it will return the defaultValue as-is.
|
||||
* @param size The size value to convert
|
||||
* @param defaultValue The default value to use if the size is undefined
|
||||
*/
|
||||
export function convertSize(size: number | string | undefined, defaultValue?: string): string {
|
||||
defaultValue = defaultValue || '';
|
||||
if (types.isUndefinedOrNull(size)) {
|
||||
return defaultValue;
|
||||
}
|
||||
let convertedSize: string = size ? size.toString() : defaultValue;
|
||||
if (!endsWith(convertedSize.toLowerCase(), 'px') && !endsWith(convertedSize.toLowerCase(), '%')) {
|
||||
convertedSize = convertedSize + 'px';
|
||||
}
|
||||
return convertedSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a size value into its number representation. Supports px, em and unspecified units.
|
||||
* @param size The size value to convert
|
||||
*/
|
||||
export function convertSizeToNumber(size: number | string | undefined): number {
|
||||
if (size && typeof (size) === 'string') {
|
||||
if (endsWith(size.toLowerCase(), 'px')) {
|
||||
return +size.replace('px', '');
|
||||
} else if (endsWith(size.toLowerCase(), 'em')) {
|
||||
return +size.replace('em', '') * 11;
|
||||
}
|
||||
} else if (!size) {
|
||||
return 0;
|
||||
}
|
||||
return +size;
|
||||
}
|
||||
|
||||
58
src/sql/base/test/browser/dom.test.ts
Normal file
58
src/sql/base/test/browser/dom.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { convertSize, convertSizeToNumber } from 'sql/base/browser/dom';
|
||||
|
||||
suite('DOM Tests', () => {
|
||||
|
||||
test('Convert size should add px', () => {
|
||||
const expected = '100px';
|
||||
const actual = convertSize(100);
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert size should not add px if it already has it', () => {
|
||||
const expected = '100px';
|
||||
const actual = convertSize('100px');
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert size should not add px if it is a percent value', () => {
|
||||
const expected = '100%';
|
||||
const actual = convertSize('100%');
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert size should return the default value given undefined value', () => {
|
||||
const expected = '200';
|
||||
const actual = convertSize(undefined, '200');
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert to number should return size without px', () => {
|
||||
const expected = 200;
|
||||
const actual = convertSizeToNumber('200px');
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert to number should return same value if already plain text number', () => {
|
||||
const expected = 200;
|
||||
const actual = convertSizeToNumber('200');
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert to number should return same value if already plain number', () => {
|
||||
const expected = 200;
|
||||
const actual = convertSizeToNumber(200);
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
|
||||
test('Convert to number should return 0 given undefined', () => {
|
||||
const expected = 0;
|
||||
const actual = convertSizeToNumber(undefined);
|
||||
assert.equal(expected, actual);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user