-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 727440c
Showing
12 changed files
with
4,786 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
## Code of Conduct | ||
|
||
### Our Pledge | ||
|
||
In the interest of fostering an open and welcoming environment, we as | ||
contributors and maintainers pledge to making participation in our project and | ||
our community a harassment-free experience for everyone, regardless of age, body | ||
size, disability, ethnicity, gender identity and expression, level of experience, | ||
nationality, personal appearance, race, religion, or sexual identity and | ||
orientation. | ||
|
||
### Our Standards | ||
|
||
Examples of behavior that contributes to creating a positive environment | ||
include: | ||
|
||
- Using welcoming and inclusive language | ||
- Being respectful of differing viewpoints and experiences | ||
- Gracefully accepting constructive criticism | ||
- Focusing on what is best for the community | ||
- Showing empathy towards other community members | ||
|
||
Examples of unacceptable behavior by participants include: | ||
|
||
- The use of sexualized language or imagery and unwelcome sexual attention or | ||
advances | ||
- Trolling, insulting/derogatory comments, and personal or political attacks | ||
- Public or private harassment | ||
- Publishing others' private information, such as a physical or electronic | ||
address, without explicit permission | ||
- Other conduct which could reasonably be considered inappropriate in a | ||
professional setting | ||
|
||
### Our Responsibilities | ||
|
||
Project maintainers are responsible for clarifying the standards of acceptable | ||
behavior and are expected to take appropriate and fair corrective action in | ||
response to any instances of unacceptable behavior. | ||
|
||
Project maintainers have the right and responsibility to remove, edit, or | ||
reject comments, commits, code, wiki edits, issues, and other contributions | ||
that are not aligned to this Code of Conduct, or to ban temporarily or | ||
permanently any contributor for other behaviors that they deem inappropriate, | ||
threatening, offensive, or harmful. | ||
|
||
### Scope | ||
|
||
This Code of Conduct applies both within project spaces and in public spaces | ||
when an individual is representing the project or its community. Examples of | ||
representing a project or community include using an official project e-mail | ||
address, posting via an official social media account, or acting as an appointed | ||
representative at an online or offline event. Representation of a project may be | ||
further defined and clarified by project maintainers. | ||
|
||
### Enforcement | ||
|
||
Instances of abusive, harassing, or otherwise unacceptable behavior may be | ||
reported by contacting the project team at `[email protected]`. All | ||
complaints will be reviewed and investigated and will result in a response that | ||
is deemed necessary and appropriate to the circumstances. The project team is | ||
obligated to maintain confidentiality with regard to the reporter of an incident. | ||
Further details of specific enforcement policies may be posted separately. | ||
|
||
Project maintainers who do not follow or enforce the Code of Conduct in good | ||
faith may face temporary or permanent repercussions as determined by other | ||
members of the project's leadership. | ||
|
||
### Attribution | ||
|
||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, | ||
available at [http://contributor-covenant.org/version/1/4][version] | ||
|
||
[homepage]: http://contributor-covenant.org | ||
[version]: http://contributor-covenant.org/version/1/4/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# This workflow will do a clean install of node dependencies, build the source code and run test | ||
# For more information see: https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-nodejs | ||
|
||
name: Node.js CI | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.x" | ||
- run: npm ci | ||
- run: npm run build --if-present | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This workflow will publish a package to NPM when a release is published | ||
# For more information see: https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-nodejs-packages#publishing-packages-to-the-npm-registry | ||
|
||
name: Publish Package to npmjs | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
# Setup .npmrc file to publish to npm | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.x" | ||
registry-url: "https://registry.npmjs.org" | ||
- run: npm ci | ||
- run: npm publish --provenance --access public | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.nyc_output | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import globals from "globals"; | ||
import pluginJs from "@eslint/js"; | ||
import tseslint from "typescript-eslint"; | ||
import eslintPluginUnicorn from "eslint-plugin-unicorn"; | ||
|
||
export default [ | ||
{ files: ["**/*.{js,mjs,cjs,ts}"] }, | ||
{ | ||
languageOptions: { globals: { ...globals.browser, ...globals.node } }, | ||
}, | ||
pluginJs.configs.recommended, | ||
...tseslint.configs.recommended, | ||
eslintPluginUnicorn.configs["flat/recommended"], | ||
{ | ||
rules: { | ||
"unicorn/prevent-abbreviations": "off", | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import assert from "node:assert"; | ||
import { PreferredSize, strapiOptimizedImage } from "."; | ||
|
||
describe("Preferred size enum", () => { | ||
it("should return thumbnail", () => { | ||
assert.equal(PreferredSize.Thumbnail, "thumbnail"); | ||
}); | ||
|
||
it("should return small", () => { | ||
assert.equal(PreferredSize.Small, "small"); | ||
}); | ||
|
||
it("should return medium", () => { | ||
assert.equal(PreferredSize.Medium, "medium"); | ||
}); | ||
|
||
it("should return large", () => { | ||
assert.equal(PreferredSize.Large, "large"); | ||
}); | ||
}); | ||
|
||
const originalImage = { | ||
height: 720, | ||
url: "https://example.com/logo.svg", | ||
width: 1280, | ||
}; | ||
const thumbnail = { | ||
height: 180, | ||
url: "https://example.com/logo_thumbnail.svg", | ||
width: 320, | ||
}; | ||
const small = { | ||
height: 225, | ||
url: "https://example.com/logo_small.svg", | ||
width: 400, | ||
}; | ||
const medium = { | ||
height: 450, | ||
url: "https://example.com/logo_medium.svg", | ||
width: 800, | ||
}; | ||
const large = { | ||
height: 900, | ||
url: "https://example.com/logo_large.svg", | ||
width: 1600, | ||
}; | ||
|
||
describe("Strapi Optimized image", () => { | ||
it("should return original image (default parameters)", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: {}, | ||
}, | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: originalImage }); | ||
}); | ||
|
||
it("should return original image (fallback to smaller)", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: {}, | ||
}, | ||
fallback: "smaller", | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: originalImage }); | ||
}); | ||
|
||
it("should return original image (fallback to smaller & preferred size is large)", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: {}, | ||
}, | ||
fallback: "smaller", | ||
preferredSize: PreferredSize.Large, | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: originalImage }); | ||
}); | ||
|
||
it("should return large image", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: { large, medium, small, thumbnail }, | ||
}, | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: large }); | ||
}); | ||
|
||
it("should return large image (fallback to smaller)", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: { large, medium, small, thumbnail }, | ||
}, | ||
fallback: "smaller", | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: large }); | ||
}); | ||
|
||
it("should return large image (fallback to smaller & preferred size is large)", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: { large, medium, small, thumbnail }, | ||
}, | ||
fallback: "smaller", | ||
preferredSize: PreferredSize.Large, | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: large }); | ||
}); | ||
|
||
it("should return smaller image possible", () => { | ||
// arrange and act | ||
const optimizedImage = strapiOptimizedImage({ | ||
image: { | ||
...originalImage, | ||
formats: { thumbnail }, | ||
}, | ||
fallback: "smaller", | ||
}); | ||
|
||
// assert | ||
assertOptimizedImage({ optimizedImage, expectedImage: thumbnail }); | ||
}); | ||
}); | ||
|
||
type StrapiImage = { | ||
height: number; | ||
url: string; | ||
width: number; | ||
}; | ||
|
||
type OptimizedImage = { | ||
src: string; | ||
} & Omit<StrapiImage, "url">; | ||
|
||
type AssertOptimizedImageParameters = { | ||
optimizedImage: OptimizedImage; | ||
expectedImage: StrapiImage; | ||
}; | ||
|
||
function assertOptimizedImage({ | ||
optimizedImage, | ||
expectedImage, | ||
}: AssertOptimizedImageParameters) { | ||
assert.equal(optimizedImage.height, expectedImage.height); | ||
assert.equal(optimizedImage.src, expectedImage.url); | ||
assert.equal(optimizedImage.width, expectedImage.width); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
type StrapiImage = { | ||
height: number; | ||
url: string; | ||
width: number; | ||
}; | ||
|
||
type StrapiImageFormats = { | ||
thumbnail?: StrapiImage; | ||
small?: StrapiImage; | ||
medium?: StrapiImage; | ||
large?: StrapiImage; | ||
}; | ||
|
||
export enum PreferredSize { | ||
Thumbnail = "thumbnail", | ||
Small = "small", | ||
Medium = "medium", | ||
Large = "large", | ||
} | ||
|
||
type GetOptimizedImageParameters = { | ||
image: { formats: StrapiImageFormats } & StrapiImage; | ||
fallback?: "original" | "smaller"; | ||
preferredSize?: PreferredSize; | ||
}; | ||
|
||
type OptimizedImage = { | ||
src: string; | ||
} & Omit<StrapiImage, "url">; | ||
|
||
export function strapiOptimizedImage({ | ||
image: { formats, height, url: src, width }, | ||
fallback = "original", | ||
preferredSize = PreferredSize.Large, | ||
}: GetOptimizedImageParameters): OptimizedImage { | ||
if (!formats?.[preferredSize]) { | ||
if (fallback === "original") { | ||
return { height, src, width }; | ||
} | ||
|
||
const image = smallerImage(formats, preferredSize); | ||
|
||
if (!image) { | ||
return { height, src, width }; | ||
} | ||
|
||
return image; | ||
} | ||
|
||
return { | ||
height: formats[preferredSize].height, | ||
src: formats[preferredSize].url, | ||
width: formats[preferredSize].width, | ||
}; | ||
} | ||
|
||
function smallerImage( | ||
formats: StrapiImageFormats, | ||
preferredSize: PreferredSize, | ||
): OptimizedImage | undefined { | ||
const preferredSizes = Object.values(PreferredSize); | ||
const end = preferredSizes.indexOf(preferredSize); | ||
for (const ps of preferredSizes.slice(0, end)) { | ||
if (formats[ps]) { | ||
const { height, url: src, width } = formats[ps]; | ||
|
||
return { height, src, width }; | ||
} | ||
} | ||
|
||
return undefined; | ||
} |
Oops, something went wrong.