From abc296cdcc73d0e039286a9107ea8b36f02ceae3 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 23 May 2020 23:21:09 +0900 Subject: [PATCH] refactor: use Object.fromEntries() instead of in-house implementation (#6401) * refactor: use Object.fromEntries() instead of in-house implementation * Remove extra type assertions --- src/client/config.ts | 3 +-- src/client/scripts/compose-notification.ts | 3 +-- src/client/scripts/set-i18n-contexts.ts | 3 +-- src/prelude/array.ts | 15 --------------- src/server/api/openapi/description.ts | 20 +++++++++----------- tsconfig.json | 3 +++ 6 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/client/config.ts b/src/client/config.ts index f71647a05..b9a476618 100644 --- a/src/client/config.ts +++ b/src/client/config.ts @@ -1,5 +1,4 @@ import { clientDb, entries } from './db'; -import { fromEntries } from '../prelude/array'; declare const _LANGS_: string[]; declare const _VERSION_: string; @@ -15,7 +14,7 @@ export const apiUrl = url + '/api'; export const wsUrl = url.replace('http://', 'ws://').replace('https://', 'wss://') + '/streaming'; export const lang = localStorage.getItem('lang'); export const langs = _LANGS_; -export const getLocale = async () => fromEntries((await entries(clientDb.i18n)) as [string, string][]); +export const getLocale = async () => Object.fromEntries((await entries(clientDb.i18n)) as [string, string][]); export const version = _VERSION_; export const env = _ENV_; export const instanceName = siteName === 'Misskey' ? null : siteName; diff --git a/src/client/scripts/compose-notification.ts b/src/client/scripts/compose-notification.ts index 29eb515bf..c3281955e 100644 --- a/src/client/scripts/compose-notification.ts +++ b/src/client/scripts/compose-notification.ts @@ -1,13 +1,12 @@ import getNoteSummary from '../../misc/get-note-summary'; import getUserName from '../../misc/get-user-name'; import { clientDb, get, bulkGet } from '../db'; -import { fromEntries } from '../../prelude/array'; const getTranslation = (text: string): Promise => get(text, clientDb.i18n); export default async function(type, data): Promise<[string, NotificationOptions]> { const contexts = ['deletedNote', 'invisibleNote', 'withNFiles', '_cw.poll']; - const locale = fromEntries(await bulkGet(contexts, clientDb.i18n) as [string, string][]); + const locale = Object.fromEntries(await bulkGet(contexts, clientDb.i18n) as [string, string][]); switch (type) { case 'driveFileCreated': // TODO (Server Side) diff --git a/src/client/scripts/set-i18n-contexts.ts b/src/client/scripts/set-i18n-contexts.ts index 2eb76047f..872153e0b 100644 --- a/src/client/scripts/set-i18n-contexts.ts +++ b/src/client/scripts/set-i18n-contexts.ts @@ -1,7 +1,6 @@ import VueI18n from 'vue-i18n'; import { clientDb, clear, bulkSet } from '../db'; import { deepEntries, delimitEntry } from 'deep-entries'; -import { fromEntries } from '../../prelude/array'; export function setI18nContexts(lang: string, version: string, i18n: VueI18n, cleardb = false) { return Promise.all([ @@ -13,6 +12,6 @@ export function setI18nContexts(lang: string, version: string, i18n: VueI18n, cl const flatLocaleEntries = deepEntries(locale, delimitEntry) as [string, string][]; bulkSet(flatLocaleEntries, clientDb.i18n); i18n.locale = lang; - i18n.setLocaleMessage(lang, fromEntries(flatLocaleEntries)); + i18n.setLocaleMessage(lang, Object.fromEntries(flatLocaleEntries)); }); } diff --git a/src/prelude/array.ts b/src/prelude/array.ts index 9e1dfead5..d63f0475d 100644 --- a/src/prelude/array.ts +++ b/src/prelude/array.ts @@ -129,21 +129,6 @@ export function cumulativeSum(xs: number[]): number[] { return ys; } -// Object.fromEntries() -export function fromEntries(xs: T): - T[number] extends infer U - ? - ( - U extends readonly any[] - ? (x: { [_ in U[0]]: U[1] }) => any - : never - ) extends (x: infer V) => any - ? V - : never - : never { - return xs.reduce((obj, [k, v]) => Object.assign(obj, { [k]: v }), {} as { [x: string]: any; }); -} - export function toArray(x: T | T[] | undefined): T[] { return Array.isArray(x) ? x : x != null ? [x] : []; } diff --git a/src/server/api/openapi/description.ts b/src/server/api/openapi/description.ts index 4fff53ac9..15831b3c9 100644 --- a/src/server/api/openapi/description.ts +++ b/src/server/api/openapi/description.ts @@ -1,7 +1,5 @@ -import config from '../../../config'; import endpoints from '../endpoints'; import * as locale from '../../../../locales/'; -import { fromEntries } from '../../../prelude/array'; import { kinds as kindsList } from '../kinds'; export interface IKindInfo { @@ -10,16 +8,16 @@ export interface IKindInfo { } export function kinds() { - const kinds = fromEntries( + const kinds = Object.fromEntries( kindsList .map(k => [k, { endpoints: [], - descs: fromEntries( + descs: Object.fromEntries( Object.keys(locale) - .map(l => [l, locale[l]._permissions[k] as string] as [string, string]) - ) as { [x: string]: string; } - }] as [ string, IKindInfo ]) - ) as { [x: string]: IKindInfo; }; + .map(l => [l, locale[l]._permissions[k] as string]) + ) + } as IKindInfo]) + ); const errors = [] as string[][]; @@ -37,17 +35,17 @@ export function kinds() { } export function getDescription(lang = 'ja-JP'): string { - const permissionTable = (Object.entries(kinds()) as [string, IKindInfo][]) + const permissionTable = Object.entries(kinds()) .map(e => `|${e[0]}|${e[1].descs[lang]}|${e[1].endpoints.map(f => `[${f}](#operation/${f})`).join(', ')}|`) .join('\n'); - const descriptions = { + const descriptions: { [x: string]: string } = { 'ja-JP': ` # Permissions |Permisson (kind)|Description|Endpoints| |:--|:--|:--| ${permissionTable} ` - } as { [x: string]: string }; + }; return lang in descriptions ? descriptions[lang] : descriptions['ja-JP']; } diff --git a/tsconfig.json b/tsconfig.json index b285259bc..260968c24 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,6 +24,9 @@ "typeRoots": [ "node_modules/@types", "src/@types" + ], + "lib": [ + "esnext" ] }, "compileOnSave": false,