Skip to content

Instantly share code, notes, and snippets.

Avatar

Christian C. Salvadó cms

View GitHub Profile
@cms
cms / getStyle.js
Created Apr 17, 2010
Get computed styles
View getStyle.js
function getStyle(el, styleProp) {
var value, defaultView = el.ownerDocument.defaultView;
// W3C standard way:
if (defaultView && defaultView.getComputedStyle) {
// sanitize property name to css notation (hypen separated words eg. font-Size)
styleProp = styleProp.replace(/([A-Z])/g, "-$1").toLowerCase();
return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
} else if (el.currentStyle) { // IE
// sanitize property name to camelCase
styleProp = styleProp.replace(/\-(\w)/g, function(str, letter) {
View frameworkDetect.js
(function () {
var frameworks = {
'MooTools Core': 'MooTools.version',
'MooTools More': 'MooTools.More.version',
'Base2': 'base2.version',
'Dojo': 'dojo.version',
'Ext JS': 'Ext.version',
'jQuery': 'jQuery.fn.jquery',
'jQuery UI': 'jQuery.ui.version',
'MochKit': 'MochiKit.MochiKit.VERSION',
View parseDate.js
function parseDate(str, format) {
format = format || 'yyyy-mm-dd'; // default format
// build a regular expression to match the format
var i = 0, fmt = {},
re = new RegExp('^'+format.replace(/(yyyy|dd|mm)/g, function (match) {
fmt[match] = ++i; // store format position
return '(\\d{'+match.length+'})'; // make a capturing group
})+'$'), parts = str.match(re); // match the date parts
if (!parts) return null; // input didn't match the format pattern
@cms
cms / besenTest.js
Created Jul 29, 2010
BESEN undefined accessor properties bug
View besenTest.js
// BESEN strange behavior on named accessor property
// Tested on r77
var obj = Object.defineProperty({}, 'foo', { set: undefined }),
desc = Object.getOwnPropertyDescriptor(obj, 'foo');
// Definning a setter just to make the property a named accessor property.
// (`undefined` is a valid value for the [[Set]] and [[Get]] property attributes)
// the getter is correctly `undefined`, the FromPropertyDescriptor
@cms
cms / besenSliceCall.js
Created Jul 29, 2010
Besen `Array.prototype.slice` bug
View besenSliceCall.js
var slice = Array.prototype.slice,
arrayLike = {0: 'a', 1: 'b', 2: 'c', 3: 'd', length:4},
a = slice.call(arrayLike), // undefined
b = slice.call(arrayLike, undefined); // the array as expected
print(a); // undefined
print(b); // "a,b,c,d"
View newApply.js
// newApply: Invoke a consturctor function correctly, passing an arbitrary
// number of arguments
function newApply(fn, argsArray) { // (c) Christian C. Salvadó - MIT License
var F = newApply, obj, ret, t, isPrimitive;
if (this instanceof F) return; // this function is used as a dummy constructor
F.prototype = fn.prototype; // set up the prototype and constructor properties
F.prototype.constructor = fn;
@cms
cms / es-harmony_array_subclassing.js
Created Aug 18, 2010
Generic Array subclassing function using ECMA-Harmony Proxy features
View es-harmony_array_subclassing.js
// Generic function to subclass an array in JavaScript
// (using the ECMAScript Harmony Proxy features)
// Structuring of code inspired by @kangax's
// array_subclassing (http://github.com/kangax/array_subclassing)
var makeSubArray = (function () {
var MAX_UINT32 = Math.pow(2, 32) - 1;
return function (properties) {
properties || (properties = {});
@cms
cms / besen-logicaloperators.js
Created Sep 8, 2010
BESEN wrong behavior of the logical && and || operators
View besen-logicaloperators.js
// BESEN wrong behavior of the logical && and || operators
// Tested on r111
var obj = {};
// Logical AND operator
print(obj && obj); // prints true, expected '[object Object]'
print(obj && 'foo'); // prints '[object Object]', expected 'foo'
print(obj && 4); // prints '[object Object]', expected 4
print(obj && NaN); // prints false, expected NaN
@cms
cms / BESEN-String-index-named-properties.js
Created Sep 11, 2010
BESEN bug on String index named properties
View BESEN-String-index-named-properties.js
/**
* BESEN bug on String index named properties
*
* There are some problems accessing the named properties of String
* objects that correspond to the individual characters of its
* [[PrimitiveValue]].
*
* Tested on BESEN r117
*/
@cms
cms / JSON.js
Created Oct 6, 2010
Added reviver support to Asen's JSON.js
View JSON.js
/**
* Written by Asen Bozhilov
* http://code.google.com/p/js-examples/source/browse/trunk/JSON.js
*/
if (!this.JSON) {
this.JSON = {
parse : (function (undefined) {
var JSON_GRAMMAR = {
STRING : '"([^"\\x00-\\x1F\\\\]|\\\\["\\\\\/bfnrt]|\\\\u[0-9A-Fa-f]{4})*"',