mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-01 01:38:07 +08:00
This reverts commit 4df552c652.
Reason for revert: Breaks esprima.
Original change's description:
> Updates node_modules and update script
>
> Change-Id: I3fcf49bc416301a030d298cfc48c448bfeba4335
> Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1878808
> Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
> Reviewed-by: Yang Guo <yangguo@chromium.org>
> Commit-Queue: Paul Lewis <aerotwist@chromium.org>
TBR=yangguo@chromium.org,aerotwist@chromium.org,tvanderlippe@chromium.org
Change-Id: Iad2bcdf98a486476715d1e829cdce450c5f218af
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1880098
Reviewed-by: Lorne Mitchell <lomitch@microsoft.com>
Commit-Queue: Lorne Mitchell <lomitch@microsoft.com>
97 lines
2.3 KiB
Plaintext
97 lines
2.3 KiB
Plaintext
import _ from 'lodash';
|
|
import stringWidth from 'string-width';
|
|
|
|
const alignments = [
|
|
'left',
|
|
'right',
|
|
'center'
|
|
];
|
|
|
|
/**
|
|
* @param {string} subject
|
|
* @param {number} width
|
|
* @returns {string}
|
|
*/
|
|
const alignLeft = (subject, width) => {
|
|
return subject + ' '.repeat(width);
|
|
};
|
|
|
|
/**
|
|
* @param {string} subject
|
|
* @param {number} width
|
|
* @returns {string}
|
|
*/
|
|
const alignRight = (subject, width) => {
|
|
return ' '.repeat(width) + subject;
|
|
};
|
|
|
|
/**
|
|
* @param {string} subject
|
|
* @param {number} width
|
|
* @returns {string}
|
|
*/
|
|
const alignCenter = (subject, width) => {
|
|
let halfWidth;
|
|
|
|
halfWidth = width / 2;
|
|
|
|
if (halfWidth % 2 === 0) {
|
|
return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
|
|
} else {
|
|
halfWidth = Math.floor(halfWidth);
|
|
|
|
return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth + 1);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Pads a string to the left and/or right to position the subject
|
|
* text in a desired alignment within a container.
|
|
*
|
|
* @param {string} subject
|
|
* @param {number} containerWidth
|
|
* @param {string} alignment One of the valid options (left, right, center).
|
|
* @returns {string}
|
|
*/
|
|
export default (subject, containerWidth, alignment) => {
|
|
if (!_.isString(subject)) {
|
|
throw new TypeError('Subject parameter value must be a string.');
|
|
}
|
|
|
|
if (!_.isNumber(containerWidth)) {
|
|
throw new TypeError('Container width parameter value must be a number.');
|
|
}
|
|
|
|
const subjectWidth = stringWidth(subject);
|
|
|
|
if (subjectWidth > containerWidth) {
|
|
// console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);
|
|
|
|
throw new Error('Subject parameter value width cannot be greater than the container width.');
|
|
}
|
|
|
|
if (!_.isString(alignment)) {
|
|
throw new TypeError('Alignment parameter value must be a string.');
|
|
}
|
|
|
|
if (alignments.indexOf(alignment) === -1) {
|
|
throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
|
|
}
|
|
|
|
if (subjectWidth === 0) {
|
|
return ' '.repeat(containerWidth);
|
|
}
|
|
|
|
const availableWidth = containerWidth - subjectWidth;
|
|
|
|
if (alignment === 'left') {
|
|
return alignLeft(subject, availableWidth);
|
|
}
|
|
|
|
if (alignment === 'right') {
|
|
return alignRight(subject, availableWidth);
|
|
}
|
|
|
|
return alignCenter(subject, availableWidth);
|
|
};
|