From 046b85d057885218325a447ea4256a00fddc3eeb Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Thu, 27 Oct 2022 15:33:19 +0200 Subject: [PATCH] Daily random projects --- package-lock.json | 43 +++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ pages/index.tsx | 25 ++++++++++++++++--------- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index ac40007..b78d8af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "next": "12.1.0", "next-themes": "^0.2.1", "node-fetch": "^3.2.0", + "random-seed": "^0.3.0", "react": "17.0.2", "react-dom": "17.0.2", "sharp": "^0.31.1" @@ -24,6 +25,7 @@ "@types/color": "^3.0.3", "@types/jsdom": "^20.0.0", "@types/node": "16.11.11", + "@types/random-seed": "^0.3.3", "@types/react": "17.0.37", "@types/react-dom": "^18.0.5", "eslint": "7.32.0", @@ -655,6 +657,12 @@ "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==", "dev": true }, + "node_modules/@types/random-seed": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/@types/random-seed/-/random-seed-0.3.3.tgz", + "integrity": "sha512-kHsCbIRHNXJo6EN5W8EA5b4i1hdT6jaZke5crBPLUcLqaLdZ0QBq8QVMbafHzhjFF83Cl9qlee2dChD18d/kPg==", + "dev": true + }, "node_modules/@types/react": { "version": "17.0.37", "resolved": "https://registry.npmjs.org/@types/react/-/react-17.0.37.tgz", @@ -3290,6 +3298,11 @@ "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, + "node_modules/json-stringify-safe": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", + "integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==" + }, "node_modules/json5": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", @@ -4032,6 +4045,17 @@ } ] }, + "node_modules/random-seed": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/random-seed/-/random-seed-0.3.0.tgz", + "integrity": "sha512-y13xtn3kcTlLub3HKWXxJNeC2qK4mB59evwZ5EkeRlolx+Bp2ztF7LbcZmyCnOqlHQrLnfuNbi1sVmm9lPDlDA==", + "dependencies": { + "json-stringify-safe": "^5.0.1" + }, + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -5774,6 +5798,12 @@ "integrity": "sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==", "dev": true }, + "@types/random-seed": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/@types/random-seed/-/random-seed-0.3.3.tgz", + "integrity": "sha512-kHsCbIRHNXJo6EN5W8EA5b4i1hdT6jaZke5crBPLUcLqaLdZ0QBq8QVMbafHzhjFF83Cl9qlee2dChD18d/kPg==", + "dev": true + }, "@types/react": { "version": "17.0.37", "resolved": "https://registry.npmjs.org/@types/react/-/react-17.0.37.tgz", @@ -7733,6 +7763,11 @@ "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, + "json-stringify-safe": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", + "integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==" + }, "json5": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", @@ -8259,6 +8294,14 @@ "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", "dev": true }, + "random-seed": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/random-seed/-/random-seed-0.3.0.tgz", + "integrity": "sha512-y13xtn3kcTlLub3HKWXxJNeC2qK4mB59evwZ5EkeRlolx+Bp2ztF7LbcZmyCnOqlHQrLnfuNbi1sVmm9lPDlDA==", + "requires": { + "json-stringify-safe": "^5.0.1" + } + }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", diff --git a/package.json b/package.json index 2b74779..ac58da4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "next": "12.1.0", "next-themes": "^0.2.1", "node-fetch": "^3.2.0", + "random-seed": "^0.3.0", "react": "17.0.2", "react-dom": "17.0.2", "sharp": "^0.31.1" @@ -26,6 +27,7 @@ "@types/color": "^3.0.3", "@types/jsdom": "^20.0.0", "@types/node": "16.11.11", + "@types/random-seed": "^0.3.3", "@types/react": "17.0.37", "@types/react-dom": "^18.0.5", "eslint": "7.32.0", diff --git a/pages/index.tsx b/pages/index.tsx index f8be2c3..f4e6666 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,4 +1,5 @@ import type { NextPage } from "next"; +import gen from "random-seed"; import Layout from "../components/Blog/Layout"; import type { ContentList, Project, Diary } from "../lib/content/types"; import ProjectCard from "../components/Blog/Card"; @@ -6,14 +7,6 @@ import { getContentList } from "../lib/content/generateBackend"; import styles from "../styles/Blog/Front.module.scss"; -// https://stackoverflow.com/a/6274381 -function shuffle(a: any[]) { - for (let i = a.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [a[i], a[j]] = [a[j], a[i]]; - } - return a; -} const Blog: NextPage<{ content: ContentList }> = ({content}) => { const clearDescription = (description: string) => { @@ -23,9 +16,23 @@ const Blog: NextPage<{ content: ContentList }> = ({content}) => { return description.replace(linkRegex, "$1").replace(cmdRegex, "\"$1\""); }; + const shuffleArray = (arr: any[]) => { + // We want shuffle but only between days + const date = new Date(); + const generator = gen.create(`${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`); + + // https://stackoverflow.com/a/6274381 + for (let i = arr.length - 1; i > 0; i--) { + const j = generator.intBetween(0, i); + [arr[i], arr[j]] = [arr[j], arr[i]]; + } + generator.done(); + return arr; + }; + const generateCards = (type: string) => { return
{ - (shuffle(content.filter(p => p.type === type)) as (Project|Diary)[]) + (shuffleArray(content.filter(p => p.type === type)) as (Project|Diary)[]) .map(p => )}