Skip to content

Instantly share code, notes, and snippets.

View lukehorvat's full-sized avatar

Luke Horvat lukehorvat

View GitHub Profile
lukehorvat / gulpfile.js
Created March 3, 2023 20:43
An example of how to print out the names of files streamed by gulp. Sometimes useful for debugging purposes.
View gulpfile.js
import gulp from 'gulp';
import { Transform } from 'node:stream';
gulp.task('build', () => {
return gulp
new Transform({
objectMode: true,
transform(file, encoding, callback) {
lukehorvat / mapped-type-conversion.ts
Last active June 6, 2020 14:12
Convert one mapped type to another. (TypeScript)
View mapped-type-conversion.ts
type Input<Output> = {
[K in keyof Output]: { foo: () => Output[K] };
function convert<Output>(input: Input<Output>): Output {
const keys = Object.keys(input) as (keyof Input<Output>)[];
return keys.reduce(
(output, key) => ({ ...output, [key]: input[key].foo() }),
{} as Output
lukehorvat / gulpfile.js
Last active March 3, 2023 18:17
A small example of a "Vinyl adapter" that replaces `gulp.src`. Demonstrates how to create a stream for a "fake" (in-memory only) file.
View gulpfile.js
import gulp from 'gulp';
import Vinyl from 'vinyl';
import vinylAdapter from './vinyl-adapter';
gulp.task('build', () => {
const file = new Vinyl({
path: 'hello.js',
contents: Buffer.from(`console.log('👋');`)
lukehorvat / ban-generators-and-async.json
Last active July 31, 2018 15:41
ESLint rule to disallow generators and async/await. Since AST selectors are used, ESLint 3.18.0 (or higher) is required.
View ban-generators-and-async.json
"rules": {
"no-restricted-syntax": [
lukehorvat / get-color-in-range.js
Last active June 21, 2016 04:07
Get a color within a defined color range, based on a percentage number.
View get-color-in-range.js
import { scaleLinear } from "d3-scale";
const getColor = scaleLinear().domain([0, 100]).range(["#ccc", "#419fcf"]);
getColor(0); // 0% = #ccc
getColor(80); // 80% = #5da8ce
getColor(100); // 100% = #419fcf
lukehorvat / express-curl-middleware.js
Created August 27, 2015 12:45
Express middleware to log a JSON request as a cURL command. Inspired by Chrome's "Copy as cURL" feature.
View express-curl-middleware.js
// After body-parsing middleware...
app.use(function(req, res, next) {
"curl '%s://%s%s' -X %s -H 'Content-Type: %s' -d '%s'",
lukehorvat / es6-map-to-object-literal.js
Last active September 27, 2023 05:01
Convert ES6 Map to Object Literal
View es6-map-to-object-literal.js
let map = new Map();
map.set("a", 1);
map.set("b", 2);
map.set("c", 3);
let obj = Array.from(map).reduce((obj, [key, value]) => (
Object.assign(obj, { [key]: value }) // Be careful! Maps can have non-String keys; object literals can't.
), {});
console.log(obj); // => { a: 1, b: 2, c: 3 }
lukehorvat /
Last active December 23, 2015 02:49
An example of how to use request/response interceptors in AngularJS 1.2 for consistent API interactions. This code does two things: 1) Automatic injection of the user auth token in all API requests. 2) Automatic event firing when the user receives an API response indicating a 401 (unauthorised) request.
.config ($httpProvider) ->
$httpProvider.interceptors.push ($q, $rootScope, apiUrl, authToken) ->
request: (config) ->
# Intercept API requests and inject the auth token.
config.headers["X-Auth-Token"] = authToken if config.url.indexOf(apiUrl) is 0 and authToken?
config or $q.when config
responseError: (response) ->
# Intercept unauthorised API responses and fire an event.
lukehorvat / routes.html.erb
Created September 1, 2013 12:10
List all of the routes in your Ruby on Rails application in a nicely-formatted HTML table. Well-suited to those times when you're building an API and need an "auto-updating" reference page of application endpoints that you can share with others.
View routes.html.erb
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//" rel="stylesheet">
lukehorvat / excel-grid-plugin.js
Last active March 8, 2019 16:08
An Ext JS 4 grid plugin that provides Microsoft Excel-esque cell navigation and editing via keyboard. (License: MIT License)
View excel-grid-plugin.js
var newGridCellValue = '',
deleteGridCellValue = false;
Ext.define('ExcelCellEditing', {
extend: 'Ext.grid.plugin.CellEditing',
alias: 'plugin.excelcellediting',
initEditTriggers: function () {
var me = this;