Skip to content

Instantly share code, notes, and snippets.

View perjo927's full-sized avatar
😎

Programmer Per perjo927

😎
  • DevCode
  • Stockholm, Sweden
View GitHub Profile
@perjo927
perjo927 / generator-paginator.js
Last active January 16, 2023 19:02
Generator function for paginating async data
// Mock data
const fakePaginatedData = [
{
next: 'https://fake-api.com/cars?page=2',
results: [
{ model: 'Volvo XC40', year: '2020', price: '30000' },
{ model: 'Renault Clio', year: '2019', price: '10000' },
{ model: 'Toyota Aygo', year: '2022', price: '20000' },
],
},
@perjo927
perjo927 / poll-async-value.js
Created January 15, 2023 19:09
Async polling generator for streams
// Wait utility
const wait = (timeInMs) => {
return new Promise((resolve) => {
setTimeout(resolve, timeInMs);
});
};
let i = 0;
// Fake real time data stream
@perjo927
perjo927 / poll-value-until-done.js
Created January 15, 2023 18:59
Async polling generator function with stop condition
const wait = (time) => {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
};
// Fake polling data
const fakeData = [null, null, null, { data: { foo: 'bar' } }];
let i = 0;
@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) {
@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.js
Last active January 15, 2023 18:34
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 / 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 / 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 / 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 / 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()));