Skip to content

Instantly share code, notes, and snippets.

View zerobias's full-sized avatar
💭
Set your status

Dmitry zerobias

💭
Set your status
View GitHub Profile
@zerobias
zerobias / luhn.js
Created August 11, 2018 09:59
Luhn algorithm
/**
* Uses Luhn algorithm to validate a numeric identifier.
* @param {String} identifier The identifier to validate.
* @return {Boolean} True if the identifier is valid, false if not.
*/
function isValidIdentifier(identifier) {
var sum = 0,
alt = false,
i = identifier.length-1,
@zerobias
zerobias / gadt as singleton type.re
Created August 5, 2018 22:57
Use cases for GADT in OCaml/Reason
type typ(_) =
| Int: typ(int)
| String: typ(string)
| Pair(typ('a), typ('b)): typ(('a, 'b));
let rec to_string: type t. (typ(t), t) => string =
(t, x) =>
switch (t) {
| Int => string_of_int(x)
| String =>
function State(setup) {'use strict';
return Object.assign(this || new State, setup);
}
State.diff = function diff(prev, curr) {
var map = {}, tmp, keys, i;
// accepts states as both prev, curr and curr, prev
if (map.isPrototypeOf.call(curr, prev)) {
tmp = curr;
'use strict'
// JS treats subjects of bitwise operators as SIGNED 32 bit numbers,
// which means the maximum amount of bits we can store inside each byte
// is 7..
const BITS_PER_BYTE = 7
module.exports = class SparseArray {
constructor () {
this._bitArrays = []
'use strict';
var Sister;
/**
* @link https://github.com/gajus/sister for the canonical source repository
* @license https://github.com/gajus/sister/blob/master/LICENSE BSD 3-Clause
*/
Sister = function () {
var sister = {},
@zerobias
zerobias / raf-scheduler.js
Created August 3, 2018 14:42
requestAnimationFrame based scheduler
// @flow
type WrapperFn = (...args: mixed[]) => void;
type CancelFn = {|
cancel: () => void,
|};
type ResultFn = WrapperFn & CancelFn;
export default (fn: Function): ResultFn => {
let lastArgs: mixed[] = [];
const memwatch = require('memwatch-next');
const prettyBytes = require('pretty-bytes');
function row(name, size, count, total) {
return (
'| ' +
name.padStart(30) +
' | ' +
size.padEnd(10) +
' | ' +
//@flow
function TCROA(iterate) {
return function(initialValue, resolve) {
function next(value) {
setImmediate(() => iterate(value, resolve, next), 0);
}
next(initialValue);
}
@zerobias
zerobias / styled grid mixins.js
Created July 25, 2018 00:00
styled grid mixins
import styled, {css} from 'styled-components'
const gridArea = area => area
? `grid-area: "${area}";`
: ``
const gridAreaMixin = css`
${props => gridArea(props.area)}
`
const CommonButton = styled.button`
function each(obj, iter) {
for(var key in obj) {
var value = obj[key]
iter(value, key, obj)
}
}
function keys (obj) {
return Object.keys(obj).sort()
}