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 / number-in-range.js
Last active July 23, 2019 03:40
Determine if a given number is in range without using comparison branches and operators
/**
* This was intended for checking that an ASCII character is in range.
* But this turns out to be a function that can be used for checking if a number is in range too.
*
* Assuming, `MIN` and `MAX` are hardcoded to the code point of the character 'a' and
* the character 'z', respectively, the following equation will always return positive integers
* in the difference of the `MIN` and the `MAX`:
*
* (MAX - x) and (x - MIN) must always be positive for any of the results.
*
@motss
motss / uuid-v4-browser.js
Created July 3, 2019 03:33
UUID v4 for browsers
/** See https://bit.ly/2gvCcqe to learn more about UUID v4 for browsers */
function uuidv4() {
if (window.crypto && window.crypto.getRandomValues) {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
@motss
motss / iterators-with-extensions.js
Last active July 1, 2019 14:31
Iterators with extensions
/** .iter() */
Object.defineProperty(Array.prototype, 'iter', {
value: function arrayIter() {
const ctx = this;
return ctx.values();
},
});
/** .sum() after calling .iter() */
@motss
motss / format-local-date-with-short-tz.js
Last active July 30, 2019 03:36
Format local date with short timezone
/**
* ```json
* e.g. 10:21:02 GMT+0700 (インドシナ時間)
* format {time} {timezone} ({timezone_name})
* ```
*
* Note that `timezone_name` will follow the system language but
* that does not affect our purpose here as we only need `time`
* and `timezone`.
*
@motss
motss / ref-cycles.js
Last active June 25, 2019 05:01
Reference cycles in JavaScript
/**
* Reference cycles can be hard to detect in normal program logic.
* It's even harder to print the entire linked list when it has its own item pointing to its first item,
* thus creating a circular linked list.
*
* However, it looks like V8 has a clever method on printing a circular data structure.
*
* In contrast, a handwritten printing mechanism fails to do so since
* there is always an item linked to the end of the list. This overflows the stack and
* crashes the browser. A simple way to deal with this is to make sure that no any other
@motss
motss / fib.js
Created June 10, 2019 04:13
Fibonacci sequence in Rust and JavaScript
function fib(nth) {
const vec = [0, 1];
let i = 2;
while (i <= nth) {
const val = vec[0] + vec[1];
vec[0] = vec[1];
vec[1] = val;
@motss
motss / LitElement_mobx.ts
Last active January 14, 2023 21:18
Best of both worlds: lit-element + mobx
import { html, LitElement } from 'https://unpkg.com/lit-element@latest/lit-element.js?module';
import { observable, action, autorun, reaction } from 'https://unpkg.com/mobx@latest/lib/mobx.es6.js?module';
const store = observable({
title: 'Hello, World!',
count: 0,
incrementCount: action(() => {
store.count += 1;
}),
@motss
motss / .editorconfig
Last active February 11, 2019 05:38
Setting ESLint up for TypeScript
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
@motss
motss / bench-node-childnodes-vs-document-queryselectorall.js
Created January 31, 2019 10:06
Simple benchmarks - `Node.childNodes` vs `Document.querySelectorAll`
const df = Array.from(Array(10), (_, i) => {
const a = document.createElement('div');
a.classList.add('item');
return a;
}).reduce((p, n) => (p.appendChild(n), p), document.createDocumentFragment());
const container = document.createElement('div');
container.classList.add('container');
@motss
motss / compute-week-number-by-type.js
Last active December 7, 2018 15:13
Compute week number based on week number type
// Basic table:
//
// 0 - first full week
// 1 - first day of year
// 2 - first 4-day week
// 0 1 2 wk
// 2010-01-01 - 52 1 52 5
// 2011-01-01 - 52 1 52 6
// 2012-01-01 - 1 1 1 0
// 2013-01-01 - 53 1 1 2