Skip to content

Instantly share code, notes, and snippets.

@caub
caub / scoped.js
Last active July 24, 2016 16:16
<style scoped> polyfill
(function(){
let scopedRule = (scope, {selectorText: selector='', cssText:css, style: {cssText}={}, styleSheet, cssRules, media}) =>
styleSheet? // @import rules
scopedRules(scope, styleSheet.cssRules):
cssRules && media? // @media rules
`@media ${Array.from(media).join(',\n')} {${scopedRules(scope, cssRules)}}`:
!selector||selector.startsWith(':root')||selector.startsWith('body')?
css:
`${selector.split(',').map(s=>`${scope} ${s}`).join(', ')} {${cssText}}`;
//`${selector.replace(/([^,]+)/g, (_, s) => `${scope} ${s}`)} {${cssText}}`;
@caub
caub / static.js
Last active August 16, 2016 14:15
const babel = require('babel-standalone');
const fs = require('fs');
const express = require('express');
const app = express();
app.get('/*.jsx', function(req, res){
fs.readFile('.'+req.path, (err,data)=>{
res.send(babel.transform(data, {presets:['react']}).code);
});
});
@caub
caub / JS wishlist.md
Last active November 28, 2017 23:45
Things I'd love to see in ES....
  • Shortening const to something like ref, val, cst because

    • it's the most frequently used keyword
    • conflicts with console.log auto-completion
  • null should be Object.freeze(Object.create(null)) or a special Symbol.nullable

  • ([] =~= []) // true shallow compare

  • ({ a: {} } =*= { a: {} }) // true deep compare

  • Making function arguments flexible and coherent with arrays

@caub
caub / co.js
Last active November 2, 2016 15:33
coroutine
module.exports = function(gen) { // coroutine, equivalent to https://github.com/tj/co
const it = gen();
return Promise.resolve().then(function pump(v) {
const next = it.next(v);
if(next.done) return next.value;
return Promise.resolve(next.value).then(pump, it.throw.bind(it));
});
};

Double vs Single quotes for JavaScript Strings

"foo"

  • Newcomers will already be familiar with double quotes from their language. In English, we must use double quotes " to identify a passage of quoted text. If we were to use a single quote ', the reader may misinterpret it as a contraction. The other meaning of a passage of text surrounded by the ' indicates the 'colloquial' meaning. It makes sense to stay consistent with pre-existing languages, and this may likely ease the learning and interpretation of code.
  • Double quotes eliminate the need to escape apostrophes (as in contraptions). Consider the string: "I'm going to the mall", vs. the otherwise escaped version: 'I'm going to the mall'.
  • Double quotes mean a string. When you learn a new language like Java, Python, or C, double quotes are always used. This is because, as mentioned above, double quotes have always been used in language to indicate quoted passages of text. Old books will use double quo
@caub
caub / import.js
Last active January 1, 2018 22:12
browser shim for window.import()
window.import = function (transformer) {
const ROOT = location.origin;
const REQUIRE_CACHE = new Map();
const RE_EXT = /\.js\w*$/;
function fqn(url, dirname) {
const {href} = new URL(url.startsWith('http') ? url : (url.startsWith('/') ? ROOT : dirname) + url),
last = href.lastIndexOf('/'), dir = href.slice(0, last+1), name = href.slice(last+1),
filename = name ? (RE_EXT.test(name) ? name: name+'.js') : 'index.js';
return {dir, abs: dir+filename};
@caub
caub / my.sql
Last active August 29, 2017 21:01
versioning
create table foo_version (
id VARCHAR(32),
v BIGINT,
data TEXT,
PRIMARY KEY (id, v)
);
create table foo (
id VARCHAR(32),
@caub
caub / jQuery4.js
Last active September 30, 2018 21:40
v4.1
function $(container, selector) {
const els =
typeof selector === 'string'
? container.querySelectorAll(selector)
: typeof container === 'string' ? document.querySelectorAll(container) : [container];
const fns = {
removeClass(...cls) {
els.forEach(el => {
el.classList.remove(...cls);
// 2 globals defined at boot of nodejs engine
rl = cb => require('readline').createInterface({input:process.stdin}).on('line',cb);
print = console.log;
/*
Let's call an integer "repetitive" if it contains two of the same digit in a row. For example, 12232 is repetitive but 1232 is not.
Given a positive integer n < 10^15, return the number of nonrepetitive integers in [1..n].
*/
// prog
rl(s=>{r=0;d=s.length-1;for(i=0;i<=d;i++){r+=(i>0&&s[i-1]<s[i]?s[i]-1:s[i])*9**(d-i);if(s[i-1]==s[i]){r--;break}}for(i=0;i<d;i++)r+=9**i;print(r)})