Skip to content

Instantly share code, notes, and snippets.

const traverseTCPStates = eventList => {
let state = "CLOSED";
const _states = {
"CLOSED" : [
{ event: "APP_PASSIVE_OPEN", output: "LISTEN" },
{ event: "APP_ACTIVE_OPEN", output: "SYN_SENT"}
],
"LISTEN": [
{ event: "RCV_SYN", output: "SYN_RCVD" },
{ event: "APP_SEND", output: "SYN_SENT" },
@exbotanical
exbotanical / uri-builder.js
Created July 20, 2020 16:24
code golf, FP and Objects edition!
class UriBuilder {
constructor(uri) {
this.root = uri;
this.url = this.root.split("?");
this.params = {};
UriBuilder.populate.apply(this);
};
static populate() {
if (!this.url.length < 2) {
@exbotanical
exbotanical / destructurings.js
Created July 19, 2020 15:54
experiments with destructuring
const weatherInfo = t => Object.values(((c) => ({ r: `${c} is ${c > 0 ? "above ": ""}freezing temperature` }))(((m) => (m - 32) * (5/9))(t)))[0];
// calc f => c
@exbotanical
exbotanical / dynamic-arr-eval.js
Last active July 19, 2020 08:07
code golf cont'd
const findInArray = (array, iterator) => {
for (let [i, v] of Object.entries(array)) {
if (iterator(v, Number(i))) {
return Number(i);
}
}
return -1;
};
/*
@exbotanical
exbotanical / alternated-weight.js
Last active July 18, 2020 05:17
more codegolf
const rowWeights = arr => {
let a = 0;
let b = 0;
Object.entries(arr).forEach(([i, val]) => {
i % 2 === 0 ? a += val : b += val;
});
return [a, b];
}
/*
@exbotanical
exbotanical / quines.js
Created July 9, 2020 20:41
Code Golf with JavaScript
/* Nodejs Quines */
// everything is wrapped in its own execution context so you can copy + paste the entire snippet without conflicts;
// else, the liberal use of semicolons (and in some instances, the lack thereof) will confuse the interpreter and throw exceptions
// IIFE ES6 quine
{
(x=_=>console.log(`(x=${x})();`))();
// 36 bytes
/* Snippets: Higher-order Functions and Functional Programming Patterns */
/* Execute function at N interval */
const setNIntervals = (fn, delay, rounds) => {
if (!rounds) {
return;
}
setTimeout(() => {
fn();
@exbotanical
exbotanical / custom-event-binder.js
Last active July 7, 2020 23:56
A custom implementation of the EventEmitter class
/* Simulates EventEmitter base, binds to and extends a given type
*
*/
class EventBinder {
listeners = {};
addListener(eventName, fn) {
// eval if listener has already been registered
this.listeners[eventName] = this.listeners[eventName] || [];
this.listeners[eventName].push(fn);
@exbotanical
exbotanical / walk-nested-obj.js
Last active July 7, 2020 23:56
How to Find Props on a Nested Object (without needing to know what the Object looks like)
// Code snippets from an article I wrote on working with JavaScript Objects
// canonical link: https://goldmund.sh/entries/work-with-javascript-objects-like-a-pro
// medium: https://medium.com/@exbotanical/work-with-javascript-objects-like-a-pro-876437a6a668
/* How to Find Props on a Nested Object (without needing to know what the Object looks like) */
const nestedPerson = {
metadata: {
id: 123456789,
@exbotanical
exbotanical / custom-logger.js
Last active July 7, 2020 23:57
A logger module with a configurable prototype for dynamic output formatting (including colored).
#!/usr/bin/env node
/**
* @param {Object} config A configurations object for method mappings and options thereof.
* @summary A logger module with a configurable prototype for dynamic output formatting.
* @description This logger module enables the user to instantiate a customized logging utility
* by passing in a configurations object. This configurations object is processed by the
* constructor, wherein values then serve as mappings, the methods and properties of which are dynamically applied.
* Ergo, this module allows the extension and augmentation of the `Console` object to create indeterminate, arbitrary
* methods.