Skip to content

Instantly share code, notes, and snippets.

@joemaller
joemaller / luxon.md
Created January 23, 2018 14:29
Luxon Timestamps

Finding this took me longer than it should have, probably because I was impatiently looking for "timeststamp" instead of "milliseconds of the Unix epoch". For future searchers, Luxon uses the methods DateTime.fromMillis and DateTime.valueOf.

// Create a Luxon DateTime from a JS Unix timestamp
const ts = new Date().getTime();     // 1516717417146
const dt = DateTime.fromMillis(ts);  // { ts: 2018-01-23T09:23:37.146-05:00 ...
console.log(dt.valueOf());           // 1516717417146
@LachlanArthur
LachlanArthur / !JS Sprintf.md
Last active July 25, 2022 19:55
Simple sprintf in TypeScript using Tagged Template Literals

JS Sprintf

Simple sprintf in JS using Tagged Template Literals

sprintf`There are ${0} monkeys in the ${1}.`( '10', 'tree' );
// > There are 10 monkeys in the tree.

const linkTemplate = sprintf`<a href="${0}" ${2}>${1}</a>`;
linkTemplate('/contact/', 'Contact Us');
linkTemplate('https://example.com', 'Open Preview', 'target="_blank"');
@antishok
antishok / NODE_ENV production.md
Last active September 25, 2020 07:09
What does `NODE_ENV=production` do?
@idleberg
idleberg / atom-macos-context-menu.md
Last active April 27, 2022 00:37
“Open in Atom” in macOS context-menu

Open in Atom

  • Open Automator
  • Create a new Service
  • Set “Service receives selected” to files or folders in any application
  • Add a Run Shell Script action
  • Set the script action to /usr/local/bin/atom -n "$@"
  • Set “Pass input” to as arguments
  • Save as Open in Atom
@harto
harto / before.js
Created April 20, 2016 17:33
Mocha before() & beforeEach() execution order with nested describe()
'use strict';
describe('mocha before hooks', function () {
before(() => console.log('*** top-level before()'));
beforeEach(() => console.log('*** top-level beforeEach()'));
describe('nesting', function () {
before(() => console.log('*** nested before()'));
beforeEach(() => console.log('*** nested beforeEach()'));
it('is a nested spec', () => true);
});
@nzakas
nzakas / arrayproxy.js
Created February 16, 2016 20:46
A proxy that acts like an array
// target is the backing object
let target = { length: 0 },
proxy = new Proxy(target, {
set(trapTarget, key, value) {
let numericKey = Number(key),
keyIsInteger = Number.isInteger(numericKey);
// special case for length property - only need to worry if length is
// shorter than number of array items
@joshnuss
joshnuss / app.js
Last active October 20, 2024 14:03
Express.js role-based permissions middleware
// the main app file
import express from "express";
import loadDb from "./loadDb"; // dummy middleware to load db (sets request.db)
import authenticate from "./authentication"; // middleware for doing authentication
import permit from "./authorization"; // middleware for checking if user's role is permitted to make request
const app = express(),
api = express.Router();
// first middleware will setup db connection
@brettz9
brettz9 / jamilih-lite.js
Last active January 11, 2020 02:37
Jamilih Lite (see also https://github.com/brettz9/jamilih ); for adaptation and inclusion in stand-alone libraries which still wish to build the DOM in an attractive, pure JS, and syntax-highlighting/JS-friendly manner. For the string-building equivalent, see https://gist.github.com/brettz9/72bb6a460212d9350f67
function jml (elName, atts, children) {'use strict';
var el = typeof elName === 'string' ? document.createElement(elName) : elName;
if (atts && Array.isArray(atts)) {
children = atts;
}
else if (atts) {
Object.keys(atts).forEach(function (att) {
var attVal = atts[att];
if (att === '$on') {
return Object.keys(attVal).forEach(function (ev) {
@gordonbrander
gordonbrander / route.js
Last active April 30, 2022 01:26
route.js: a history.pushState microrouter without the routing fluff.
// Microrouter based on history.pushState.
// All thrills, no frills.
// Usage:
//
// var h = urlstate(callback);
// h.push('#foo')
function urlstate(callback) {
// Since `history.pushState` doesn't fire `popstate`, we can use this function
// instead. Will update history state and call `callback` with currently
// showing `url`.
@woollsta
woollsta / chunkify.js
Last active December 25, 2023 10:45
Fixes an issue with Google Chrome Speech Synthesis where long texts pause mid-speaking. The function takes in a speechUtterance object and intelligently chunks it into smaller blocks of text that are stringed together one after the other. Basically, you can play any length of text. See http://stackoverflow.com/questions/21947730/chrome-speech-sy…
/**
* Chunkify
* Google Chrome Speech Synthesis Chunking Pattern
* Fixes inconsistencies with speaking long texts in speechUtterance objects
* Licensed under the MIT License
*
* Peter Woolley and Brett Zamir
*/
var speechUtteranceChunker = function (utt, settings, callback) {