mirror of
https://github.com/jlfwong/speedscope.git
synced 2024-11-22 22:14:25 +03:00
1bcb88670b
* Install prettier, set up the config file, and run it on all ts and tsx files. * Install eslint and configure it with just eslint-plugin-prettier to check to make sure that prettier has been run. * Add a basic .travis.yml that runs eslint. There are other style things that might be nice to enforce with ESLint/TSLint, like using const, import order, etc, but this commit just focuses on prettier, which gets most of the way there. One annoying issue for now is that typescript-eslint-parser gives a big warning message since they haven't updated to officially support TypeScript 2.8 yet. We aren't even using any ESLint rules that need the parser, but if we don't include it, ESLint will crash. TS2.8 support is hopefully coming really soon, though: https://github.com/eslint/typescript-eslint-parser/pull/454 As for the prettier config specifically, see https://prettier.io/docs/en/options.html for the available options. Config settings that seem non-controversial: Semicolons: You don't use semicolons. (I prefer semicolons, but either way is fine.) Quote style: Looks like you consistently use single quotes outside JSX and double quotes in JSX, which is the `singleQuote: true` option. Config settings worth discussion: Line width: You don't have a specific max. I put 100 since I think it's a good number for people (like both of us, probably) who find 80 a bit cramped. (At Benchling we use 110.) Prettier has a big red warning box recommending 80, but I still prefer 100ish. Bracket spacing: This is `{foo}` vs `{ foo }` for imports, exports, object literals, and destructuring. Looks like you're inconsistent but lean toward spaces. I personally really dislike bracket spacing (it feels inconsistent with arrays and function calls), but I'm certainly fine with it and Prettier has it enabled by default, so I kept it enabled. Trailing comma style: Options are "no trailing commas", "trailing commas for everything exception function calls and parameter lists", and "trailing commas everywhere". TypeScript can handle trailing commas everywhere, so there isn't a concern with tooling. You're inconsistent, and it looks like you tend to not have trailing commas, but I think it's probably best to just have them everywhere, so I enabled them. JSX Brackets: You're inconsistent about this, I think. I'd prefer to just keep the default and wrap the `>` to the next line. Arrow function parens: I only found two cases of arrow functions with one param (both `c => c.frame === frame`), and both omitted the parens, so I kept the default of omitting parens. This makes it mildly more annoying to add a typescript type or additional param, which is a possible reason for always requiring parens. Everything else is non-configurable, although it's possible some places would be better with a `// prettier-ignore` comment (but I usually try to avoid those).
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
export function lastOf<T>(ts: T[]): T | null {
|
|
return ts[ts.length - 1] || null
|
|
}
|
|
|
|
export function sortBy<T>(ts: T[], key: (t: T) => number | string): void {
|
|
function comparator(a: T, b: T) {
|
|
return key(a) < key(b) ? -1 : 1
|
|
}
|
|
ts.sort(comparator)
|
|
}
|
|
|
|
export function getOrInsert<K, V>(map: Map<K, V>, k: K, fallback: (k: K) => V): V {
|
|
if (!map.has(k)) map.set(k, fallback(k))
|
|
return map.get(k)!
|
|
}
|
|
|
|
export function getOrElse<K, V>(map: Map<K, V>, k: K, fallback: (k?: K) => V): V {
|
|
if (!map.has(k)) return fallback(k)
|
|
return map.get(k)!
|
|
}
|
|
|
|
export function* itMap<T, U>(it: Iterable<T>, f: (t: T) => U): Iterable<U> {
|
|
for (let t of it) {
|
|
yield f(t)
|
|
}
|
|
}
|
|
|
|
export function itForEach<T>(it: Iterable<T>, f: (t: T) => void): void {
|
|
for (let t of it) {
|
|
f(t)
|
|
}
|
|
}
|
|
|
|
export function itReduce<T, U>(it: Iterable<T>, f: (a: U, b: T) => U, init: U): U {
|
|
let accum: U = init
|
|
for (let t of it) {
|
|
accum = f(accum, t)
|
|
}
|
|
return accum
|
|
}
|
|
|
|
// NOTE: This blindly assumes the same result across contexts.
|
|
const measureTextCache = new Map<string, number>()
|
|
export function cachedMeasureTextWidth(ctx: CanvasRenderingContext2D, text: string): number {
|
|
if (!measureTextCache.has(text)) {
|
|
measureTextCache.set(text, ctx.measureText(text).width)
|
|
}
|
|
return measureTextCache.get(text)!
|
|
}
|