Skip to content

Instantly share code, notes, and snippets.

@sonyseng
sonyseng / HTMLOverlayExample.js
Created April 18, 2019 16:10
HTMLOverlay (Uber react-map-gl) - Used to display HTML including svgs from react and place them on the map
// It's hard to find good examples of overlay usage for react's wrapper for mapbox gl. this little snippet is a component
// that will display points on a map and allow the parent to render HTML. The positioning has to be absolute for the
// projection to render the images in the right spot on the map (Adjust for image height/width centering). - Sony
import React, {PureComponent} from 'react';
import {HTMLOverlay} from 'react-map-gl';
const ICON_HEIGHT = 32;
const ICON_WIDTH = 32;
const iconUrl = 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg';
@sonyseng
sonyseng / getBroadcastDateRange.js
Last active March 11, 2022 06:50
Given a Gregorian Date, get the Broadcast Date Ranges for Week, Month, Quarter, and Year
(function (url, onLoad) {
var script = document.createElement('script');
script.addEventListener('load', onLoad);
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}('https://cdn.jsdelivr.net/npm/moment@2.19.2/moment.min.js', function () {
const fmt = 'MM/DD/YYYY';
function getBroadcastWeek(momentDate) {
@sonyseng
sonyseng / SearchStringTokenizer.js
Last active April 17, 2016 04:23
Will tokenize quoted and unquoted strings for searching
var str = '\"hello world\" arg1 arg2 \'multi arg that has nested strings \"haha hehe\"\' arg3 anotherArg _arg4_\'';
function tokenizer (str) {
var tokens = [];
var k, i, temp;
var strLen = str.length;
for (i = 0; i < strLen; i++) {
if (str[i] === '\"') {
for (temp = [], k = i+1; k < strLen && str[k] !== '\"'; k++) {
@sonyseng
sonyseng / LazyCollection.js
Last active November 10, 2015 05:41
Using Generators to lazily build a collection of items in ES6 (babeljs)
// Infinitely generate all odd numbers or whatever the MAX Number is
function* oddNumbers() {
let i = 1
// Infinite loop. But thanks to the yield keyword, we exit this
// loop and return a value. On the next call back in, we resume
// where we left off.
while(1) {
yield i;
@sonyseng
sonyseng / TreeGenerator.js
Last active November 10, 2015 04:54
Use Generators to simplify the creation of an iterator in ES6 (babeljs)
// Test tree with contrived data
let testTree = {
value: 1,
child: [
{value: 2, child: [
{value: 7, child: []},
{value: 8, child: []},
{value: 9, child: []},
]},
{value: 3, child: []},
@sonyseng
sonyseng / TreeIterator.js
Last active December 9, 2023 08:49
Playing around with the Iterators from ES6 to walk a tree non-recursively (babeljs)
// Test tree with contrived data
let testTree = {
value: 1,
child: [
{value: 2, child: [
{value: 7, child: []},
{value: 8, child: []},
{value: 9, child: []},
]},
{value: 3, child: []},
@sonyseng
sonyseng / SimpleTemplate.js
Last active August 29, 2015 13:59
Simple search and replace Templating using Regex
// Example Usage: evalTempl("Hello {name}", {name: "sdg"})
function evalTempl (str, context) {
return str.replace(/\{(.*?)\}/g, function(match, key) {return context[key];});
}
@sonyseng
sonyseng / LoadScript.js
Created April 15, 2014 01:41
Load some javascript from a CDN. Doesn't work if the response header includes Content-Security-Policy prohibiting such actions. (i.e. github)
(function (url, onLoad) {
var script = document.createElement('script');
script.addEventListener('load', onLoad);
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}('https://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.2/q.js', function () {
// Load the Q promises library dynamically and print out the object
console.log(Q);
}))
@sonyseng
sonyseng / DeleteOtherElements.js
Last active August 29, 2015 13:58
Delete Every other element
// Get an existing child element that is not the passed in element
function getFirstChildThatIsNot(parent, child) {
return parent.children[0] != child ? parent.children[0] : parent.children[1];
}
// Get the parent node and delete everything except the passed in element
function deleteSiblingsOf(element) {
var parent = element.parentNode;
while (getFirstChildThatIsNot(parent, element)) {