Skip to content

Instantly share code, notes, and snippets.

// 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);
};
@uhop
uhop / dnd-g.html
Last active August 3, 2021 23:34
A minimalistic generic DnD code. A facelift of https://gist.github.com/uhop/21fa11bf387e234e0d79bf90d7b08735
<!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.mjs
Last active May 12, 2023 17:22
No-dependency ES6 drop-in modular static development HTTP server.
// https://gist.github.com/uhop/fbb7fc3606cbb2fa54e0ce4f9a200037
// License: BSD-3
// (c) 2023 Eugene Lazutkin
import http from 'http';
import fs from 'fs';
import path from 'path';
const fsp = fs.promises;
@uhop
uhop / ListHead.js
Last active October 19, 2020 02:59
Simple yet complete implementation of a double-linked list that reuses objects building lists with their properties.
'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 July 4, 2020 15:28
Image-related utilities for use in browsers.
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 April 20, 2020 21:39
Access to S3 bucket by roles

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 February 14, 2020 17:23
How many tries it takes to roll a particular sequence
'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 June 17, 2021 22:24
Script to set up AWS environment according to its profile.
'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 October 19, 2020 03:09
Useful scripts to deal with AWS CloudFront distributions by the associated domain names and invalidations.
'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;
@uhop
uhop / Cache.js
Last active October 14, 2019 15:40
Simple cache implementation.
'use strict';
const List = require('./List');
class Cache {
constructor(capacity = 10) {
this.capacity = capacity;
this.size = 0;
this.list = new List();
this.dict = {};