Skip to content

Instantly share code, notes, and snippets.

View lsauer's full-sized avatar
🎯
Focusing

Lorenz Lo Sauer lsauer

🎯
Focusing
View GitHub Profile
@lsauer
lsauer / gist:1247072
Created September 28, 2011 05:38
jquery doesn't parse JSON with comments
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
$.getJSON('b8irg1.json', function(data) { console.log(data);});
will only contain an responseText property but not the JSON.parse'd data appended if the JSON file contains comments of the kind '// or /*...*/'
Note tried with jquery 1.6.4
@lsauer
lsauer / gist:1247582
Created September 28, 2011 10:19
Employee list - anonymized for statistical evaluation
//author: lsauer.com
//Data is in the form
//[ ["JOEBRA","Doeduardo","9.9.1999","Midwaystreet 12","1160 Vienna", "Austria" ],...]
//if you need the name fully anonymized shuffle the name before substr or calculate a Soundex of it
for(i=0; i<x.autpolData.length; i++){
tmp[i] = [].concat(x.autpolData[i][1].substr(0,4), x.autpolData[i][2].split('.')[2], x.autpolData[i].slice(3) )
}
//["Doed", "1948", "Midwaystreet 12", "1160 Vienna", "Austria"
//the address is resolved to GPS lat/long -> see my scripts_n_snips folder
//often times JS is faster and more powerful than an editor with RegExp
@lsauer
lsauer / gist:1259821
Created October 3, 2011 18:15
RegEx - matching numbers only in select regions (JS RegEx vs Python PCRE)
#author: lo sauer 2011 - lsauer.com
#demonstrating the difference in RegEx engines
Python:
import re
a="123 test 42 <!-- comment 345 commentEnd --> 678 test2 348"
filter(None, re.findall('(?:\<\!--.+\d+.+--\>)|(\d+)',a))
#With RegEx lookbehinds; for demonstration only - not very practical (slow!)
re.findall('(\d+)(?!(?:[^<]+|<(?!!--))*-->)',a))
@lsauer
lsauer / gist:1259865
Created October 3, 2011 18:36
Filter empty elements from an Array or csv string/file
//lo sauer 2011 - lsauer.com
//join() is by default: join(',')
[1,2,,3,,3,,,0,,,4,,4,,5,,6,,,,,8,,,-2].filter(String).join()
>>>"1,2,3,3,0,4,4,5,6,8,-2"
//same result for: filter(function(){return 1});
[1,2,,3,,3,,,0,,,4,,4,,5,,6,,,,,8,,,-2].filter(function(){return 1}).join()
>>>"1,2,3,3,0,4,4,5,6,8,-2"
//to conveniently eliminate null-elements...
@lsauer
lsauer / gist:1260006
Created October 3, 2011 19:32
PHP's trim(chars) in JS and mapping RegExp matches
//author: lo sauer 2011 - lsauer.com
//implementation of PHP's trim
//see http://php.net/manual/en/function.trim.php
//bug: doesn't work with ']' in the charlist
//@param: charlist <string>
String.prototype.phptrim = function(s)
{
if(0==arguments.length)
return this.valueOf().trim();
@lsauer
lsauer / gist:1260083
Created October 3, 2011 20:04
JavaScript compensating for RegExp global non capturing groups
//author: lo sauer 2011
//given for instance, is a text with placeholders of which only the value is to be captured
//JS's 'match' ignores the non-capture group (?:...), for instance in a global search
str = "Hello I am {{name}}. Is it already the {{date}}";
str.match(/(?:\{{2})([a-z0-9]*)(?:\}{2})/i);
>>>["{{name}}", "name"]
str.match(/(?:\{{2})([a-z0-9]*)(?:\}{2})/gi);
>>>["{{name}}", "{{date}}"]
@lsauer
lsauer / gist:1264847
Created October 5, 2011 16:09
JSON.stringify: Forcing Array notation for JavaScript's serialized function arguments
//author: lo sauer 2011, lsauer.com
//description: Forcing JSON array notation on array-like object (i.e. arguments, DOMCollection)
//application: e.g. JSON-RPC
//Take for instance this simple function, which returns a pure JS object notation
function x(){console.log(JSON.stringify(arguments))}
x(1,2,3,"sfdsf", [1,2,3], {1:2})
>>>[{"0":1,"1":2,"2":3,"3":"sfdsf","4":[1,2,3],"5":{"1":2}}]
//There is no straightforward Array typecasting in JS; Array() will nest the object in an array
@lsauer
lsauer / gist:1269901
Created October 7, 2011 09:36
DOM Storage - Browser Storage types overview and examples
//author:lo sauer 2011; lsauer.com
"DOM Storage is the name given to the set of storage-related features"
see here: https://developer.mozilla.org/en/DOM/Storage
==LOCAL STORAGE/SESSION STORAGE==
Cookies result in HTTP overhead
HTML5 solutions:
Local Storage & Session Storage
*Hash key/value store
*5MB of data across browsers / per site (W3C Specification)
@lsauer
lsauer / gist:1278258
Created October 11, 2011 14:38
Importing a SQL database file without DATABASE CREATE information
//instructions are especially for mysql:
//lo sauer, 2011
1. create the database: 'CREATE DATABASE `enzymes`;'
2. now either select the database with 'USE `enzymes`;' or put the same command at the beginning of your sql-file.
in the latter case run sqlimport which is a shortcut for the more powerful command LOAD DATA INFILE http://dev.mysql.com/doc/refman/5.1/en/load-data.html
' mysqlimport -u root -p --local enzymes enzymes.sql'
Or in mysql (e.g. mysql -u root -p) you can use the LOAD DATA INFILE Command
@lsauer
lsauer / gist:1278532
Created October 11, 2011 16:10
PHP accessing numeric properties from an object to allow e.g. obj(explode("-","my-test-test"))->_0
//lo sauer, 2011 - lsauer.com
PHP variable names can't start with a digit. As such non-associative array conversion can pose a problem.
Additionally stdClass does not implement ArrayAccess - as PHP's base class.
//note: this is not serialized php code
$var = stdClass Object
(
[0] => stdClass Object
(
['my'] => 1442,