Skip to content

Instantly share code, notes, and snippets.

Avatar
💯
Product Designer Interactive Storyteller

Scott C. Krause neodigm

💯
Product Designer Interactive Storyteller
View GitHub Profile
View kilo_format.js
export const kFormat = (num) =>
Math.abs(num) > 999
? Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + 'k'
: Math.sign(num) * Math.abs(num)
View getReadableTS.js
getReadableTS(){. //. Human readable timestamp for automatic file naming
const date = new Date();
const year = date.getFullYear();
const month = `${date.getMonth() + 1}`.padStart(2, '0');
const day =`${date.getDate()}`.padStart(2, '0');
const hour = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return `${year}${month}${day}_${hour}${minutes}${seconds}`
}
@neodigm
neodigm / cryptorange.js
Created Mar 17, 2021
unbiased dice roll in browser
View cryptorange.js
function getCryptoRange(min, max) {
const range = max - min + 1
const mBits = Math.ceil(Math.log2(range))
const mBytes = Math.ceil(mBits / 8)
const nAllowed = Math.floor((256 ** mBytes) / range) * range
const arBytes = new Uint8Array(mBytes)
let value
do {
crypto.getRandomValues(arBytes)
value = arBytes.reduce((acc, x, n) => acc + x * 256 ** n, 0)
View web_crypto_api_aes_base64.js
var winEcrypt = ( ( _w )=>{
let oKey = {};
return {
"base64ToArrayBuffer": function(base64) {
var binary_string = _w.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
View generate_csv_and_download.js
// Desc: Produce CSV with client-side JS. Contruct Blob and Download as CSV file
let nativeCSV = ( ( _d )=>{
let oCnt, jnCSV, sCSV, blCSV, elCSV; // config, json, array, blob, and element
let retObj = {
"init": ( _oCnt )=>{
oCnt = _oCnt;
if( oCnt.fileName.indexOf("####") !== -1) {
oCnt.fileName = oCnt.fileName.replace("####", Date.now() );}
jnCSV = sCSV = blCSV = elCSV = "";
return retObj;
View recursive_object_squash.js
function squash(object) { // Deep JavaScript Object Squash
return Object
.entries(object)
.map(([key, value]) => Object.assign({ key }, value && typeof value === "object"
? { value: '', children: squash(value) }
: { value, children: [] }
));
}
View show_email_popover_every_7_days.js
"use strict";
// Advanced eCom UX Patterns
// Show email promo image (branded) reveal once, every 7 days
// Open drawer to email panel when clicked
var fElmRevPromo = function( _d, _aIds ){
var _eRev = _d.getElementById( _aIds[0] ), _sBrand="LTD";
var _eRevI = _d.getElementsByClassName( _aIds[1] )[0];
return {
init: function( sBrand ){
View chrome_extension_vue_auth_token.js
// Scott C. Krause 👁️ neodigm | 2021
// Wait for Vue.js to mount and make VUEX auth token available to the chrome extension.
var _d = document, eScr = _d.createElement("script");
eScr.innerHTML = `
setInterval(() => {
var _d = document;
if( !_d.querySelector("[data-ext-auth]")){
var eV = _d.querySelector( 'DIV#app' );
if( eV?.__vue__?.$store?.state?.auth?.token ){
View pure_css_checkbox.html
<!DOCTYPE=HTML>
<html>
<head>
<title>Scotts Awesome Sauce Checkbox!</title>
<meta name="generator" content="Hugo 0.76.4">
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=2, minimum-scale=1, width=device-width, height=device-height">
</head>
<body>
<section role="main">
View doTwoTouch.js
var doTwoTouch = (function( _d, _q){ // Capture Pinch attempt on carousel
var eCntr, aEv=[];
setTimeout(function(){
if( oTopmenu && oTopmenu.isMobile() ) doTwoTouch.init();
}, 2800);
return {
"init": function(){
eCntr = _d.querySelector( _q[0] );
if( eCntr ){
eCntr.addEventListener("pointerdown", doTwoTouch.twoTouchDown );