Skip to content

Instantly share code, notes, and snippets.

Avatar

Aleksandr Sergeev sergeevabc

View GitHub Profile
@sergeevabc
sergeevabc / timecmd.bat
Created Nov 29, 2016
Measure execution time
View timecmd.bat
@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 Oct 28, 2014
Validate string against password policy via regex
View validatePassword.js
// 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 Oct 24, 2014
Deduplicate chars in string nLpMLCkDfrDdnLpMLCkDfrDd -> nLpMCkDfr
View dedupString.js
// 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;
});
View consistentShuffle.js
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 Oct 21, 2014
Open link with POST method
View postToURL.js
// <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 Oct 20, 2014
Base16 array to hex
View Base16.js
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;
}
View stringToUTF8.js
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 Aug 29, 2015
Base32 Crockford's edition https://github.com/agnoster/base32-js 0123456789abcdefghjkmnpqrtuvwxyz
View base32crock.js
(function () {
var alphabet = '0123456789abcdefghjkmnpqrtuvwxyz';
var alias = {
o: 0,
i: 1,
l: 1,
s: 5
};
var lookup = function () {
var table = {};
View gist:0dd6673a6197dcc53ca9
// 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 Oct 19, 2014
Count bytes in array
View bytesInArray.js
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;
You can’t perform that action at this time.