Skip to content

Commit

Permalink
Add redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
benborgers committed Mar 26, 2024
1 parent 5d5f102 commit a489a1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# emojicdn

`emoji.json` from: https://github.com/iamcal/emoji-data/blob/master/emoji.json
1 change: 1 addition & 0 deletions emoji.json

Large diffs are not rendered by default.

57 changes: 22 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import emoji from "./emoji.json";

const leftPad = (string, length, character) => {
return string.length >= length
? string
: new Array(length - string.length + 1).join(character) + string;
};

export default {
async fetch(request) {
const url = new URL(request.url);
Expand All @@ -11,42 +19,21 @@ export default {
return new Response("");
}

function emojiToUnicodeCodePoints(emoji) {
// Get the code point for the emoji itself, in hexadecimal
let baseCodePoint = emoji.codePointAt(0).toString(16).toUpperCase();

// Initialize the result with the base emoji code point
let result = baseCodePoint;

// Check if there's a variation selector or another part (e.g., skin tone modifiers)
// Since an emoji can be composed of multiple code points, we use `Array.from` to split it properly
if (Array.from(emoji).length > 1) {
// Iterate through each character (considering surrogate pairs) in the emoji string after the first character
for (let i = 1; i < Array.from(emoji).length; i++) {
// Get the code point of the current part of the emoji, in hexadecimal
let additionalCodePoint = emoji
.codePointAt(i)
.toString(16)
.toUpperCase();

// Append the additional code point to the result string, with a dash
result += "-" + additionalCodePoint;

// Consider emojis that might be represented by more than one code point
// Adjust the loop counter by the number of code units the current code point takes up
// This is a precaution if emojis in future Unicode versions take up more than 2 code units
if (emoji.codePointAt(i) > 0xffff) {
i++; // Skip the next because we already handled a surrogate pair
}
}
}

return result;
}
const emojiText = decodeURIComponent(path);
const code = Array.from(emojiText)
.map((char) => leftPad(char.codePointAt(0).toString(16), 4, "0"))
.join("-");

const emojiData = emoji.find(
(e) => e.unified.toLowerCase() === code.toLowerCase()
);

console.log(decodeURIComponent(path));
console.log("HERE: " + emojiToUnicodeCodePoints(decodeURIComponent(path)));
if (!emojiData) {
return new Response("Emoji not found", { status: 404 });
}

return Response.json({});
return Response.redirect(
`https://cdn.jsdelivr.net/gh/iamcal/emoji-data/img-apple-160/${emojiData.image}`
);
},
};

0 comments on commit a489a1f

Please sign in to comment.