This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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. | |
| * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** .iter() */ | |
| Object.defineProperty(Array.prototype, 'iter', { | |
| value: function arrayIter() { | |
| const ctx = this; | |
| return ctx.values(); | |
| }, | |
| }); | |
| /** .sum() after calling .iter() */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * ```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`. | |
| * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| root = true | |
| [*] | |
| indent_style = space | |
| indent_size = 2 | |
| end_of_line = lf | |
| charset = utf-8 | |
| trim_trailing_whitespace = false | |
| insert_final_newline = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |