Skip to content

Instantly share code, notes, and snippets.

@mieszko4
mieszko4 / atob.lua
Last active December 3, 2022 22:16
-- Lua uses at least 8 bytes to represent a number
-- Base64 minimum processable unit is represented by four 6-bit chunks which is 24-bits => 3 bytes
-- Hence we can safely use bit operations on number type for multiplexing and extraction
-- Define function to extract 8-bit ASCII char from 24-bit base64 unit
local function extractChar(unit, idx)
-- Skip mask for first ASCII char since shifting will destroy other two parts
if idx == 2 then
local charCode = (unit) >> (8 * idx)
@mieszko4
mieszko4 / confused.ts
Created December 13, 2020 12:49
Confused TypeScript
type A = {a: 'a'} | {b: 'b'};
const c: A = {
a: 'a',
b: 'b'
};
type C = typeof c; // === A
// Confused TS: I cannot access c.a nor c.b
@mieszko4
mieszko4 / PromiseDelayToDelay.codemod.js
Created March 28, 2018 23:43
Js-codemod converter from bluebird's Promise.delay to delay
module.exports = function (fileInfo, api) {
const j = api.jscodeshift;
const isPromiseDelay = (nodePath) => {
const { object, property } = nodePath.node.callee;
return object && object.name === 'Promise' && property && property.name === 'delay';
};
const isRequire = (nodePath, name) => {
@mieszko4
mieszko4 / customer-to-string.stream.js
Last active April 20, 2016 15:22
JsMeetup - Streaming in javascript - Stream apppoach
const util = require('util');
const Transform = require('stream').Transform;
const intercomDrinks = require('intercom-drinks').default;
function Stream() {
if (!(this instanceof Stream)) {
return new Stream();
}
Transform.call(this, {
writableObjectMode: true
@mieszko4
mieszko4 / intercom-drinks-functional.js
Last active April 20, 2016 15:20
JsMeetup - Streaming in javascript - Functional approach
const request = require('request');
const fs = require('fs');
const intercomDrinks = require('intercom-drinks').default;
const radius = 100000; //in meters
const latitude = 53.3381985;
const longitude = -6.2592576;
const onlineSource = 'https://gist.githubusercontent.com/brianw/19896c50afa89ad4dec3/raw/6c11047887a03483c50017c1d451667fd62a53ca/gistfile1.txt';
const offlineSource = 'input.jsonl';
@mieszko4
mieszko4 / encoder.js
Last active March 3, 2016 03:26
Encoder with generator
Number.prototype.mod = function(n) {
return ((this % n) + n) % n;
};
function encode(string, generator, cycle=10, reverse=false) {
let sequence = generator();
let start = ' '.charCodeAt();
let end = '~'.charCodeAt();
return Array.from(string).map((c, i) => {
@mieszko4
mieszko4 / knowledge-hardwork-attitude.js
Created December 5, 2015 12:09
If ABCDEFGHIJKLMNOPQRSTUVWXYZ equals 1234567891011121314151617181920212223242526 then...
['Knowledge', 'Hardwork', 'Attitude'].map(function (word) {
return {
word: word,
count: word.toUpperCase().split('').reduce(function (p, c) {
return p + (c.charCodeAt(0)-'A'.charCodeAt(0) + 1);
}, 0)
};
});