Skip to content

Instantly share code, notes, and snippets.

@andrejewski
andrejewski / noid.js
Created October 8, 2017 02:26
Add an annoying particle on the screen of a webpage
document.body.appendChild((function () {
var e = document.createElement('div');
e.style = 'background-color: #000; position: fixed; right: 30vw; top: 40vh; width: 2px; height: 4px';
return e;
})())
@andrejewski
andrejewski / can-map-spec.js
Created October 23, 2017 19:18
Clojure Spec, but for CanJs
export function getNestedChecks (check) {
if (typeof check !== 'object') {
return check;
}
return (x, key) => {
const props = Object.keys(check);
for (let i = 0; i < props.length; i++) {
const prop = props[i];
const subCheck = getNestedChecks(check[prop]);
@andrejewski
andrejewski / raj-flat-structure.js
Created December 2, 2017 20:24
Raj program husk with separated concerns
import request from './shared-request'
export function assemble (deps, {data, view, logic}) {
return {view, ...logic(data(deps))}
}
export function data (deps) {
return {
getUser (userId) {
return dispatch => {
@andrejewski
andrejewski / css-naming-convention.md
Last active December 22, 2017 13:50
My CSS naming convention

My convention for CSS naming is:

  • For a component, pascal-case (capitalized + camel-case) the root class name, Button
  • For any other element classes within that component, use the component name followed by a dash - and then the element'c class name in dash case icon, so Button's icon would be Button-icon.
  • For modifiers, meaning classes that override styles from a base class name, use the component name followed by two dashes -- and then the class name in dash case shopping-cart, so Button's icon style overrides for the shopping cart icon would be Button-icon--shopping-cart.

A button component with an icon and text:

<button class='Button Button--primary'>
@andrejewski
andrejewski / raj.clj
Created December 29, 2017 05:39
Raj ported to Clojure/Script
(defn program [{:keys [init, update, done, view]}]
(let [state (atom nil)
running (atom true)
change (fn [dispatch [newState effect]]
(do
(if effect
(effect dispatch))
(reset! state newState)
(view @state dispatch)))
dispatch (fn dispatch [message]
@andrejewski
andrejewski / tagged-union.ts
Created January 6, 2018 15:51
Tagged union in TypeScript
interface Tag {
kind: string
data: any
}
interface Union {
match<T>(tag: Tag, handleMap: { [kind: string]: (data: any) => T }, catchAll?: () => T): T,
tags: { [kind: string]: (data: any) => Tag }
}
@andrejewski
andrejewski / example.js
Last active January 12, 2018 21:20
A Can Map powered by a Raj runtime
import Component from 'can-component'
import {program} from './raj-can-map'
import Map from 'can-map' // or whatever Map-like thing
import stache from 'can-stache'
export default Component.extend({
tag: 'my-app',
template: stache(`
<p>{{count}}</p>
<button on:click="increment()">Increment</button>
@andrejewski
andrejewski / a-refactoring.md
Created February 16, 2018 02:50
A code refactoring with commentary

A refactoring

This is a refactoring where an example task goes through rounds of iteration, improving the design with commentary.

Our goal is to create a service for sending email. We need a set of options that fit into two sets:

  • Service options, that will remain constant for the service's lifetime, like transport configuration.
  • Per-email options, that will be used to send a single email.

We separate the two for ergonomics.

@andrejewski
andrejewski / can-portal.js
Created February 21, 2018 15:20
Portals for Can.js (V2)
import Map from 'can/map/';
import Component from 'can/component/';
// Range methods from https://github.com/yapplabs/ember-wormhole/blob/f6bdea79ee1d9b3ef6b7f19e378e3bfb103b2e5e/addon/components/ember-wormhole.js
function appendRange (destinationElement, firstNode, lastNode) {
while(firstNode) {
destinationElement.insertBefore(firstNode, null);
firstNode = firstNode !== lastNode ? lastNode.parentNode.firstChild : null;
}
}
@andrejewski
andrejewski / a-small-refactor.js
Created February 22, 2018 19:30
A small refactor
// A quick refactor
// 1
function foo (fieldValue) {
//If the fieldValue is a number, use ID
//Otherwise, use codeName
//Postgres gets grouchy about comparing strings to numbers
let fieldName;
try {
const parsed = parseInt(fieldValue);