Skip to content

Instantly share code, notes, and snippets.

View premasagar's full-sized avatar

Prem Rose premasagar

View GitHub Profile
@premasagar
premasagar / tim.js
Created August 12, 2010 17:52
A tiny, secure JavaScript micro-templating script. It doesn't use eval or (new Function), so it cannot execute malicious code.
/*
== Tim ==
A tiny, secure JavaScript micro-templating script.
This has now moved to:
github.com/premasagar/tim
*/
@premasagar
premasagar / _gzip.js
Created August 11, 2010 17:57 — forked from bga/_gzip.js
window._gzip = (function()
{
var _matchCount = function(s, word)
{
var n = 0, wordLen = word.length, p = -wordLen;
while((p = s.indexOf(word, p + wordLen)) > -1)
++n;
return n;
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=107931 - painting; portrait
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=109011 - Isaac Newton’s reflecting telescope (replica)
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=107901 - print; portrait
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=126567 - medal
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=55342 - Metal spectacles with small thick oval lenses, as suggested
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=107052 - Newton's furnace, 1696-1727.
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=126565 - Royal Society Medal
http://collectionsonline.nmsi.ac.uk/grabimg.php?kv=122835 - Engraving: The paternal house of Sir Isaac Newton ... born 2
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name, ?depiction, ?thumbnail, ?influences, ?abstract
WHERE {
<http://dbpedia.org/resource/Isaac_Newton> rdfs:label ?name;
foaf:depiction ?depiction ;
<http://dbpedia.org/ontology/thumbnail> ?thumbnail;
<http://dbpedia.org/ontology/abstract> ?abstract.
@premasagar
premasagar / trim.js
Created June 19, 2010 18:54
Fast, complete JavaScript trim
function trim(str){
return str.replace(/^[\0\t\n\v\f\r\s]+|[\0\t\n\v\f\r\s]+$/g, ''); // match the full set of whitespace characters
}
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name, ?description, ?depiction, ?thumbnail, ?scientificName
FROM <http://www.bbc.co.uk/nature/species/Black_Grouse.rdf> {
?species a <http://purl.org/ontology/wo/Species>;
rdfs:label ?name;
dcterms:description ?description;
foaf:depiction ?depiction;
function compile (template) {
function addText (buffer, text, unescaped) {
unescaped = !!unescaped;
buffer.push("\tprint(");
buffer.push(unescaped ? text : "\"" + text
.split("\r").join("\\r")
.split("\n").join("\\n")
.split("\t").join("\\t")
.split("\"").join("\\\"")
+ "\""
@premasagar
premasagar / pngcrush.sh
Created June 12, 2010 19:04
Highest minification with pngcrush
# single image
pngcrush -rem alla -brute -reduce src.png dest.png
# directory of images
pngcrush -rem alla -brute -reduce -d outputdir ./*.png
@premasagar
premasagar / getby.js
Created June 3, 2010 21:18
Pluck an object that contains a key and optional value
// Pluck an object that contains a key and optional value
function getBy(enumerable, findProperty, findValue){
return jQuery.map(enumerable, function(el){
if (typeof el[findProperty] !== 'undefined'){
if (typeof findValue === 'undefined' ||
el[findProperty] === findValue){
return el;
}
}
});
// random number. Default: maximum of 4 digits
function rand(maxDigits){
var m = Math;
return m.floor(
m.random() * m.pow(10, maxDigits || 4)
);
}