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 / templates.js
Last active January 12, 2016 01:12
simple templates
/**
* Use a simple object to create your html with variables
* and whatever code you need to build it up.
*
* You can take advantage of type coercion here to
* end up with the final strings.
*/
var template = {
placeholder1: undefined,
placeholder2: undefined,
@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 / querify.js
Created November 15, 2013 16:02
Takes data in a hierarchy and returns a string suitably formatted for a POST request's postBody or a GET request's query component.
/**
* querify
*
* Takes data in a hierarchy and returns a string suitably formatted for a POST
* request's postBody or a GET request's query component.
*
* Note:
* This function will not play well with circular referenced objects, it
* will result in an infinite loop. Most likely. I haven't tested
* that case.
@jdfm
jdfm / mutableBind.js
Last active December 25, 2015 21:49
mutableBind - Keep the wrapper, redefine everything else.
/**
* Mutable Binds.
*
* Why are mutable binds needed?
* I don't know about "needed", I came up with the idea because I was
* curious about if it would work, how it would work, and if there are
* any use cases. As for the use cases, I don't really know. If you can
* find a legitimate use case, let me know, I'd be happy to hear from
* you.
*
@jdfm
jdfm / styled-input-type.html
Last active December 25, 2015 17:59
Minimal JS, crossbrowser, styled input[type="file"] with simple markup.
<!doctype html>
<!--
Minimal JS, crossbrowser, styled input[type="file"] with simple markup.
Tested working in IE[>=7], FF[24], O[16], C[30], S[6.0.5]; Should work in
all browsers that support opening a file browser via a label associated
to an input[type="file"]. Some adjustments might be necessary for IE[6]
support.
Tested using html5 doctype. Further testing is necessary to ensure proper
@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.