Skip to content

Commit

Permalink
refactor: convert func to arrow (#30)
Browse files Browse the repository at this point in the history
## Summary
converting regular functions to arrow functions and transitioning from
the export {} format to prefixing individual functions with export.
Alongside these major changes, several minor updates were made:

- Moved packages from dependencies to devDependencies in package.json.
- Updated lint scripts.
- Disabled coverage patch status.
- eslintignore yarn remove.

<!-- Please summarize your changes. -->

<!-- Please link to any applicable information (forum posts, bug
reports, etc.). -->

## Checks

<!-- For completed items, change [ ] to [x]. -->

<!-- If you leave this checklist empty, your PR will very likely be
closed. -->

Please check the following:

- [x] I have written documents and tests, if needed.
  • Loading branch information
tooooo1 authored Sep 20, 2023
1 parent fe93f59 commit 64121c7
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 51 deletions.
8 changes: 8 additions & 0 deletions .changeset/red-poets-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@fepack/eslint-config-js": patch
"@fepack/eslint-config-ts": patch
"@fepack/eslint-config": patch
"@fepack/image": patch
---

refactor: convert func to arrow
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.vscode
.yarn
dist
.eslintrc.cjs
packlint.config.mjs
5 changes: 3 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
coverage:
status:
patch: off
project:
default:
target: 100%
threshold: 100%
threshold: 50%
fepack-image:
target: 100%
threshold: 100%
threshold: 50%

comment:
layout: "header, reach, diff, flags, components"
Expand Down
2 changes: 1 addition & 1 deletion configs/eslint-config-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"lint": "eslint ."
},
"dependencies": {
"devDependencies": {
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.2.1",
Expand Down
2 changes: 1 addition & 1 deletion configs/eslint-config-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"lint": "eslint ."
},
"dependencies": {
"devDependencies": {
"@fepack/eslint-config-js": "workspace:*",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"build:tsc": "tsc --emitDeclarationOnly",
"clean": "rm -rf dist",
"copy:cts": "cp dist/index.d.ts dist/index.d.cts",
"lint": "eslint \"**/*.ts*\"",
"lint:pub": "publint --strict",
"prepack": "pnpm build",
"test": "vitest run --coverage",
"test:watch": "vitest"
Expand Down
15 changes: 6 additions & 9 deletions packages/image/src/checkWebPSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ type Feature = keyof typeof kTestImages;
* Checks if a specific WebP feature is supported by the browser.
* @param feature - The WebP feature to check for.
*/
function checkWebPFeatureSupport(feature: Feature) {
return new Promise<boolean>((resolve) => {
const checkWebPFeatureSupport = (feature: Feature) =>
new Promise<boolean>((resolve) => {
const image = new Image();
image.onload = () => resolve(image.width > 0 && image.height > 0);
image.onerror = () => resolve(false);
image.src = "data:image/webp;base64," + kTestImages[feature];
});
}

const features = Object.keys(kTestImages).filter((key): key is Feature =>
Object.prototype.hasOwnProperty.call(kTestImages, key),
Expand All @@ -33,9 +32,7 @@ const features = Object.keys(kTestImages).filter((key): key is Feature =>
/**
* Checks if the browser supports all key WebP features.
*/
async function checkWebPSupport() {
const results = await Promise.all(features.map(checkWebPFeatureSupport));
return results.every(Boolean);
}

export default checkWebPSupport;
export const checkWebPSupport = () =>
Promise.all(features.map(checkWebPFeatureSupport)).then((results) =>
results.every(Boolean),
);
4 changes: 2 additions & 2 deletions packages/image/src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const FILE_TYPE_KEYS = Object.keys(
* @param {Buffer} buffer - The buffer containing the file's first few bytes.
* @returns The detected MIME type or null if no known signature is matched.
*/
export function detect(buffer: Buffer) {
export const detect = (buffer: Buffer) => {
for (const key of FILE_TYPE_KEYS) {
const { mime, signature } = FILE_TYPES[key];

Expand All @@ -54,4 +54,4 @@ export function detect(buffer: Buffer) {
}

return null;
}
};
2 changes: 1 addition & 1 deletion packages/image/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as checkWebPSupport } from "./checkWebPSupport";
export { checkWebPSupport } from "./checkWebPSupport";
export { detect } from "./detect";
export { load, type ImageSource } from "./load";
8 changes: 3 additions & 5 deletions packages/image/src/load.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface ImageSource {
export interface ImageSource {
defaultSrc: string;
webpSrc?: string;
}
Expand All @@ -7,11 +7,9 @@ interface ImageSource {
* Loads the given images. If WebP is WebP source is provided, it will load that. Otherwise, it loads the default source.
* @param {ImageSource[]} images - Array of image sources to preload.
*/
function load(images: ImageSource[]) {
export const load = (images: ImageSource[]) => {
for (const image of images) {
const imageElement = new Image();
imageElement.src = image.webpSrc ? image.webpSrc : image.defaultSrc;
}
}

export { load, type ImageSource };
};
Loading

0 comments on commit 64121c7

Please sign in to comment.