Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View Yaffle's full-sized avatar

Viktor Yaffle

View GitHub Profile
@Yaffle
Yaffle / URLUtils.js
Last active September 5, 2022 02:19
parse URL + absolutize URL in javascript (URLUtils shim - http://url.spec.whatwg.org/#url)
/*jslint regexp: true, maxerr: 50, indent: 2 */
(function (global) {
"use strict";
function URLUtils(url, baseURL) {
var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
if (!m) {
throw new RangeError();
}
// http://www.w3.org/TR/html5/association-of-controls-and-forms.html#concept-form-submit
// FormData (to support files?) FormData.append... (only POST + not string result???)
function serializeForm(form, submitter, useFormData) {
useFormData = useFormData && window.FormData;
var results = useFormData ? (new FormData()) : [], tagName, i, opts, j, field, elements = form.elements;
for (j = 0; j < elements.length; j++) {
field = elements[j];
tagName = field.tagName.toLowerCase();
if (tagName === 'object' || (tagName === 'input' && (field.type === 'image' || (!useFormData && field.type === 'file')))) {
return null;
@Yaffle
Yaffle / convertPointFromPageToNode.js
Last active May 17, 2023 08:53
function to get the MouseEvent coordinates for an element that has CSS3 Transforms
/*jslint plusplus: true, vars: true, indent: 2 */
/*
convertPointFromPageToNode(element, event.pageX, event.pageY) -> {x, y}
returns coordinate in element's local coordinate system (works properly with css transforms without perspective projection)
convertPointFromNodeToPage(element, offsetX, offsetY) -> {x, y}
returns coordinate in window's coordinate system (works properly with css transforms without perspective projection)
*/
@Yaffle
Yaffle / gist:1284012
Last active April 2, 2018 08:03
javascript base64 encode/decode
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script type="text/javascript">
var nativeAtob = window.atob;
var nativeBtoa = window.btoa;
// http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#atob
(function (global) {
"use strict";
@Yaffle
Yaffle / gist:1287361
Last active November 24, 2021 13:24
crc 32 in javascript
function crc32(s/*, polynomial = 0x04C11DB7, initialValue = 0xFFFFFFFF, finalXORValue = 0xFFFFFFFF*/) {
s = String(s);
var polynomial = arguments.length < 2 ? 0x04C11DB7 : (arguments[1] >>> 0);
var initialValue = arguments.length < 3 ? 0xFFFFFFFF : (arguments[2] >>> 0);
var finalXORValue = arguments.length < 4 ? 0xFFFFFFFF : (arguments[3] >>> 0);
var table = new Array(256);
var reverse = function (x, n) {
var b = 0;
@Yaffle
Yaffle / gist:1316257
Created October 26, 2011 12:52
Permutation#Generation_in_lexicographic_order
// http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
// callback(permutation, even)
function permutations(n, callback) {
n >>>= 0;
var p = n ? [] : null,
even = true;
for (i = 0; i < n; i++) {
p[i] = i;
}
@Yaffle
Yaffle / gist:1336740
Created November 3, 2011 15:14
javascript Approximate string matching
// http://en.wikipedia.org/wiki/Approximate_string_matching
function fuzzySearch(t, p) { // returns minimum edit distance between substring of t and p
var a = [], // current row
b = [], // previous row
pa = [], // from
pb = [],
s, i, j;
for (i = 0; i <= p.length; i++) {
s = b;
b = a;
function strtolower_utf8($string) {
$convert_to = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
"з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
"ь", "э", "ю", "я"
);
$convert_from = array(
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
@Yaffle
Yaffle / gist:1381713
Created November 21, 2011 05:15
udpproxy in node.js
/*jslint node: true, indent: 2 */
"use strict";
var httpPort = 4022;
var http = require('http');
var dgram = require('dgram');
var querystring = require('querystring');
CREATE TABLE IF NOT EXISTS `rabota_ids` (
`ID` varchar( 255 ) NOT NULL DEFAULT '',
`region` varchar( 1 ) NOT NULL ,
`parsed` varchar( 1 ) DEFAULT '0',
`added` date NOT NULL ,
UNIQUE KEY `ID` ( `ID` )
) ENGINE = MYISAM DEFAULT CHARSET = utf8;
ALTER TABLE `zarplata_v` CHANGE `source` `source` ENUM( '0', '1', '2', '3', '4' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0';
ALTER TABLE `zarplata_r` CHANGE `source` `source` ENUM( '0', '1', '2', '3', '4' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0';