Skip to content

Instantly share code, notes, and snippets.

@alenaksu
alenaksu / tea.js
Last active September 9, 2019 10:41
ES6 JavaScript implementation of TEA `Tiny Encryption Algorithm`
export default class TEA {
get DELTA() {
return 0x9e3779b9;
}
constructor(/* Uint32Array(4) */ _key) {
this._key = _key;
}
encrypt(/* Uint32Array(2) */ _value) {
@alenaksu
alenaksu / rdp.ts
Last active October 6, 2022 11:46
Ramer-Douglas-Peucker implementation
type Point = {
x: number;
y: number;
};
/**
* Compute the perpendicular distance between a point and a line
* @param point
* @param line
*/
@alenaksu
alenaksu / sutherland–hodgman.js
Last active July 16, 2024 01:04
Sutherland–Hodgman algorithm
function isInside(p, [a, b]) {
return (b[0] - a[0]) * (p[1] - a[1]) > (b[1] - a[1]) * (p[0] - a[0]);
}
function getEdges(polygon) {
let edges = [];
for (let i = 0; i < polygon.length; i++) {
let edge = [polygon[(i + polygon.length - 1) % polygon.length], polygon[i]];
const trusted = [],
untrusted = [
'eval',
...Object.keys(window)
].filter(k => trusted.indexOf(k) !== -1);
export default function createSecureScript(script, ...args) {
return new Function(
...untrusted,
...args,

Keybase proof

I hereby claim:

  • I am alenaksu on github.
  • I am alenaksu (https://keybase.io/alenaksu) on keybase.
  • I have a public key ASCDJ8C2NWmHdBP3cDwzEEGpBDeVbDZoqS5rbXc3CTXWBgo

To claim this, I am signing this object:

@alenaksu
alenaksu / curry.js
Created June 19, 2018 10:40
js currying
function curry(fn) {
function nest(...args) {
const next = this.bind(this, ...args);
if (args.length === 0 || next.length === 0) {
return next();
} else {
return nest.bind(next);
}
}
@alenaksu
alenaksu / centroid.js
Created August 29, 2018 14:31
Centroid of a polygon
export function default (polygon) {
let cx = 0, cy = 0, a = 0;
for (let i = 0, l = polygon.length; i < l; i++) {
let x0 = polygon[i].x,
x1 = polygon[(i + 1) % l].x,
y0 = polygon[i].y,
y1 = polygon[(i + 1) % l].y;
let m = (x0 * y1 - x1 * y0);
@alenaksu
alenaksu / polyline.js
Last active February 11, 2019 17:36
Encoded Polyline Algorithm Format
const encodeNumber = (n, precision) => {
// Multiple number by 10^precision to round the result
n = Math.round(n * +`1e${precision}`);
/**
* Number is already in two's complement so just shift left
* by 1-bit and negate if and only if number is negative
*/
n = n < 0 ? ~(n << 1) : n << 1;
@alenaksu
alenaksu / Array.prototype.flat.js
Last active January 14, 2019 12:37
Array.flat polyfill
export const flat = arr => {
// Create a copy of the original array
const result = [...arr];
for (let i = 0; i < result.length; i++) {
while (Array.isArray(result[i])) {
// insert in the current position a copy of the original item
result.splice(i, 1, ...result[i]);
}
}
@alenaksu
alenaksu / config.json
Last active January 18, 2019 17:12
Node.js Telnet Chat Server
{
"server": {
"maxConnections": 100,
"host": "0.0.0.0",
"port": 10000
},
"chat": {
"messageLength": 150,
"messages": {
"welcome": "Welcome to chat server. Press CTRL+C to leave.",