Skip to content

Instantly share code, notes, and snippets.

@fernandocamargo
Created August 31, 2022 16:59
Show Gist options
  • Save fernandocamargo/f9d72ea521a8ca226360eaa6e08b7ef5 to your computer and use it in GitHub Desktop.
Save fernandocamargo/f9d72ea521a8ca226360eaa6e08b7ef5 to your computer and use it in GitHub Desktop.
// Feb 21, 2016, 4:31 AM
// I have no idea why I wrote this
console.clear();
var identities = [];
var identity = function () {
var symbol = '█████';
var color = (this.color || '#000');
var repeated = (identities.indexOf(color) >= 0);
var output = (repeated ? identity() : [`%c${symbol}`, `color: ${color};`]);
return output;
};
var hexadecimal = function () {
var white = 16777215;
var code = Math.floor(Math.random() * white).toString(16);
return `#${code}`;
};
var noop = function () {
return;
};
var value = function () {
return this.value;
};
var array = function (input) {
var empty = ((input === undefined) || (input === null));
var string = (typeof input === 'string');
var prototype = (!empty && !string && (Object.getPrototypeOf(input) !== input.constructor.prototype));
var number = (typeof input === 'number');
var boolean = (typeof input === 'boolean');
var executable = (typeof input === 'function');
var object = ((typeof input === 'object') && !empty && !('length' in input));
var singular = [empty, string, prototype, number, boolean, executable, object];
var valid = !!!singular.filter(Boolean).length;
var collection = (!empty && Array.prototype.slice.call(input));
var collectible = (valid && !!collection.length);
var output = (!collectible ? [input] : collection);
return {
value: value.bind({value: output}),
valid: value.bind({value: valid})
};
};
var post = function () {
var url = this.url;
var data = this.data;
var load = this.load;
var request = new XMLHttpRequest();
var header = 'Content-Type';
var value = 'application/x-www-form-urlencoded; charset=UTF-8';
request.open('POST', url, true);
request.setRequestHeader(header, value);
request.addEventListener('load', load);
return request.send(data);
};
var log = function () {
var type = (this.type || 'log');
var color = this.color;
var prefix = identity.call({color});
var params = array(arguments).value();
var output = prefix.concat(params);
return console[type].apply(console, output);
};
var resolve = function () {
var done = this.done;
var task = this.queue.shift();
var handler = (task || done);
return handler(done.bind(this));
};
var enqueue = function (task) {
return this.queue.push(task);
};
var receive = function (event) {
var target = (event.target || event.currentTarget);
var response = (target.response || target.responseText);
var color = this.color;
var position = this.position;
var item = this.item;
var handler = (!!item ? log : noop);
var context = {color};
handler.call(context, position, item, response);
return resolve.call(this);
};
var retrieve = function (done) {
var url = '/echo/json/';
var timestamp = (new Date()).getTime();
var input = {timestamp};
var json = JSON.stringify(input);
var data = `json=${json}`;
var context = Object.assign(this, {done});
var load = receive.bind(context);
return post.call({url, data, load});
};
var then = function (done) {
var context = Object.assign({}, this, {done});
return resolve.call(context);
};
var to = function (execute) {
var collection = this.collection.value();
var valid = this.collection.valid();
var chain = {then: then.bind(this)};
var iterate = collection.forEach.bind(collection, execute.bind(this));
var skip = execute.bind(this);
var handler = (valid ? iterate : skip);
handler();
return chain;
};
var use = function (input) {
var color = hexadecimal();
var queue = [];
var collection = array(input);
var context = {color, queue, input, collection};
var chain = {to: to.bind(context)};
return chain;
};
var request = function (item, position) {
var context = Object.assign({}, this, {position, item});
return enqueue.call(context, retrieve.bind(context));
};
var warn = function () {
var input = this.input;
var color = this.color;
var context = {color};
return log.call(context, input, 'complete!');
};
var execute = function (item) {
return use(item).to(request).then(warn);
};
var items = [
{foo: 'bar'},
[123, 456, 987, 'a', 'b', 'c'],
'LOL',
undefined,
false,
null,
1,
new Date(),
document.getElementsByTagName('script'),
document.querySelectorAll('meta'),
new RegExp('a'),
Array.prototype,
function () {},
Object,
true
];
items.forEach(execute);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment