Add type annotation for some js files (#24787)

This commit is contained in:
fusagiko / takayamaki 2023-05-03 18:43:29 +09:00 committed by GitHub
parent 9b8cb947a7
commit e38b391940
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 49 deletions

View file

@ -84,7 +84,7 @@ const DIGIT_CHARACTERS = [
'~', '~',
]; ];
export const decode83 = (str) => { export const decode83 = (str: string) => {
let value = 0; let value = 0;
let c, digit; let c, digit;
@ -97,13 +97,13 @@ export const decode83 = (str) => {
return value; return value;
}; };
export const intToRGB = int => ({ export const intToRGB = (int: number) => ({
r: Math.max(0, (int >> 16)), r: Math.max(0, (int >> 16)),
g: Math.max(0, (int >> 8) & 255), g: Math.max(0, (int >> 8) & 255),
b: Math.max(0, (int & 255)), b: Math.max(0, (int & 255)),
}); });
export const getAverageFromBlurhash = blurhash => { export const getAverageFromBlurhash = (blurhash: string) => {
if (!blurhash) { if (!blurhash) {
return null; return null;
} }

View file

@ -1,4 +1,4 @@
export default function compareId (id1, id2) { export default function compareId (id1: string, id2: string) {
if (id1 === id2) { if (id1 === id2) {
return 0; return 0;
} }

View file

@ -1,21 +1,12 @@
// @ts-check
import { supportsPassiveEvents } from 'detect-passive-events'; import { supportsPassiveEvents } from 'detect-passive-events';
// @ts-expect-error import { forceSingleColumn } from './initial_state';
import { forceSingleColumn } from 'mastodon/initial_state';
const LAYOUT_BREAKPOINT = 630; const LAYOUT_BREAKPOINT = 630;
/** export const isMobile = (width: number) => width <= LAYOUT_BREAKPOINT;
* @param {number} width
* @returns {boolean}
*/
export const isMobile = width => width <= LAYOUT_BREAKPOINT;
/** export type LayoutType = 'mobile' | 'single-column' | 'multi-column';
* @returns {string} export const layoutFromWindow = (): LayoutType => {
*/
export const layoutFromWindow = () => {
if (isMobile(window.innerWidth)) { if (isMobile(window.innerWidth)) {
return 'mobile'; return 'mobile';
} else if (forceSingleColumn) { } else if (forceSingleColumn) {
@ -25,8 +16,9 @@ export const layoutFromWindow = () => {
} }
}; };
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error // @ts-expect-error
const iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; const iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && window.MSStream != null;
const listenerOptions = supportsPassiveEvents ? { passive: true } : false; const listenerOptions = supportsPassiveEvents ? { passive: true } : false;

View file

@ -1,6 +1,5 @@
const easingOutQuint = (x, t, b, c, d) => c * ((t = t / d - 1) * t * t * t * t + 1) + b; const easingOutQuint = (x: number, t: number, b: number, c: number, d: number) => c * ((t = t / d - 1) * t * t * t * t + 1) + b;
const scroll = (node: Element, key: 'scrollTop' | 'scrollLeft', target: number) => {
const scroll = (node, key, target) => {
const startTime = Date.now(); const startTime = Date.now();
const offset = node[key]; const offset = node[key];
const gap = target - offset; const gap = target - offset;
@ -28,5 +27,5 @@ const scroll = (node, key, target) => {
const isScrollBehaviorSupported = 'scrollBehavior' in document.documentElement.style; const isScrollBehaviorSupported = 'scrollBehavior' in document.documentElement.style;
export const scrollRight = (node, position) => isScrollBehaviorSupported ? node.scrollTo({ left: position, behavior: 'smooth' }) : scroll(node, 'scrollLeft', position); export const scrollRight = (node: Element, position: number) => isScrollBehaviorSupported ? node.scrollTo({ left: position, behavior: 'smooth' }) : scroll(node, 'scrollLeft', position);
export const scrollTop = (node) => isScrollBehaviorSupported ? node.scrollTo({ top: 0, behavior: 'smooth' }) : scroll(node, 'scrollTop', 0); export const scrollTop = (node: Element) => isScrollBehaviorSupported ? node.scrollTo({ top: 0, behavior: 'smooth' }) : scroll(node, 'scrollTop', 0);

View file

@ -1,4 +1,4 @@
export const decode = base64 => { export const decode = (base64: string): Uint8Array => {
const rawData = window.atob(base64); const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length); const outputArray = new Uint8Array(rawData.length);

View file

@ -1,4 +1,4 @@
export const toServerSideType = columnType => { export const toServerSideType = (columnType: string) => {
switch (columnType) { switch (columnType) {
case 'home': case 'home':
case 'notifications': case 'notifications':

View file

@ -1,23 +1,19 @@
// @ts-check import type { ValueOf } from '../../types/util';
export const DECIMAL_UNITS = Object.freeze({ export const DECIMAL_UNITS = Object.freeze({
ONE: 1, ONE: 1,
TEN: 10, TEN: 10,
HUNDRED: Math.pow(10, 2), HUNDRED: 100,
THOUSAND: Math.pow(10, 3), THOUSAND: 1_000,
MILLION: Math.pow(10, 6), MILLION: 1_000_000,
BILLION: Math.pow(10, 9), BILLION: 1_000_000_000,
TRILLION: Math.pow(10, 12), TRILLION: 1_000_000_000_000,
}); });
export type DecimalUnits = ValueOf<typeof DECIMAL_UNITS>;
const TEN_THOUSAND = DECIMAL_UNITS.THOUSAND * 10; const TEN_THOUSAND = DECIMAL_UNITS.THOUSAND * 10;
const TEN_MILLIONS = DECIMAL_UNITS.MILLION * 10; const TEN_MILLIONS = DECIMAL_UNITS.MILLION * 10;
/**
* @typedef {[number, number, number]} ShortNumber
* Array of: shorten number, unit of shorten number and maximum fraction digits
*/
/** /**
* @param {number} sourceNumber Number to convert to short number * @param {number} sourceNumber Number to convert to short number
* @returns {ShortNumber} Calculated short number * @returns {ShortNumber} Calculated short number
@ -25,7 +21,8 @@ const TEN_MILLIONS = DECIMAL_UNITS.MILLION * 10;
* shortNumber(5936); * shortNumber(5936);
* // => [5.936, 1000, 1] * // => [5.936, 1000, 1]
*/ */
export function toShortNumber(sourceNumber) { export type ShortNumber = [number, DecimalUnits, 0 | 1] // Array of: shorten number, unit of shorten number and maximum fraction digits
export function toShortNumber(sourceNumber: number): ShortNumber {
if (sourceNumber < DECIMAL_UNITS.THOUSAND) { if (sourceNumber < DECIMAL_UNITS.THOUSAND) {
return [sourceNumber, DECIMAL_UNITS.ONE, 0]; return [sourceNumber, DECIMAL_UNITS.ONE, 0];
} else if (sourceNumber < DECIMAL_UNITS.MILLION) { } else if (sourceNumber < DECIMAL_UNITS.MILLION) {
@ -59,20 +56,16 @@ export function toShortNumber(sourceNumber) {
* pluralReady(1793, DECIMAL_UNITS.THOUSAND) * pluralReady(1793, DECIMAL_UNITS.THOUSAND)
* // => 1790 * // => 1790
*/ */
export function pluralReady(sourceNumber, division) { export function pluralReady(sourceNumber: number, division: DecimalUnits): number {
if (division == null || division < DECIMAL_UNITS.HUNDRED) { if (division == null || division < DECIMAL_UNITS.HUNDRED) {
return sourceNumber; return sourceNumber;
} }
let closestScale = division / DECIMAL_UNITS.TEN; const closestScale = division / DECIMAL_UNITS.TEN;
return Math.trunc(sourceNumber / closestScale) * closestScale; return Math.trunc(sourceNumber / closestScale) * closestScale;
} }
/** export function roundTo10(num: number): number {
* @param {number} num
* @returns {number}
*/
export function roundTo10(num) {
return Math.round(num * 0.1) / 0.1; return Math.round(num * 0.1) / 0.1;
} }

View file

@ -1,8 +1,4 @@
interface MastodonMap<T> { import type { MastodonMap } from './util';
get<K extends keyof T>(key: K): T[K];
has<K extends keyof T>(key: K): boolean;
set<K extends keyof T>(key: K, value: T[K]): this;
}
type AccountValues = { type AccountValues = {
id: number; id: number;

View file

@ -0,0 +1,7 @@
export interface MastodonMap<T> {
get<K extends keyof T>(key: K): T[K];
has<K extends keyof T>(key: K): boolean;
set<K extends keyof T>(key: K, value: T[K]): this;
}
export type ValueOf<T> = T[keyof T];