Skip to content

Commit

Permalink
0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
alesvaupotic committed Apr 26, 2021
1 parent 896137f commit dcf5fb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
35 changes: 14 additions & 21 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ function log(param: any) {
}

// ----------------------------------------------------------------------------
function avif(url: string, size: number) {
// converts url string to passed extension, like jpg to avif or png to webp
// prepends size if passed for srcset, responsive images
function imgExtension(toExtension: string, url: string, size?: number) {
let s = url;
if (size) {
const fname = s.split("/").pop().split("/").pop();
s = s.replace(fname, `${size}/${fname}`);
}

if (s.endsWith("png")) {
return s.split(".png").join(".avif");
return s.split(".png").join("." + toExtension);
}
if (s.endsWith("jpg")) {
return s.split(".jpg").join(".avif");
return s.split(".jpg").join("." + toExtension);
}
if (s.endsWith("jpeg")) {
return s.split(".jpeg").join(".avif");
return s.split(".jpeg").join("." + toExtension);
}
}

// ----------------------------------------------------------------------------
function jpeg(url: string, size: number) {
function img(url: string, size?: number) {
let s = url;
if (size) {
const fname = s.split("/").pop().split("/").pop();
Expand All @@ -43,22 +45,13 @@ function jpeg(url: string, size: number) {
}

// ----------------------------------------------------------------------------
function webp(url: string, size: number) {
let s = url;
if (size) {
const fname = s.split("/").pop().split("/").pop();
s = s.replace(fname, `${size}/${fname}`);
}
function avif(url: string, size?: number) {
return imgExtension("avif", url, size);
}

if (s.endsWith("png")) {
return s.split(".png").join(".webp");
}
if (s.endsWith("jpg")) {
return s.split(".jpg").join(".webp");
}
if (s.endsWith("jpeg")) {
return s.split(".jpeg").join(".webp");
}
// ----------------------------------------------------------------------------
function webp(url: string, size?: number) {
return imgExtension("webp", url, size);
}

export { dateForDisplay, log, avif, jpeg, webp };
export { dateForDisplay, log, img, avif, webp };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-helpers",
"version": "0.1.8",
"version": "0.1.9",
"description": "Common JS helpers for development",
"main": "index.ts",
"scripts": {
Expand Down

0 comments on commit dcf5fb5

Please sign in to comment.