Skip to content

Instantly share code, notes, and snippets.

View moimikey's full-sized avatar
:shipit:
ship it

Michael Scott Hertzberg moimikey

:shipit:
ship it
View GitHub Profile
@moimikey
moimikey / a.js
Created January 6, 2018 17:20
filter out only `on` event props from react component
// ...
renderMenu() {
const { children, ...rest } = this.props || {};
// only store props that startWith `on` in case the parent component
// wants to send event callbacks down, for subsequent use.
const eventProps = Object.keys(rest)
.filter(key => key.substr(0, 2) === 'on') // if startsWith 'on' (ie. onMouseOver)
.reduce((obj, key) => Object.assign(obj, { [key]: rest[key] }), {});
// ...
@moimikey
moimikey / fill-array.js
Created December 5, 2017 01:35
fill array with value of variable in javascript in 49 bytes
const o = { a: true };
[...Array(9)].map(Function.prototype.valueOf, o);
@moimikey
moimikey / whatis.js
Last active March 18, 2016 14:55
really simple javascript type detection
const assert = function (condition) {
if (condition !== true && condition !== false) {
throw new TypeError("Assertions should be only of booleans; you're writing your spec wrong.");
}
if (!condition) {
throw new Error("Specification-level assertion failure");
}
};
function whatIs(this$) {
@moimikey
moimikey / after.js
Last active June 5, 2023 04:50
object literals for redux reducers
// O(1)
const todo = (state, action) => {
const actions = {
ADD_TODO: () => {
return {
id: action.id,
text: action.text,
completed: false
}
},
@moimikey
moimikey / babel.md
Last active January 24, 2016 02:00
babel notes of wisdom
lib notes
babel-cli basically what you need always
babel-register this is only for dev or local use and not production or browser
babel-core only necessary if using babel programatically
babel-polyfill shims inconsistent browser functions like Array.from, etc.
babel-runtime only helps at a code level by optimizing babel helpers into a single runtime

use presets

@moimikey
moimikey / custom.php
Created December 30, 2015 18:22
thermal, pull custom & acf fields
<?php
function north_cast_api_data($content) {
if (is_numeric($content)) $content = intval($content);
else {
$unserialized_content = @unserialize($content);
// we got serialized content
if ($unserialized_content !== false) {
// make sure that integers are represented as such, instead of str
foreach ($unserialized_content as $fn => &$c) {
if (is_numeric($c)) $c = intval($c);
@moimikey
moimikey / promise.js
Created November 23, 2015 21:24
a promise in javascript for dummies
// promises != callbacks
function doSomeAction() {
// like $.ajax or $.getJSON from jQuery, fetch will similarly reach out to a remote/local
// destination over HTTP, return a Promise object (the result of the `window.fetch`) that
// we can listen to by simply chaining the `then` method, which is provided by the Promise
// object that is returned by `window.fetch`.
window.fetch(new Request('http://website.com/products.json')).then(function(response) {
imFinished(response)
})
}
@moimikey
moimikey / callback.js
Created November 23, 2015 20:51
a callback in javascript for dummies
// callbacks != promises
function doSomeAction() {
return true
}
function appleTree() {
return doSomeAction()
}
appleTree()
@moimikey
moimikey / index.js
Last active November 20, 2015 21:18
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var locale2 = require('locale2');
document.write(locale2);
/**
* Michael Scott Hertzberg <[email protected]> (http://hertzber.gs)
*
* recursively check for set localStorage value every 100ms.
* this will also safely clear the timeout once found.
* can be used callback style or directly.
* functionally pure.
*/
function waitForLocalStorage(key, cb, timer) {
if (!localStorage.getItem(key)) return (timer = setTimeout(waitForLocalStorage.bind(null, key, cb), 100))