Skip to content

Instantly share code, notes, and snippets.

View ryasmi's full-sized avatar
🐿️
Checks emails once per day Mon-Fri

Ryan Smith ryasmi

🐿️
Checks emails once per day Mon-Fri
View GitHub Profile
@ryasmi
ryasmi / _setGlobalLogLevel.md
Last active December 10, 2020 18:13
A function to set the global log level in Node/Browser.

In PM2 you can use the PM2 time option to add timestamps to logs. In a Lambda, it automatically adds the timestamp to CloudWatch. I think the log level is also shown in CloudWatch. Ideally PM2 would output the level of the log too.

@ryasmi
ryasmi / testRunner.js
Created August 20, 2018 11:40
Starting a potential test runner that doesn't provide an opinionated assertion library.
createRunner = () => {
const beforeAlls = [];
const afterAlls = [];
const beforeEaches = [];
const afterEaches = [];
const tests = [];
const test = (description, process) => {
tests.push({
description,
@ryasmi
ryasmi / code-habit-const-functions-1.js
Last active August 7, 2018 16:10
Code Habit - Const Functions
// Preventing hoisting. We're used to reading top to bottom, so this can be quite confusing.
const result = add(1, 2);
function add(x, y) {
return x + y;
};
console.log(result);
import * as lodash from 'lodash';
import * as moment from 'moment';
// tslint:disable-next-line:no-class
class A {
public constructor() { return; }
}
interface B {
readonly c: number;
@ryasmi
ryasmi / js-mushroom-episode-3-servers.md
Last active March 23, 2018 16:55
JS Mushroom Episode 3 - Servers (2018-03-23)

First of all, make sure you have Node installed on your computer, I'd recommend using NVM. Then create a new folder and make a "index.js" file with the following contents.

const http = require('http');

const server = http.createServer((request, response) => {
  response.end('hello');
});

server.listen(1337, () => {
app = async ({count,fraction}) => {
const seconds = Math.floor(count / fraction);
const div = document.createElement('div');
const span = document.createElement('span');
span.innerText = `${seconds}.${count % fraction}`;
div.style.color = 'white'
div.appendChild(span);
return div;
};
client = () => {
@ryasmi
ryasmi / js-mushroom-episode-1-array-functions.js
Last active February 23, 2018 15:44
JS Mushroom Episode 1 - Array Functions (2018-02-09)
// Using forEach.
[1,2,3].forEach((item, index, items) => {
console.log('item', item); // Logs "1" for the first item, "2" for the second item, and "3" for the third item.
console.log('index', index); // Logs "0" for the first item, "1" for the second item, and "2" for the third item.
console.log('items', items); // Logs "[1,2,3]" to the console for every item.
});
// Using map to add one to each item in an array.
const plusOneItems = [1,2,3].map((item, index, items) => {
return item + 1;
@ryasmi
ryasmi / js-mushroom-episode-2-promises.js
Last active February 23, 2018 15:44
JS Mushroom Episode 2 - Promises (2018-02-23)
// Using resolved promises with the .then callback.
Promise.resolve('hello world').then((message) => {
console.log(message); // Logs "hello world" in the console.
});
// Using rejected promises with the .catch callback.
Promise.reject('error message').catch((err) => {
console.error(err); // Logs "error message" in the console.
});
@ryasmi
ryasmi / renameAll.sh
Created February 23, 2018 11:10
Script to rename all files recursively in a directory on a Mac.
for file in $(find src -name '*.js'); do mv "$file" "${file%.js}.ts"; done
escapedChar = (char) => {
return `\\${char}`;
}
normChars = `w_~!$&'()*+,;=:-`.split('').map(escapedChar).join('');
otherChars = '\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF\\uFFEF-\\uFFFD';
allChars = `${normChars}${otherChars}`;
extChars = `(?:[${allChars}]|(?:\\%[a-f0-9][a-f0-9]))`;
capturePattern = (pattern) => {