Files
Sophie Alpert 767f52237c Use .slice() for all substring-ing (#26677)
- substr is Annex B
- substring silently flips its arguments if they're in the "wrong order", which is confusing
- slice is better than sliced bread (no pun intended) and also it works the same way on Arrays so there's less to remember

---

> I'd be down to just lint and enforce a single form just for the potential compression savings by using a repeated string.

_Originally posted by @sebmarkbage in https://github.com/facebook/react/pull/26663#discussion_r1170455401_
2023-04-19 14:26:01 -07:00

46 lines
1.1 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {SchedulingEvent} from '../types';
import prettyMilliseconds from 'pretty-ms';
export function formatTimestamp(ms: number): string {
return (
ms.toLocaleString(undefined, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
}) + 'ms'
);
}
export function formatDuration(ms: number): string {
return prettyMilliseconds(ms, {millisecondsDecimalDigits: 1});
}
export function trimString(string: string, length: number): string {
if (string.length > length) {
return `${string.slice(0, length - 1)}`;
}
return string;
}
export function getSchedulingEventLabel(event: SchedulingEvent): string | null {
switch (event.type) {
case 'schedule-render':
return 'render scheduled';
case 'schedule-state-update':
return 'state update scheduled';
case 'schedule-force-update':
return 'force update scheduled';
default:
return null;
}
}