Skip to content

Instantly share code, notes, and snippets.

View lewdev's full-sized avatar

Lewis Nakao lewdev

View GitHub Profile
@lewdev
lewdev / genRandStr.js
Created January 28, 2021 19:27
JavaScript function to generate a random string given length n.
const genRandStr = n =>
Array.apply(null, Array(n + 1))
.map((a, i) => i)
.reduce(a => a += "abcdefghijklmnopqrstuvwxyz1234567890".charAt(Math.random() * 36))
.substr(1);
genRandStr(20);
<!doctype html><html contenteditable><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Template</title>
<style>*{font-size: 1rem;font-family:Verdana;line-height:1.5;}body{margin:.5rem;}</style>
<body><script>//mongoexport --collection=events --db=reporting --out=events.json
window.onload = function() {
fetch('data.json').then(r => r.json()).then(rows => {
const arr = [];
rows.filter(r => true).map(row => {
arr.push(row.str);
});
document.body.innerHTML = `["${arr.join('","')}"]`;
@lewdev
lewdev / UrlParam.js
Last active September 27, 2024 01:09
Get and set URL parameters dynamically.
onload = _ => {
//setUrlParam will trigger `render`
onLocationChange(render);
};
const getUrlParam = param => {
const urlArr = document.location.href.split`?`;
const paramArr = urlArr.length > 1 ? urlArr[1].split`&` : [];
const dataPair = paramArr.find(a => a.indexOf(param + "=") === 0);
return dataPair ? dataPair.split`=`[1].replace(/%20/g, " ") : false;
@lewdev
lewdev / index.html
Last active July 18, 2021 11:32
Canvas Template with common functions
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{title}</title>
<meta name="author" content="Lewis Nakao">
<meta name="description" content="{description}">
<meta name="keywords" content="JavaScript, Emoji, HTML Canvas, Open Source">
@lewdev
lewdev / show-keycodes.html
Created December 18, 2021 02:19
Show Keyboard Key Codes
<table id=t></table><script>addEventListener("keydown",e=>t.innerHTML=["key","keyCode","code"].map(a=>`<tr><td>${a}</td><td>${e[a]}</td></tr>`).join(""))</script>
@lewdev
lewdev / tce-load-bootstrap-5.html
Created February 3, 2022 00:50
Load Bootstrap 5 in Tiny Code Editor
<style id=style></style>
<script id=script></script>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
@lewdev
lewdev / pixmoji.html
Last active February 8, 2022 12:11
pixmoji - Pixel Editor using Emojis
<div id=o></div><script>
d=document;
c="⬛⬜🟥🟦🟨🟩🟧🟪🟫".split(/.*?/u); //square emojis
b=c[0]; //background color
s=c[5]; //selected color
g=[]; //the grid
//create new array and fill it
a=(n,m)=>new Array(n).fill(m);
@lewdev
lewdev / top-75-curated-leetcode.js
Last active July 6, 2024 12:46
Top 75 LeetCode Questions to Save Your Time in JSON Array
/*
New Year Gift - Curated List of Top 75 LeetCode Questions to Save Your Time
https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU
*/
const top75problems = [
{ name: "Two Sum", category: "Array", problem: "two-sum" },
{ name: "Best Time to Buy and Sell Stock", category: "Array", problem: "best-time-to-buy-and-sell-stock" },
{ name: "Contains Duplicate", category: "Array", problem: "contains-duplicate" },
{ name: "Product of Array Except Self", category: "Array", problem: "product-of-array-except-self" },
{ name: "Maximum Subarray", category: "Array", problem: "maximum-subarray" },
@lewdev
lewdev / formatBytes.html
Last active March 3, 2022 03:18
Convert bytes to formatted KB, MB, GB, TB, etc. in JavaScript.
<pre id=o></pre>
<script>
const UNITS = ",K,M,G,T,P".split(",");
const UNIT_SIZES = UNITS.reduce((prev, curr, i) => {
prev[curr] = Math.pow(1024, i);
return prev;
}, {});
const formatBytes = bytes => {
let size, unit;
@lewdev
lewdev / reverseLinkedList.html
Created March 8, 2022 04:51
Reverse a linked list in JavaScript and HTML.
<pre id=o></pre>
<script>
class LinkedList {
constructor(value, next) {
this.value = value;
this.next = next;
}
}