Skip to content

Instantly share code, notes, and snippets.

@jyc
Last active November 16, 2022 20:41
Show Gist options
  • Save jyc/189f8cd546b44e8ae4991e036e750126 to your computer and use it in GitHub Desktop.
Save jyc/189f8cd546b44e8ae4991e036e750126 to your computer and use it in GitHub Desktop.
function isLeapYear(date) {
var year = date.getFullYear();
if((year & 3) != 0) return false;
return ((year % 100) != 0 || (year % 400) == 0);
};
// https://stackoverflow.com/a/26426761
function getDOY(date) {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = date.getMonth();
var dn = date.getDate();
var dayOfYear = dayCount[mn] + dn;
if(mn > 1 && isLeapYear(date)) dayOfYear++;
return dayOfYear;
};
function cardOfTheWeek(date) {
const days = getDOY(date);
const week = Math.floor(days / 7);
if (week == 52) {
// actually the white joker, because the black joker is rendered as an emoji
// color: black though because the text is on a white background
return { glyph: "🃟", text: "Jkr", color: "black"};
} else {
const SUITS = "♦♣♥♠"; // Big 2 instead of Poker for alternating colors
const CARDS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const CLUBS_OFFSET = 0x1F0D1;
const DIAMONDS_OFFSET = 0x1F0C1;
const HEARTS_OFFSET = 0x1F0B1;
const SPADES_OFFSET = 0x1F0A1;
const SUIT_OFFSETS = [DIAMONDS_OFFSET, CLUBS_OFFSET, HEARTS_OFFSET, SPADES_OFFSET];
const suitIdx = Math.floor(week / 13);
const cardIdx = week % 13
const glyph = String.fromCodePoint(SUIT_OFFSETS[suitIdx] + cardIdx);
const text = CARDS[cardIdx] + SUITS[suitIdx];
return { glyph, text, color: suitIdx % 2 ? "black" : "red" };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment