Skip to content

Instantly share code, notes, and snippets.

@vukicevic
vukicevic / CalendarWidgetGenerator
Created March 15, 2015 23:06
Calendar widget dates generator using ES6
function *calendar (month, year) {
var c = new Date(year, month, 0).getDate();
var l = new Date(year, month - 1, 0).getDate();
var o = new Date(year, month - 1, 1).getDay();
var p = o > 0 ? 1 - o : -6;
var m = 42 + p;
while (p < m)
@vukicevic
vukicevic / readable
Created October 21, 2014 16:03
Human-readable file size from bytes.
function readable($size, $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB')) {
return ($size > 1024) ? readable($size/1024, array_slice($unit, 1)) : round($size, 2) . ' ' . array_shift($unit);
}
@vukicevic
vukicevic / b64StringToByteArray
Last active August 29, 2015 14:00
Base64 encode/eecode byte array
/*
* Input: Base64 encoded string
* Output: Raw byte-array
*/
function decodeStringToArray(s) {
return Array.prototype.map.call(atob(s), function(c) { return c.charCodeAt() });
}
/*
* Input: Raw byte-array
@vukicevic
vukicevic / Latin to Cyrillic Bookmarklet
Last active August 29, 2015 13:58
Latin script to Serbian cyrillic converter bookmarklet, preserves DOM elements.
(function () {
var s, r, t;
function replace(parent) {
var n, w = document.createTreeWalker(parent, NodeFilter.SHOW_TEXT, null, false),
c = ['љ','Љ','њ','Њ','џ','Џ','ч','Ч','ћ','Ћ','ж','Ж','ш','Ш','ђ','Ђ','з','З','е','Е','р','Р','т','Т','у','У','и','И','о','О','п','П','а','А','с','С','д','Д','ф','Ф','г','Г','х','Х','ј','Ј','к','К','л','Л','ц','Ц','в','В','б','Б','н','Н','м','М'],
l = ['lj','Lj','nj','Nj','dž','Dž','č','Č','ć','Ć','ž','Ž','š','Š','đ','Đ','z','Z','e','E','r','R','t','T','u','U','i','I','o','O','p','P','a','A','s','S','d','D','f','F','g','G','h','H','j','J','k','K','l','L','c','C','v','V','b','B','n','N','m','M'];
while (n = w.nextNode())
c.forEach(function(v, i) { n.nodeValue = n.nodeValue.split(l[i]).join(v) });
@vukicevic
vukicevic / Byte length
Created December 31, 2013 13:24
Calculate length, in bytes, of input number. JavaScript.
function byteLength(l) {
return Math.floor(Math.log(l)/Math.log(256))+1;
}
@vukicevic
vukicevic / BER Length
Last active January 1, 2016 20:19
ASN.1 BER length generator. Pass a length l to the function, outputs an array with a BER format length. JavaScript.
function berLength(l) {
var ll, bl;
if (l < 128) {
bl = [l];
} else {
ll = Math.floor(Math.log(l)/Math.log(256))+1;
bl = [128 + ll];
while (ll-- > 0)
@vukicevic
vukicevic / Simple Javascript Templates
Last active January 1, 2016 07:39
Simple javascript template function. Pre-prepared templates are created on the page, and then parsed/compiled. Variables are set with {{var}} and repeating sections with {{#sec}} ... {{/sec}}. Content is passed as JSON.
function TemplateEngine(templ) {
if (typeof templ != "undefined")
templ = document.getElementById(templ).innerHTML.replace(/\n|\r/g, "").replace(/>\s+</g, "><").trim();
return function compile(data, template) {
var match, tpart, i, template = template || templ;
while (match = /\{\{#([a-zA-Z]+)\}\}(.+)\{\{\/\1\}\}/g.exec(template)) {
for (tpart = '', i = 0; i < data[match[1]].length; i++)
tpart += compile(data[match[1]][i], match[2]);
@vukicevic
vukicevic / Draw Bitmap From Int Array
Last active October 31, 2023 08:50
Generate a monochrome bitmap image in JavaScript using a byte-array. The image is generated such that every bit in the array is shown as either a black (1) or white (0) pixel.
/**
* depth: 1 - monochrome
* 4 - 4-bit grayscale
* 8 - 8-bit grayscale
* 16 - 16-bit colour
* 32 - 32-bit colour
**/
function drawArray(arr, depth) {
var offset, height, data, image;
@vukicevic
vukicevic / String to Byte Array conversion
Last active December 26, 2015 18:29
Convert UTF-8 string to and from integer byte-array.
function arrayToString(input) {
return decodeURIComponent(
input.map(function(v) {
return '%'+v.toString(16);
})
.join('')
);
};
function stringToArray(input) {