Skip to content

Instantly share code, notes, and snippets.

View rendfall's full-sized avatar
💭
Coffee is a drink, not a language! ☕️

rendfall rendfall

💭
Coffee is a drink, not a language! ☕️
View GitHub Profile
@rendfall
rendfall / random-with-weight.js
Created November 14, 2017 23:44
Generate a weighted random number
// Source: https://stackoverflow.com/a/8435261
function weightedRand(spec) {
let i, j, table = [];
for (i in spec) {
// The constant 10 below should be computed based on the
// weights in the spec for a correct and optimal table size.
// E.g. the spec {0:0.999, 1:0.001} will break this impl.
for (j = 0; j < spec[i] * 10; j++) {
table.push(i);
@rendfall
rendfall / i18n.service.js
Last active October 12, 2017 15:58
Angular 2+ - internationalization with Polyglot.js
import { Injectable } from '@angular/core';
const Polyglot = require('node-polyglot'); // https://www.npmjs.com/package/node-polyglot
// Example of pl_pl.json file: { "Hello world":"Witaj świecie" }
let dictionaries = {
pl_pl: require('./pl_pl/pl_pl.json'),
en_gb: require('./en_gb/en_gb.json')
};
@rendfall
rendfall / cached-service.ts
Created September 28, 2017 22:23
Angular 2+ cache observable http result data
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, ReplaySubject } from 'rxjs';
@Injectable()
export class CachedService {
data$: Observable<any> = this.dataSubject.asObservable();
private dataSubject = new ReplaySubject<any>(1);
@rendfall
rendfall / angular-tabs.component.ts
Created September 28, 2017 15:30
Angular 2+ tabsComponent
import { AfterContentInit, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
interface TabInterface{
active: boolean;
name: string
}
@Component({
selector: 'app-tab',
template: `<div class="tab" [hidden]="!active"><ng-content></ng-content></div>`,
@rendfall
rendfall / get-only-modified-values.js
Created May 31, 2017 23:44
Get only modified values by comparing objects (naive version)
function getOnlyModifiedValues(originalValues, newValues) {
let isObject = (o) => (typeof o === 'object' && o !== null);
let isEquals = (a, b) => (String(a).trim() === String(b).trim());
let isEmpty = (o) => (Object.keys(o).length === 0);
function diff(a, b) {
return Object.keys(b).reduce((value, key) => {
let v1 = a[key];
let v2 = b[key];
@rendfall
rendfall / flexbox-grid.scss
Created May 21, 2017 23:10
Simple flexbox grid
@mixin element($e) {
&__#{$e} { @content; }
}
@mixin modifier($e) {
&--#{$e} { @content; }
}
.grid {
$gutter: 30px;
@rendfall
rendfall / dom-helper.js
Created March 31, 2017 11:34 — forked from SitePointEditors/dom-helper.js
Mini jQuery, sort of.
/**
* A collection of helper prototype for everyday DOM traversal, manipulation,
* and event binding. Sort of a minimalist jQuery, mainly for demonstration
* purposes. MIT @ m3g4p0p
*/
window.$ = (function (undefined) {
/**
* Duration constants
* @type {Object}
@rendfall
rendfall / hosts.bat
Created January 19, 2017 23:02
CMD batch without UAC confirmation
runas /profile /user:Administrator /savecred "notepad C:\Windows\System32\drivers\etc\hosts"
@rendfall
rendfall / invoke-once.js
Last active November 16, 2016 15:15
invokeOnce
function invokeOnce($element, eventType, handler, useCapture = false) {
var args = arguments;
$element.addEventListener(eventType, function selfRemoving(event) {
event.target.removeEventListener(event.type, selfRemoving, useCapture);
return handler.apply(handler, args);
});
}
@rendfall
rendfall / get-average-color.js
Last active October 19, 2016 01:52
Get average color from image
(function (root) {
var canvas = document.createElement('canvas');
var context = canvas.getContext && canvas.getContext('2d');
var DEFAULT_RGB = { r: 0, g: 0, b: 0 };
function extractRGB(imageData) {
var len = imageData.data.length;
var rgb = { r: 0, g: 0, b: 0 };
var blockSize = 5; // Check every 5 pixels
var i = -4;