Skip to content

Commit

Permalink
chore: optimize libs
Browse files Browse the repository at this point in the history
  • Loading branch information
flamrdevs committed Dec 25, 2023
1 parent 400e3fc commit b4ca20d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/libs/bundlejs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as v from 'valibot';

import * as fetchs from './fetchs';
import * as ftch from './ftch';

import { getPackageItem } from './npm';
import { memo } from './utils';
Expand Down Expand Up @@ -30,7 +30,7 @@ const getBundleItem = (name: string): Promise<BundleItem> =>
if (npm.peerDependencies) external.push(...Object.keys(npm.peerDependencies));
const config = { esbuild: { external } };

return v.parse(BundleItemSchema, await fetchs.get.json(`https://deno.bundlejs.com/?q=${name}&config=${JSON.stringify(config)}`));
return v.parse(BundleItemSchema, await ftch.get.json(`https://deno.bundlejs.com/?q=${name}&config=${JSON.stringify(config)}`));
});

export { getBundleItem };
File renamed without changes.
4 changes: 2 additions & 2 deletions src/libs/github.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as v from 'valibot';

import * as fetchs from './fetchs';
import * as ftch from './ftch';

import { memo } from './utils';

Expand Down Expand Up @@ -33,7 +33,7 @@ const RepoItemSchema = v.object({
const loadRepoItem = memo<RepoItem>();

const getRepoItem = (user: string, repo: string): Promise<RepoItem> =>
loadRepoItem([user, repo].join('/'), async () => v.parse(RepoItemSchema, await fetchs.get.json(`https://api.github.com/repos/${user}/${repo}`)));
loadRepoItem([user, repo].join('/'), async () => v.parse(RepoItemSchema, await ftch.get.json(`https://api.github.com/repos/${user}/${repo}`)));

const formatCount = (number: number): string => (number < 1000 ? number.toString() : number < 1000000 ? (number / 1000).toFixed(1) + 'K' : (number / 1000000).toFixed(1) + 'M');

Expand Down
8 changes: 4 additions & 4 deletions src/libs/npm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as v from 'valibot';

import * as fetchs from './fetchs';
import * as ftch from './ftch';

import { memo } from './utils';

Expand Down Expand Up @@ -33,11 +33,11 @@ const loadPackageItem = memo<PackageItem>();
const loadDownloadPointWeekItem = memo<DownloadPointItem>();
const loadDownloadPointMonthItem = memo<DownloadPointItem>();

const getPackageItem = (name: string): Promise<PackageItem> => loadPackageItem(name, async () => v.parse(PackageItemSchema, await fetchs.get.json(`https://registry.npmjs.org/${name}/latest`)));
const getPackageItem = (name: string): Promise<PackageItem> => loadPackageItem(name, async () => v.parse(PackageItemSchema, await ftch.get.json(`https://registry.npmjs.org/${name}/latest`)));
const getDownloadPointWeekItem = (name: string): Promise<DownloadPointItem> =>
loadDownloadPointWeekItem(name, async () => v.parse(DownloadPointItemSchema, await fetchs.get.json(`https://api.npmjs.org/downloads/point/last-week/${name}`)));
loadDownloadPointWeekItem(name, async () => v.parse(DownloadPointItemSchema, await ftch.get.json(`https://api.npmjs.org/downloads/point/last-week/${name}`)));
const getDownloadPointMonthItem = (name: string): Promise<DownloadPointItem> =>
loadDownloadPointMonthItem(name, async () => v.parse(DownloadPointItemSchema, await fetchs.get.json(`https://api.npmjs.org/downloads/point/last-month/${name}`)));
loadDownloadPointMonthItem(name, async () => v.parse(DownloadPointItemSchema, await ftch.get.json(`https://api.npmjs.org/downloads/point/last-month/${name}`)));

const formatDownloads = (number: number): string => (number < 1000 ? number.toString() : number < 1000000 ? (number / 1000).toFixed(1) + 'K' : (number / 1000000).toFixed(1) + 'M');

Expand Down
44 changes: 25 additions & 19 deletions src/libs/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ type SafeProps<P extends Record<string, unknown>> = P & { [key: string]: unknown
type SafeCSSValue<T extends string | number = string | number> = T | (string & {}) | (number & {});

type CSSProperties = Partial<{
display: SafeCSSValue<'none' | 'flex'>;
position: SafeCSSValue<'absolute' | 'relative'>;
display: 'none' | 'flex';
position: 'absolute' | 'relative';
top: SafeCSSValue;
right: SafeCSSValue;
bottom: SafeCSSValue;
left: SafeCSSValue;
justifyContent: SafeCSSValue<'normal' | 'center' | 'flex-start' | 'flex-end' | 'space-between' | 'space-around' | 'space-evenly'>;
alignItems: SafeCSSValue<'normal' | 'stretch' | 'center' | 'flex-start' | 'flex-end' | 'self-start' | 'self-end'>;
flexDirection: SafeCSSValue<'row' | 'column' | 'row-reverse' | 'column-reverse'>;
justifyContent: SafeCSSValue<'center' | 'flex-start' | 'flex-end' | 'space-between' | 'space-around' | 'space-evenly'>;
alignItems: 'normal' | 'stretch' | 'center' | 'flex-start' | 'flex-end' | 'baseline';
flexDirection: 'row' | 'column' | 'row-reverse' | 'column-reverse';
flexGrow: SafeCSSValue;
flexShrink: SafeCSSValue;
flexWrap: SafeCSSValue<'nowrap' | 'wrap' | 'wrap-reverse'>;
flexWrap: 'nowrap' | 'wrap' | 'wrap-reverse';
gap: SafeCSSValue;
overflow: SafeCSSValue<'visible' | 'hidden'>;
overflow: 'visible' | 'hidden';
margin: SafeCSSValue;
marginTop: SafeCSSValue;
marginRight: SafeCSSValue;
Expand All @@ -48,11 +48,11 @@ type CSSProperties = Partial<{
borderBottomColor: SafeCSSValue;
borderRightColor: SafeCSSValue;
borderLeftColor: SafeCSSValue;
borderStyle: SafeCSSValue<'solid' | 'dashed'>;
borderTopStyle: SafeCSSValue<'solid' | 'dashed'>;
borderBottomStyle: SafeCSSValue<'solid' | 'dashed'>;
borderRightStyle: SafeCSSValue<'solid' | 'dashed'>;
borderLeftStyle: SafeCSSValue<'solid' | 'dashed'>;
borderStyle: 'solid' | 'dashed';
borderTopStyle: 'solid' | 'dashed';
borderBottomStyle: 'solid' | 'dashed';
borderRightStyle: 'solid' | 'dashed';
borderLeftStyle: 'solid' | 'dashed';
borderWidth: SafeCSSValue;
borderTopWidth: SafeCSSValue;
borderBottomWidth: SafeCSSValue;
Expand All @@ -67,19 +67,21 @@ type CSSProperties = Partial<{
backgroundImage: SafeCSSValue;
backgroundPosition: SafeCSSValue;
backgroundSize: SafeCSSValue;
backgroundClip: SafeCSSValue;
backgroundRepeat: SafeCSSValue;
color: SafeCSSValue;
backgroundClip: 'border-box' | 'text';
backgroundRepeat: 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
color: SafeCSSValue<'currentcolor'>;
opacity: SafeCSSValue;
fontFamily: SafeCSSValue;
fontSize: SafeCSSValue;
fontStyle: SafeCSSValue<'normal'>;
fontWeight: SafeCSSValue;
textAlign: SafeCSSValue<'start' | 'end' | 'center' | 'left' | 'right'>;
textTransform: SafeCSSValue<'none' | 'lowercase' | 'uppercase' | 'capitalize'>;
fontWeight: SafeCSSValue<300 | 400 | 500 | 600 | 700>;
textAlign: 'start' | 'end' | 'center' | 'left' | 'right' | 'justiy';
textTransform: 'none' | 'lowercase' | 'uppercase' | 'capitalize';
textOverflow: 'clip' | 'ellipsis';
textDecoration: SafeCSSValue<'underline' | 'line-through'>;
textShadow: SafeCSSValue;
textWrap: SafeCSSValue<'wrap' | 'balance'>;
textWrap: 'wrap' | 'balance';
lineHeight: SafeCSSValue;
filter: SafeCSSValue;
boxShadow: SafeCSSValue;
lineClamp: SafeCSSValue;
Expand All @@ -94,6 +96,10 @@ type CSSProperties = Partial<{
skew: SafeCSSValue;
skewX: SafeCSSValue;
skewY: SafeCSSValue;
maskImage: SafeCSSValue;
maskPosition: SafeCSSValue;
maskSize: SafeCSSValue;
maskRepeat: 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
}>;
type RootCSSProperties = Omit<CSSProperties, 'width' | 'height'> & { width: number; height: number };

Expand Down
19 changes: 4 additions & 15 deletions src/libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,14 @@ const memo = <T>() => {
const wait = (key: string): Promise<void> => {
return new Promise((resolve) => {
const id = setInterval(() => {
if (!loading[key]) {
clearInterval(id);
resolve();
}
}, 50);
if (!loading[key]) clearInterval(id), resolve();
}, 10);
});
};

const load = async (key: string, fn: () => Promise<T>) => {
loading[key] = true;
const result = await fn();
loading[key] = false;
return result;
};
const load = async (key: string, fn: () => Promise<T>, result?: unknown) => ((loading[key] = true), (result = await fn()), (loading[key] = false), result) as T;

return async (key: string, fn: () => Promise<T>) => {
await wait(key);
return (state[key] ??= await load(key, fn));
};
return async (key: string, fn: () => Promise<T>) => (await wait(key), (state[key] ??= await load(key, fn)));
};

export { memo };

0 comments on commit b4ca20d

Please sign in to comment.