Skip to content

Instantly share code, notes, and snippets.

View sym3tri's full-sized avatar
🌴
On vacation

Ed Rooth sym3tri

🌴
On vacation
View GitHub Profile
@sym3tri
sym3tri / Read from MongoDB slave.js
Created February 12, 2011 02:33
Read from a MongoDB replica set's slave node
db.getMongo().setSlaveOk()
@sym3tri
sym3tri / JavaScript-new-operator.js
Created February 12, 2011 04:35
How JavaScript new operator works
// constructor definition
var Foo = function() {
// when invoked with new it's as if the following implicitly happens
// Foo.prototype = {};
// this = {};
// return this;
};
// create new instance with 'new'
var x = new Foo();
@sym3tri
sym3tri / MongoDB update all matching.js
Created March 7, 2011 06:15
How to update a single field in a MongoDB collection for all documents matching a specific criteria
// FROM: http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29
//
// db.collection.update( criteria, objNew, upsert, multi )
// criteria - query which selects the record to update;
// objNew - updated object or $ operators (e.g., $inc) which manipulate the object
// upsert - if this should be an "upsert"; that is, if the record does not exist, insert it
// multi - if all documents matching criteria should be updated
//
// SQL VERSION:
// UPDATE myTable SET dateField = '2011-01-01' WHERE condField = 'condValue'
@sym3tri
sym3tri / JavaScript ORvsANDvsCOMMA.txt
Last active January 10, 2019 09:16
How AND, OR, and COMMA operators are evaluated in JavaScript
//(LHE: left hand expression, RHE right hand expression)
LHE && RHE
1. Always evaluate LHE
2. If LHE is true, evaluate RHE
LHE || RHE
1. Always evaluate LHE
2. If LHE is false, evaluate RHE
@sym3tri
sym3tri / JapaneseRegex.js
Created May 19, 2011 02:46
Regex to test for presence of Japanese characters
// REFERENCE UNICODE TABLES:
// http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml
// http://www.tamasoft.co.jp/en/general-info/unicode.html
//
// TEST EDITOR:
// http://www.gethifi.com/tools/regex
//
// UNICODE RANGE : DESCRIPTION
//
// 3000-303F : punctuation
@sym3tri
sym3tri / UrlRegex.js
Created June 2, 2011 10:24
Validate URLs with JavaScript Regex
/^(http(s)?:\/\/)?([\w-]+\.{1})*(([\w-]*){1}(\.{1}[A-Za-z]{2,4}){1}){1}(\:{1}\d*)?([\?\/|\#]+[\@\~\;\+\'\#\,\.\%\-\/\&\?\=\w\$\s]*)*$/i
@sym3tri
sym3tri / sprop.js
Created August 18, 2011 03:27
Safe deep property access in JavaScript
// Gets the deep value of property specified in the ns param.
// Will always return undefined any properties don't exist or if obj input is invalid.
// obj (object): The object to inspect.
// ns (array of strings): Ordered properties that are the namespace to the property of interest.
// returns: Whatever value the property contains, or undefined
function sprop(obj, ns) {
var result, i, len;
@sym3tri
sym3tri / README.markdown
Created January 24, 2012 07:47 — forked from erikfried/README.markdown
shell script to invoke jslint via jsc on Mac OS X

I just realized that there is a rather well hidden javascript interpreter in the mac os x terminal out of the box. /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc See http://www.phpied.com/javascript-shell-scripting/

Then i figured it coud be quite easy to set up a command line util to run jslint from anywhere. Thought i´d share how.

Setup

@sym3tri
sym3tri / GH-WS-Ignore
Created April 6, 2012 19:42
Bookmarklet to quickly ignore whitespace in github diffs
javascript:(function() { window.location.href += '?w=1';})();
@sym3tri
sym3tri / ES5 Newnewss
Created April 20, 2012 04:18
New features of ES5 summarized
- Trailing commas are ok
- No reserved words for property names
- NaN, Infinity, undefined : are all constants
- parseInt() defaults to radix 10
- /regexp/ produces new reg ex object every time
- JSON.parse(), JSON.stringify()
- Function.prototype.bind
- String.prototype.trim
- Array.prototype.every, filter, forEach, indexOf, lastIndexOf, map, reduce, reduceRight, some,
- Date.now()