Skip to content

Instantly share code, notes, and snippets.

View ArchangelGabriel's full-sized avatar

Gabe Trinidad ArchangelGabriel

View GitHub Profile
@ArchangelGabriel
ArchangelGabriel / machine.js
Last active November 21, 2019 20:55
Generated by XState Viz: https://xstate.js.org/viz
const MyForm = Machine({
id: 'my-form',
initial: 'isClosed',
context: {
firstName: '',
lastName: '',
email: '',
phone: '',
category: '',
message: '',
#customblog .et_pb_column.et_pb_column_1_4:first-child {
width: 71%;
}
#customblog .et_pb_blog_grid .et_pb_post {
float: left;
margin-right: 1%;
max-width: 32%;
margin-bottom: 10px;
}
@ArchangelGabriel
ArchangelGabriel / timeserver.js
Created June 1, 2017 23:26
learnyounode:timeserver
var net = require('net');
String.prototype.padStart = function(pad, padWith) {
return padWith.repeat(pad - this.length) + this;
}
var server = net.createServer(socket => {
var now = new Date();
var YYYY = now.getFullYear().toString().padStart(4, "0");
var MM = JSON.stringify(now.getMonth() + 1).padStart(2, "0");
@ArchangelGabriel
ArchangelGabriel / filterDirByExt.js
Created June 1, 2017 21:21
A small function to filter files in directory by extension.
var fs = require('fs');
module.exports = function(dir, ext, callback) {
fs.readdir(dir, (error, data) => {
if (error) {
callback(error);
} else {
var regex = new RegExp(`\.${ext}$`);
callback(null, data.filter(datum => regex.test(datum)));
}
@ArchangelGabriel
ArchangelGabriel / getElementsByClassName.js
Created April 28, 2017 19:48
Implement document.getElementsByClassName from scratch.
var getElementsByClassName = function(className) {
return getNodesWithClassName(document.childNodes, className);
};
var getNodesWithClassName = function(nodes, className) {
if (nodes.length === 0) return [];
return (hasClassOf(nodes[0], className) ? [nodes[0]] : [])
.concat(getNodesWithClassName(nodes[0].childNodes, className))
.concat(getNodesWithClassName([...nodes].slice(1), className));
};
@ArchangelGabriel
ArchangelGabriel / stringifyJSON.js
Last active April 28, 2017 04:34
Implement JSON.stringify from scratch.
const NUMBER = 'number';
const STRING = 'string';
const OBJECT = 'object';
const BOOLEAN = 'boolean';
var stringifyJSON = function(obj) {
switch(typeof obj) {
case NUMBER:
case BOOLEAN: return `${obj}`;
case STRING: return `\"${obj}\"`;