Skip to content

Instantly share code, notes, and snippets.

@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 = {};
@uhop
uhop / SplayTree.js
Last active October 11, 2019 21:47
Simple implementation of a splay tree.
'use strict';
const zig = tree => {
const newTree = tree.l,
parent = (newTree.p = tree.p);
if (parent) {
if (parent.l === tree) parent.l = newTree;
else parent.r = newTree;
}
tree.p = newTree;
@uhop
uhop / List.js
Last active October 11, 2019 21:46
Simple yet complete implementation of a double-linked list.
'use strict';
class ListNode {
constructor() {
this.prev = this.next = this;
}
}
const pop = head => {
const rest = head.next;
@uhop
uhop / Heap.js
Last active October 9, 2019 23:55
Simple heap implementation
'use strict';
// using heap implementation from https://github.com/heya/ctr under the BSD-3 license
class Heap {
constructor(less = (a, b) => a < b, arrayLike = []) {
this.less = less;
this.array = Heap.make(Array.from(arrayLike), this.less);
}