Skip to content

Instantly share code, notes, and snippets.

View JO3-W3B-D3V's full-sized avatar
:atom:
I'm probably busy writing code or something.

Joseph Evans JO3-W3B-D3V

:atom:
I'm probably busy writing code or something.
View GitHub Profile
import React, { useState, useEffect, useReducer, useContext, useRef, createContext } from "react";
// Hook definition.
export const useContextAndReducer = (reducer, context) => {
const ctx = useContext(context);
const [state, dispatch] = useReducer(reducer, ctx.state);
const { setState } = ctx;
// Update the context when the state changes from the reducer.
useEffect(() => {
@JO3-W3B-D3V
JO3-W3B-D3V / fizzbuzz.js
Created September 24, 2019 17:12
This is a concise fizzbuzz solution.
/**
* This is just an example of how concise the FizzBuzz solution can be.
* This may not be the most efficient or elegant solution in the world, but eh, it's a bit of fun! :)
*/
// Just off load the logic to a method for the sake of ease.
// It could be a one line solution, but it's a little harder to read that way.
const print = i => (i % 3 == 0 ? 'Fizz' : '') + (i % 5 == 0 ? 'Buzz' : '') || i;
// Now to iterate over an array with 100 elments & print the solution.
import { Observable } from 'rxjs';
export class BasicFileInformation {
public fileName: String;
public fileSize: Number;
public fileData: String | ArrayBuffer;
public fileMimeType: String;
constructor(fileName: String, fileSize: Number, fileData: String | ArrayBuffer, fileMimeType: String) {
this.fileName = fileName;
@JO3-W3B-D3V
JO3-W3B-D3V / getCss.js
Created July 4, 2019 11:05
Fun little snippet to get a web page(s) CSS in JavaScript as text.
var css = Array.from(document.styleSheets).reduce((l, s) => {
l.push([...s.cssRules].map(c => c.cssText));
return l;
}, []).map(arr => arr.join()).join();
console.log(css);
// A functional example
return someList.stream()
.map(String::toLowerCase)
.anyMatch(x::contains);
// A more traditional example
for (String str : someList) {
if (x.contains(str.toLowerCase())){
return true;
}
@JO3-W3B-D3V
JO3-W3B-D3V / anagrams.js
Created March 8, 2019 09:44
This is a simple script to show how you can implement functional style JavaScript.
// Just a short hand function to assist the push function, it simply
// turns a string into an array, and due to surrogate pairs,
// this solution removes all instances of null, then it sorts the array,
// and finally returns it as a string.
const sort = s => [...s].filter(x => x != null).sort().join('');
// A function that will assist the anagrams function, it will
// essentially state whether or not to push a string onto the
// anagrams array or to push null onto the array, there are times