Skip to content

Instantly share code, notes, and snippets.

View jdfm's full-sized avatar

Jonatas Miguel jdfm

View GitHub Profile
@jdfm
jdfm / destructured-arguments.ts
Last active June 17, 2019 14:36
Typescript: Typing Destructured Arguments
/**
* Typing destructured arguments
*/
/**
* You don't need to know the type of the destructured prop up front with this construct.
* Useful for cases where you want to make a function that destructures something and passes that value along.
* No intellisense.
*/
export type PassthroughProperty<K extends PropertyKey> = <T extends Pick<any, K>>(arg: T) => T[K]
@jdfm
jdfm / flatten_iterative.js
Created October 18, 2016 03:39
iterative implementation of array flattening in javascript
var flatten_v1 = function(array){
var indices = [array.length - 1];
var arrays = [array];
var output = [];
while(indices.length){
if(indices[0] === -1){
indices.shift();
arrays.shift();
@jdfm
jdfm / leftPad.js
Last active September 5, 2016 01:59
ES6 Exploration: leftPad for natural numbers
const leftPad = (
( input=0, minOutputLength=0 ) =>
String( input ).replace( /^(\d+)$/, ( match, $1='' ) =>
'0'.repeat( Math.max( minOutputLength - $1.length, 0 ) ) + $1
)
)
console.log(leftPad(10000, 10))
@jdfm
jdfm / primelist.js
Created September 5, 2016 01:30
ES6 Exploration: Use iterator pattern to generate a prime list
const primeList = new Array(1000)
primeList[Symbol.iterator] = function(){
const buffer = [2, 3]
let bufferLength = 2
const _this = this
const thisLength = this.length
let lastChecked = 4
@jdfm
jdfm / random_utilities.js
Last active January 4, 2016 22:08
Generating random numbers with javascript
/**
* Generate an integer between [min..max]
*
* @param {Number} min The minimum integer you want to create
* @param {Number} max The maximum integer you want to create
* @return {Number} A random integer between [min..max]
*/
function getRandomInteger(min, max){
return min + Math.floor(Math.random() * (max - min + 1));
}
@jdfm
jdfm / switch fizzbuzz.js
Created June 23, 2015 23:54
use a for switch block to implement a fizz buzz
for ( var i = 1; i <= 100; i++ ) switch ( true ) {
case !( i % 3 ) && !( i % 5 ): console.log('fizzbuzz', i); break;
case !( i % 3 ): console.log('fizz', i); break;
case !( i % 5 ): console.log('buzz', i); break;
default:
}
@jdfm
jdfm / switch(value) case expression.js
Last active August 29, 2015 14:23
It's possible to use a switch block as an alternative to if else if else, but with a few caveats...
// classic example
if ( somevalue === 1 ) {
// do the thing
} else if ( somevalue === 2 ) {
// do some other thing
}
switch ( somevalue ) {
case 1: /* do some thing */ break;
case 2: /* do some other thing */ break;
@jdfm
jdfm / if else switch.js
Last active August 29, 2015 14:23
it's possible to place a switch block right after an else (or an if(condition)) statement, making it look like an alternate conditional block
if ( condition ) {
// do the thing
} else switch ( state ) {
case 1: /* do something */ break;
default: case 2: /* do something else */
}
@jdfm
jdfm / unrenderable-comment.md
Last active December 1, 2015 15:43
markdown specific comments that won't be in the rendered document - via http://stackoverflow.com/a/20885980/4644997

//: # If you can see this, this variation doesn't work

Above are a few examples of comments in markdown, the purpose of these comments is to not render an empty line in it's place. If anything is visible above this paragraph, it's because one of the variants being tested does not work with the rendering engine you are using.

@jdfm
jdfm / small-nest.js
Last active August 29, 2015 14:21
Reducing "if" nesting - via http://qr.ae/f3BuB
// Original nested version
if ( a > 0 ) {
if ( b > 10 ) {
if ( c > 0 ) {
//complex code
}
}
}
return;