Skip to content

Instantly share code, notes, and snippets.

@i-like-robots
i-like-robots / Pagination.spec.js
Last active Jun 11, 2021
Just some more pagination!
View Pagination.spec.js
const Pagination = require("./Pagination").Pagination;
describe("Pagination", () => {
describe(".fromResult", () => {
it("calculates the index of the first result on the current page", () => {
const instance = new Pagination({
totalResults: 25,
resultsPerPage: 10,
currentPage: 2,
});
@i-like-robots
i-like-robots / fileUtils.js
Created Apr 27, 2021
Mocking a callback with Jest
View fileUtils.js
const fs = require('fs');
const glob = require('glob');
/**
* Find files
* @param {String} globPattern
* @returns {Promise<String[]>}
*/
function findFiles(globPattern) {
return new Promise((resolve, reject) => {
@i-like-robots
i-like-robots / instrumentRequests.js
Last active Sep 11, 2020
Instrument Node.js HTTP requests
View instrumentRequests.js
// Based on <https://blog.bearer.sh/http-api-instrumentation-nodejs/>
const http = require("http");
const https = require("https");
const { performance } = require("perf_hooks");
function patchModule(module) {
const original = module.request;
function newRequest(outgoing) {
// Using perf.now() instead of Date.now() not because we need the resolution
@i-like-robots
i-like-robots / upload-to-s3.sh
Last active Oct 1, 2019
Upload client-side assets to S3 via AWS CLI
View upload-to-s3.sh
#!/bin/bash
SOURCE_FILES=($(find public/*.{js,css,gz,br,map} 2>/dev/null))
DESTINATION_BUCKET="hashed-assets-eu"
DESTINATION_FOLDER="page-kit"
for FILE in ${SOURCE_FILES[@]}; do
if [[ $FILE == *".gz" ]]; then
@i-like-robots
i-like-robots / fetch.js
Created Mar 17, 2019
Signed AWS requests with node fetch
View fetch.js
const nodeFetch = require('node-fetch');
const httpsAgent = require('./httpsAgent');
const handleResponse = require('./handleResponse');
module.exports = async (url, options = {}) => {
const response = await nodeFetch(url, {
...options,
agent: httpsAgent
});
@i-like-robots
i-like-robots / hyperons.js
Last active Jan 29, 2019
A quick experiment refactoring Hyperons to output real DOM nodes
View hyperons.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.hyperons = {}));
}(this, function (exports) {
'use strict';
/**
* Base Component class
* @param {object} props The initial component props
@i-like-robots
i-like-robots / highlight.scss
Created Dec 19, 2017
Monokai Pro Pygments/Rouge theme
View highlight.scss
.highlight { background: #282A3A; color: #EAF2F1 }
.ge { font-style: italic } // Generic.Emph
.gs { font-weight: bold } // Generic.Strong
.gi { color: #BAD761 } // Generic.Inserted
.gd { color: #FF657A } // Generic.Deleted
.err { color: #FF657A } // Error
.nc, // Name.Class
@i-like-robots
i-like-robots / index.html
Created Sep 15, 2017
Normalize onChangeCapture
View index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Preact Compat onChange normalization</title>
</head>
<body>
<div id="app"></div>
<script src="dist/main.js"></script>
</body>
@i-like-robots
i-like-robots / ads.js
Last active Jan 28, 2021
Minimum viable IMA implementation for desktop and mobile with support for autoplay when available and basic error handling.
View ads.js
'use strict'
const CLASSNAME_WAITING = 'is-waiting'
const CLASSNAME_LOADING = 'is-loading'
const CLASSNAME_PREROLL = 'is-preroll'
const CLASSNAME_PLAYING = 'is-playing'
const CLASSNAME_PROBLEM = 'is-problem'
// <https://developers.google.com/interactive-media-ads/docs/sdks/html5/sdk-player>
function ads (target) {
@i-like-robots
i-like-robots / toggle.js
Created Jul 19, 2016
universal accessible toggle button
View toggle.js
module.exports = function (buttons, target, callback) {
buttons = buttons.length === undefined ? [ buttons ] : Array.from(buttons);
const toggle = function (e) {
const state = target.classList.toggle('is-active');
buttons.forEach(button => {
button.setAttribute('aria-expanded', state);
});