Skip to content

Instantly share code, notes, and snippets.

View sergeevabc's full-sized avatar

Aleksandr Sergeev sergeevabc

View GitHub Profile
@sergeevabc
sergeevabc / timecmd.bat
Created November 29, 2016 01:21
Measure execution time
@echo off
:: Get start timestamp
set _time=%time%
set _hours=100%_time:~0,2%%%100
set _min=100%_time:~3,2%%%100
set _sec=100%_time:~6,2%%%100
set _cs=%_time:~9,2%
set /a _started=_hours*60*60*100+_min*60*100+_sec*100+_cs
@sergeevabc
sergeevabc / validatePassword.js
Created October 28, 2014 03:46
Validate string against password policy via regex
// Rules: At leasst 1 uppercase, 1 lowercase, 1 digit, 1 special char from z85 alphabet
// Test: http://regex101.com/r/yP9aU6/1
function validateResult(string) {
var regex = (/^(?=\S*?\w)(?=\S*?\d)(?=\S*?[.\-:+=^!\/*?&<>()\[\]{}@%$#])[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.\-:+=^!/*?&<>()\[\]{}@%$#]+$/gm);
return regex.test(string);
}
@sergeevabc
sergeevabc / dedupString.js
Created October 24, 2014 22:58
Deduplicate chars in string nLpMLCkDfrDdnLpMLCkDfrDd -> nLpMCkDfr
// Maxdamntus.forEach
function dedupString(string) {
var acc = {
str: "",
map: {}
};
Array.prototype.forEach.call(string, function(char) {
if (!acc.map[char.toLowerCase()]) acc.str += char;
acc.map[char.toLowerCase()] = true;
});
@sergeevabc
sergeevabc / consistentShuffle.js
Created October 21, 2014 17:32
Salted variation of Fisher-Yates shuffle http://hashids.org/javascript/
function saltedShuffle(alphabet, salt) {
var integer, j, temp, i, v, p;
for (i = alphabet.length - 1, v = 0, p = 0; i > 0; i--, v++) {
v %= salt.length;
p += integer = salt[v].charCodeAt(0);
j = (integer + v + p) % i;
temp = alphabet[j];
alphabet = alphabet.substr(0, j) + alphabet[i] + alphabet.substr(j + 1);
alphabet = alphabet.substr(0, i) + temp + alphabet.substr(i + 1);
}
@sergeevabc
sergeevabc / postToURL.js
Created October 21, 2014 14:26
Open link with POST method
// <a href="javascript: ... ">Open</a>
function post_to_url(url, params) {
var form = document.createElement('form');
form.action = url;
form.method = 'POST';
for (var i in params) {
if (params.hasOwnProperty(i)) {
var input = document.createElement('input');
input.type = 'hidden';
@sergeevabc
sergeevabc / Base16.js
Created October 20, 2014 14:05
Base16 array to hex
function Uint8Array2hex(arr) {
var hexEncodeArray = '0123456789abcdef'.split('');
var s = '';
for (var i = 0; i < arr.length; i++) {
var code = arr[i];
s += hexEncodeArray[code >>> 4];
s += hexEncodeArray[code & 0x0F];
}
return s;
}
var x = "ляляфа";
var y = unescape(encodeURIComponent(x));
var z = decodeURIComponent(escape(y));
// alternative
var UTF8 = {
encode: function(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
@sergeevabc
sergeevabc / base32crock.js
Last active September 11, 2021 04:41
Base32 Crockford's edition https://github.com/agnoster/base32-js 0123456789abcdefghjkmnpqrtuvwxyz
(function () {
var alphabet = '0123456789abcdefghjkmnpqrtuvwxyz';
var alias = {
o: 0,
i: 1,
l: 1,
s: 5
};
var lookup = function () {
var table = {};
// Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then
// use window.btoa' step. According to my tests, this appears to be a faster approach:
// http://jsperf.com/encoding-xhr-image-data/5
function base64ArrayBuffer(arrayBuffer) {
var base64 = '';
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var bytes = new Uint8Array(arrayBuffer);
var byteLength = bytes.byteLength;
var byteRemainder = byteLength % 3;
@sergeevabc
sergeevabc / bytesInArray.js
Created October 19, 2014 23:03
Count bytes in array
function getByteCount(s) {
var count = 0,
stringLength = s.length,
i;
s = String(s || "");
for (i = 0; i < stringLength; i++) {
var partCount = encodeURI(s[i]).split("%").length;
count += partCount == 1 ? 1 : partCount - 1;
}
return count;