Skip to content

Instantly share code, notes, and snippets.

View motss's full-sized avatar
🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!

The web walker motss

🥑
Life is like a prototype, just like JavaScript. So, keep betting on JavaScript!
View GitHub Profile
@motss
motss / 99-bottles-of-beer-on-the-wall.js
Last active October 9, 2018 05:00
99 bottles of beer on the wall
/**
* See {@link http://99-bottles-of-beer.net|99 Bottles of Beer}
*/
function bottles(i) {
return i === 0 ? 'no more bottles' : `${i} bottle${i > 1 ? 's' : ''}`;
}
function beer(i) {
const str = bottles(i);
@motss
motss / to-split-or-not-to-split.js
Created September 13, 2018 14:41
Curious to know how 1e3 HTTPs requests are handled in your browser?
function splitFetch(data, chunk = 50) {
return Promise.all(Array.from(Array(Math.ceil(data.length / chunk)), (_, i) => {
return Promise.all(data.slice(i * chunk, (chunk * (i + 1))).map(() => fetch('https://example.com')));
}));
}
(async () => {
console.time('split');
await splitFetch(Array.from(Array(1e3))).then(console.log).catch(console.error);
console.timeEnd('split');
@motss
motss / crack-the-event-loop-2.js
Last active October 4, 2021 13:17
Crack the event loop
function main() {
console.log('a');
const a = new Promise((resolve) => {
resolve('b');
console.log('c');
}).then(console.log);
setTimeout(async () => {
b();
@motss
motss / kill-pid-addr-in-use.md
Created August 8, 2018 08:39
Kill process that causes issue where address already in use

Kill with PID that hogging a TCP port

# lsof -t -i tcp:<PORT_TO_KILL> -s tcp:listen | sudo xargs kill
$ lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill
@motss
motss / README.md
Last active August 3, 2018 08:26
Logger compatible with Google Stackdriver's with Pino

Usage

// @ts-check

import logger from '<PATH_TO>/logger';

// logger.error(...);
// logger.warn(...);
// logger.debug(...);
@motss
motss / EXPLANATION.md
Last active August 2, 2018 03:13
Compute valid date range w/ or w/o start and/ or end date

Explanation

The basic idea of such implementation is that user can allow to supply either from or to, both or neither.

from to R
0 0 v
0 1 v
1 0 v
1 1 v
@motss
motss / two-sum.js
Last active April 20, 2018 03:23
When going casual on Leetcode...
/**
*
* Given an array of integers,
* return indices of the two numbers such that they add up to a specific target.
*
* You may assume that each input would have exactly one solution,
* and you may not use the same element twice.
*
* Example:
*
@motss
motss / comments.js
Last active February 27, 2018 08:51
LatLng of all countries on the planet
// From https://developers.google.com/public-data/docs/canonical/countries_csv
@motss
motss / simple-random-generator.js
Created January 30, 2018 03:38
Simple random generator
const rdc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const rdi = '1234567890';
const rdl = 'abcdefghijklmnopqrstuvwxyz';
const ipt = '6';
const rd = [
...rdi
];
Array.from(Array(ipt.length)).reduce((p, n) => p + rd[Math.floor(Math.random() * rd.length)], '');
@motss
motss / is-a-number.js
Last active June 27, 2018 02:27
Fast check if an input is a number
// function isANumber(n) {
// return !Number.isNaN(+n)
// && /^\-?\d*(\.\d*)?(e(\+|\-)?\d+)?$/i.test(
// Array.isArray(n) || (typeof n === 'string' && !n.length)
// ? NaN
// : n
// );
// }
/** NOTE: A better version to detect Symbol */