Skip to content

Instantly share code, notes, and snippets.

View marcosmapf's full-sized avatar

Marcos de Andrade marcosmapf

View GitHub Profile
@marcosmapf
marcosmapf / reactClassCheatSheet.jsx
Last active June 2, 2019 05:13
Example class-based React Component used for practice purposes
import React from 'react';
class UserCardExample extends React.Component {
constructor() {
super();
this.state = {
user: "",
fetching: true
};
}
@marcosmapf
marcosmapf / collatzSequence.js
Last active May 13, 2019 03:01
Generate and compare sizes from a Collatz Sequence
/*
Generate Collatz Sequences and compare and discover sizes from sequences
To avoid state mutation, there is no default memoized sequence
*/
function* Range(start = 0, end = Infinity, step = 1) {
for (let i = start; i <= end; i += step) {
yield i;
}
}
@marcosmapf
marcosmapf / waitForEach.js
Created May 4, 2019 22:07
Synchronously process an array of elements through a function that returns a Promise
/*
Sequentially / synchronously process an array of elements through a function that returns a Promise.
Func must be a function that returns a Promise
Basic usage: Elements must be processed in sequence, but the Process Function
returns a Promise, so it must be resolved until the next element is processed
Credits to Mattias Petter Johansson, https://github.com/mpj
*/
@marcosmapf
marcosmapf / pipe.js
Last active January 27, 2020 14:54
Defines a function Pipe used in composition
/*
Defined the function Pipe.
Pipe accepts a list of functions and makes use of composition to apply the resulting values in sequence, given an initial value
As an example, a userList mock is created and the data is filtered, processed and then logged through the function Pipe.
*/
export default const pipe = (...funcList) => input => funcList.reduce((acc, func) => func(acc), input);
@marcosmapf
marcosmapf / generatorFrom.js
Last active May 9, 2019 03:24
Returns a generator from a given element
/*
Returns a well-formed generator from a given argument even if said argument is non-iterable.
If argument is an iterable, iterating via for...of, calls to .next() or other methods yields the argument values.
if argument is not an iterable, returns the default value as per Iteration Protocol: {value: undefined, done: true}
*/
function* generatorFrom (element) {
if (element && element[Symbol.iterator]) yield* element;
return;
}
@marcosmapf
marcosmapf / getNestedProperty.js
Last active January 19, 2020 22:41
Returns the value of the property or null
const getNestedProperty = (obj, path) => {
const isObject = obj => Object.prototype.toString.call(obj) === '[object Object]'
if(!isObject(obj) || typeof path !== 'string') return false
const properties = path.split('.')
let state = obj
for (const prop of properties) {
if (!state[prop]) return null
state = state[prop]
}
return state
@marcosmapf
marcosmapf / LinkedList.js
Last active April 26, 2019 12:08
Javascript List Class to practice ES6 Class syntax and usage of Symbol.Iterator (Generator)
/*
Class Defining a LinkedList Functor and its ListNode members.
*/
//STILL NEED TO IMPLEMENT INDEX MODIFICATION AFTER PREPEND AND REMOVE
'use strict'
const isPrimitive = value => typeof value === "boolean" || value && typeof value === "number" || typeof value !== "object" && typeof value !== "function" && value.constructor !== Array;
@marcosmapf
marcosmapf / Range.js
Last active January 19, 2020 22:09
Defines the data structure Range
/*
Defines the data structure Range as a Custom Iterator, a Custom Generator and as a Class.
Range makes use of the Iterator Protocol and Symbol.iterator to generate new values
to the user and takes three optional arguments:
- start: the starting value of the range
- end: the final value of the range upon which the Range will be done
- step: the increment upon the returned values
*/
function rangeAsIterator() {
@marcosmapf
marcosmapf / imageWorker.html
Last active April 11, 2019 04:07
HTML/Javascript Fragment to Asynchronously load images to an HTML Element
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="JS Image Appending">
<meta name="author" content="Marcos de Andrade Pinto Filho">
<title>HTML5 Template</title>
</head>
<body>
<main class="imageContainer">
@marcosmapf
marcosmapf / dragAndDrop.html
Last active April 11, 2019 04:16
HTML detailing the process of modifying divs and other elements into draggables and droppables
<!DOCTYPE HTML>
<html>
<head>
<style>
.droppableBox {
display: flex;
align-items: center;
justify-content: center;
width: 250px;
height: 70px;