Skip to content

Instantly share code, notes, and snippets.

@uhop
uhop / dnd.js
Last active Jul 23, 2022
The facelift of the minimalistic DnD code from https://gist.github.com/uhop/d87365fac38ba6b8cbf0b890d0c2258e
View dnd.js
'use strict';
// from https://gist.github.com/uhop/d87365fac38ba6b8cbf0b890d0c2258e
const noop = () => {};
class DndMove {
static supportedEvents = {
pointerup: 'onPointerUp',
pointermove: 'onPointerMove',
View batch.js
// runs asynchronous operations in parallel, no more than a specified number at a time
// it takes an array of functions, which return promises when invoked without arguments
// modelled after Promise.all()
const wrap = value => {
if (typeof value == 'function') return value();
if (value && typeof value.then == 'function') return value; // thenable
return Promise.resolve(value);
};
View dnd-g.html
<!DOCTYPE html>
<html>
<head>
<title>DnD test bed - grouping</title>
<link rel="stylesheet" href="./dnd.css" />
</head>
<body>
<h1>DnD test bed - grouping</h1>
<div class="container">
<div class="item dnd-item dnd-handle">One</div>
@uhop
uhop / static-server.js
Last active Aug 29, 2022
No-dependency ES6 drop-in modular static development HTTP server.
View static-server.js
import http from 'http';
import fs from 'fs';
import path from 'path';
const fsp = fs.promises;
// simple development static server with no dependencies
// default environment variables: HOST=localhost, PORT=3000, SERVER_ROOT is process.cwd()
@uhop
uhop / ListHead.js
Last active Oct 19, 2020
Simple yet complete implementation of a double-linked list that reuses objects building lists with their properties.
View ListHead.js
'use strict';
class ListHead {
constructor(head = null, next = 'next', prev = 'prev') {
if (head instanceof ListHead) {
({nextName: this.nextName, prevName: this.prevName, head: this.head} = head);
return;
}
this.nextName = next;
this.prevName = prev;
@uhop
uhop / detectWebp.js
Last active Jul 4, 2020
Image-related utilities for use in browsers.
View detectWebp.js
let flag;
const detectWebp = () => {
if (typeof flag == 'boolean') return flag;
const canvas = document.createElement('canvas');
flag = !!(canvas.getContext && canvas.getContext('2d') && /^data:image\/webp;/.test(canvas.toDataURL('image/webp')));
return flag;
};
export default detectWebp;
@uhop
uhop / README.md
Created Apr 20, 2020
Access to S3 bucket by roles
View README.md

What

We need to setup an S3 bucket so we can use AWS JS SDK to upload files from a web application only for logged in users.

We already have a Cognito user pool, e.g., self-managed, and/or tied to a corporate SSO or social networks. Users may have different permissions depending on custom groups they are on. There is an app client, which is used to authenticate users in our web app.

How

@uhop
uhop / roll.js
Created Feb 14, 2020
How many tries it takes to roll a particular sequence
View roll.js
'use strict';
const roll = pattern => {
const samples = [];
let counter = 0;
for(;;){
if (samples.length >= pattern.length) {
samples.shift();
}
const sample = Math.floor(Math.random() * 6) + 1;
@uhop
uhop / getAWS.js
Last active Jun 17, 2021
Script to set up AWS environment according to its profile.
View getAWS.js
'use strict';
const AWS = require('aws-sdk');
let profile = process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV ? '' : 'default';
const profileIndex = process.argv.indexOf('--profile');
if (profileIndex > 0 && profileIndex + 1 < process.argv.length) {
profile = process.argv[profileIndex + 1];
}
@uhop
uhop / distributions.js
Last active Oct 19, 2020
Useful scripts to deal with AWS CloudFront distributions by the associated domain names and invalidations.
View distributions.js
'use strict';
const iterate = async function*(client) {
const params = {MaxItems: '100'};
for (;;) {
const data = await client.listDistributions(params).promise(),
list = data.DistributionList,
items = list.Items;
if (items) {
yield items;