Skip to content

Instantly share code, notes, and snippets.

View perjo927's full-sized avatar
πŸ§‘β€πŸ’»

Per Jonsson perjo927

πŸ§‘β€πŸ’»
  • DevCode
  • Stockholm, Sweden
View GitHub Profile
@perjo927
perjo927 / business-logic.js
Created September 8, 2020 16:38
Business Logic
import { CONSTS } from "../redux/actions/index.js";
const { ALL, DONE, IN_PROGRESS } = CONSTS.visibilityFilters;
export const makeRegrettable = (store, actions) => ({
undo() {
return store.dispatch(actions.undo());
},
redo() {
return store.dispatch(actions.redo());
@perjo927
perjo927 / view-logic.js
Created September 8, 2020 16:42
View Logic
export const getAndResetInput = (e) => {
const [input] = e.target;
const { value } = input;
input.value = "";
return value;
};
export const maybeRender = (template, validator) =>
validator ? template : null;
@perjo927
perjo927 / main.css
Created September 8, 2020 16:43
Styling
body {
font: 14px "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 14px;
background: #f5f5f5;
color: #4d4d4d;
margin: 1em;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-weight: 300;
display: flex;
@perjo927
perjo927 / main.js
Created September 8, 2020 16:49
Premature Main file
import css from "./main.css";
import { createStore } from "./redux/store/index";
import { actions } from "./redux/actions/index";
import { rootReducer } from "./redux/reducers/index";
import { render } from "lit-html";
import { App } from "./templates/App";
const store = createStore(rootReducer, {});
store.subscribe(() => console.log(store.getState()));
@perjo927
perjo927 / main.js
Created September 8, 2020 16:53
Mature Main File
import css from "./main.css";
import { createStore } from "./redux/store/index";
import { actions } from "./redux/actions/index";
import { rootReducer } from "./redux/reducers/index";
import { render } from "lit-html";
import { App } from "./templates/App";
const store = createStore(rootReducer, {});
export default () => {
@perjo927
perjo927 / palindrome.js
Created September 13, 2020 10:52
Palindrome FP Validator
const isEmpty = word => word === "";
const isSingleChar = word => word.length === 1;
const isDoubleChar = word => word.length === 2;
const firstChar = word => word[0];
const lastChar = word => word.slice(-1);
const isMatch = (a,b) => a === b;
const trimEnds = word => word.slice(1,-1);
const isEndsMatched = word => isEmpty(word) || isMatch(firstChar(word), lastChar(word))
const isPalindrome = word => {
@perjo927
perjo927 / combinations.js
Last active September 25, 2021 12:05
Combine
const categorizeValues = ([category, values]) =>
values.map(value => Object.fromEntries([[category, value]]))
const combineCategories = (prev, curr) =>
curr.flatMap(currEl => prev.map(prevEl => ({...prevEl, ...currEl})))
const combinations = input =>
Object.entries(input).map(categorizeValues).reduce(combineCategories);
/*
@perjo927
perjo927 / generator-slot-game.js
Last active February 3, 2025 16:17
Generator-based slot machine game
const getRandomInt = (range) => Math.floor(Math.random() * range);
// Create an array of symbols for the slot machine
const symbols = ['πŸ’', 'πŸ‹', 'πŸ””', '7️⃣', '🎱'];
// Define a generator function that will yield a random symbol
// from the array when iterated over
function* getReels(noOfReels = 3) {
let i = 0;
while (i++ < noOfReels) {
@perjo927
perjo927 / emoji-generator.js
Created January 10, 2023 19:02
Emoji fusion generators
const emoji = require('emoji-name-map')
// 1. Iterable object
const impersonator = {
genders: ['man', 'woman', 'adult'],
professions: [],
[Symbol.iterator]: function* () {
for (const profession of this.professions) {
for (const gender of this.genders) {
const professional = emoji.get(gender) + '\u200d' + emoji.get(profession)
@perjo927
perjo927 / generator-slot-game-2.js
Last active January 15, 2023 18:33
Generator-based slot machine game #2
const getRandomInt = (range) => Math.floor(Math.random() * range);
// Create an array of symbols for the slot machine
const symbols = ['πŸ’', 'πŸ‹', 'πŸ””', '7️⃣', '🎱'];
// Define a generator function that will yield a random symbol
// from the array when iterated over
function* getReels(noOfReels = 3) {
let i = 0;
while (i++ < noOfReels) {