UNPKG

who-asked

Version:

A tiny package to deliver sarcastic replies, memes, or GIFs for when no one asked — but your product still has to say something.

238 lines (234 loc) 6.16 kB
// data/text.json var text_default = [ { text: "Did I miss the part where I asked for your opinion?", context: ["unsolicited_opinion", "irrelevant_comment", "social_media_comeback"] }, { text: "I've been searching for who asked. Even Google couldn't find them.", context: ["search_loader", "404_not_found", "irrelevant_comment"] }, { text: "Hold on, let me check my notes... aaaaand, nobody asked.", context: ["playful_banter", "social_media_comeback", "unsolicited_opinion"] }, { text: "Your opinion is noted. And filed under 'Things I Didn't Ask For'.", context: ["unsolicited_opinion", "irrelevant_comment"] }, { text: "My ancestors fought for my right to speak, not for you to ask if anyone asked.", context: ["social_media_comeback", "playful_banter"] }, { text: "404: The person who asked was not found.", context: ["404_not_found", "irrelevant_comment"] }, { text: "Was there a 'please interrupt me' sign I was unaware of?", context: ["conversation_interjection", "unsolicited_opinion"] }, { text: "I'm sorry, I must have mistaken you for someone who I asked.", context: ["unsolicited_opinion", "playful_banter"] }, { text: "The voices in my head didn't ask, and they're the only ones I'm obligated to listen to.", context: ["playful_banter", "social_media_comeback"] }, { text: "If I wanted your opinion, I would have given it to you.", context: ["unsolicited_opinion", "social_media_comeback"] }, { text: "The council has reviewed your statement and has concluded that nobody, in fact, asked.", context: ["irrelevant_comment", "social_media_comeback", "playful_banter"] }, { text: "Are you the human embodiment of an unskippable ad?", context: ["conversation_interjection", "irrelevant_comment"] }, { text: "I'm currently accepting applications for 'who asked'. You are not qualified.", context: ["unsolicited_opinion", "social_media_comeback"] }, { text: "Paging... paging anyone who asked... The line seems to be dead.", context: ["search_loader", "404_not_found", "playful_banter"] }, { text: "Your contribution has been noted and will be ignored.", context: ["irrelevant_comment", "unsolicited_opinion"] } ]; // data/media.json var media_default = [ { uri: "amogus.jpg", width: 423, height: 640, type: "image" }, { uri: "car-drive-search.png", width: 535, height: 969, type: "image" }, { uri: "cat.jpg", width: 719, height: 1406, type: "image" }, { uri: "empty-group.png", width: 1124, height: 740, type: "image" }, { uri: "finding-nemo.png", width: 2654, height: 1480, type: "image" }, { uri: "grover.jpg", width: 222, height: 227, type: "image" }, { uri: "minecraft-renderdragon.jpg", width: 1242, height: 664, type: "image" }, { uri: "satellite-search.png", width: 498, height: 375, type: "image" }, { uri: "benjammins-search.gif", width: 498, height: 498, type: "gif" }, { uri: "jameswebb-search.gif", width: 640, height: 526, type: "gif" }, { uri: "jet-search.gif", width: 640, height: 360, type: "gif" }, { uri: "monkey-surprised.gif", width: 512, height: 640, type: "gif" }, { uri: "satellite-search.gif", width: 640, height: 394, type: "gif" }, { uri: "spongebob-dance.gif", width: 640, height: 640, type: "gif" }, { uri: "spongebob-search.gif", width: 640, height: 354, type: "gif" }, { uri: "variouslocations-search.gif", width: 640, height: 480, type: "gif" }, { uri: "weeknd-search.gif", width: 640, height: 356, type: "gif" } ]; // src/utils.ts var generateTagsFromUri = (uri) => { const nameWithoutExtension = uri.substring(0, uri.lastIndexOf(".")); const tags = nameWithoutExtension.split("-").filter((tag) => tag.length > 0); return tags; }; var mediaWithTags = media_default.map((item) => ({ ...item, uri: `https://ik.imagekit.io/jayowiee/who-asked-${item.type === "image" ? "img" : "gif"}/${item.uri}`, tags: generateTagsFromUri(item.uri) })); var textData = text_default; var imgData = mediaWithTags.filter((item) => item.type === "image"); var gifData = mediaWithTags.filter((item) => item.type === "gif"); var getRandomText = () => { return textData[Math.floor(Math.random() * textData.length)]; }; var getRandomImage = () => { return imgData[Math.floor(Math.random() * imgData.length)]; }; var getRandomGif = () => { return gifData[Math.floor(Math.random() * gifData.length)]; }; var matchesTags = (tags, params) => { return params.some( (param) => tags.some( (tag) => tag.toLowerCase().includes(param.toLowerCase()) || param.toLowerCase().includes(tag.toLowerCase()) ) ); }; var text = (params) => { const filtered = textData.filter((item) => matchesTags(item.context, params)); return filtered.length > 0 ? filtered[Math.floor(Math.random() * filtered.length)] : getRandomText(); }; var image = (params) => { const filtered = imgData.filter((item) => matchesTags(item.tags, params)); return filtered.length > 0 ? filtered[Math.floor(Math.random() * filtered.length)] : getRandomImage(); }; var gif = (params) => { const filtered = gifData.filter((item) => matchesTags(item.tags, params)); return filtered.length > 0 ? filtered[Math.floor(Math.random() * filtered.length)] : getRandomGif(); }; // src/index.ts var whoAsked = (type, params) => { const normalizedType = type.toLowerCase(); switch (normalizedType) { case "text": return params ? text(params) : getRandomText(); case "image": case "img": return params ? image(params) : getRandomImage(); case "gif": return params ? gif(params) : getRandomGif(); default: throw new Error(`Invalid type: ${type}. Valid types are: text, image, img, gif`); } }; export { getRandomGif, getRandomImage, getRandomText, gif, image, text, whoAsked };