Skip to content

Instantly share code, notes, and snippets.

View johnloy's full-sized avatar

John Loy johnloy

View GitHub Profile
@johnloy
johnloy / bundle.js
Created May 6, 2021 20:49
Webpack runtime example
/*
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
* This devtool is neither made for production nor for readable output files.
* It uses "eval()" calls to create a separate source file in the browser devtools.
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
* or disable the default devtool with "devtool: false".
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
*/
/******/ (() => { // webpackBootstrap
/******/ "use strict";
rollup -c ; cat my-element.bundled.js | gzip -9 | wc -c ; rm my-element.bundled.js
// tzName is one of the tz database names at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
// Example: 'Pacific/Honolulu' (UTC-10)
function getUTCOffset(tzName, date) {
date = new Date(date || Date.now());
date.setMilliseconds(0);
// 1) Get the local UTC offset
// ---------------------------
// Convert date UTC offset to true hours offset.
// Date.prototype.getTimezoneOffset() confusingly returns positive
function isDstObserved(date = new Date()) {
var jan = new Date(0, 1);
var jul = new Date(6, 1);
const stdTimezoneOffset = Math.max(
jan.getTimezoneOffset(),
jul.getTimezoneOffset()
);
return date.getTimezoneOffset() < stdTimezoneOffset;
}
append_line() {
set -e
local update line file pat lno
update="$1"
line="$2"
file="$3"
pat="${4:-}"
lno=""
confirm() {
while true; do
read -p "$1 ([y]/n) " -r
REPLY=${REPLY:-"y"}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 1
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 0
fi
done
@johnloy
johnloy / convert-seconds.js
Created March 28, 2021 12:56 — forked from martinbean/convert-seconds.js
Convert seconds to HH:MM:SS format in JavaScript.
new Date(seconds * 1000).toISOString().substr(11, 8)
export declare type Constructor<T> = new (...args: any[]) => T
@johnloy
johnloy / bytesToSize.js
Created March 1, 2021 13:34 — forked from lanqy/bytesToSize.js
JavaScript To Convert Bytes To MB, KB, Etc
// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
@johnloy
johnloy / convertFromBaseToBase.js
Last active February 7, 2021 18:20
Convert the string representation of a number to another base
function convertFromBaseToBase(str, fromBase, toBase) {
var num = parseInt(str, fromBase);
return num.toString(toBase);
}