Skip to content

Instantly share code, notes, and snippets.

View cms's full-sized avatar

Christian C. Salvadó cms

View GitHub Profile
@cms
cms / getStyle.js
Created April 17, 2010 00:48
Get computed styles
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) {
(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',
@cms
cms / parseDate.js
Created June 2, 2010 07:16
parseDate
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 July 29, 2010 06:16
BESEN undefined accessor properties bug
// 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 July 29, 2010 07:26
Besen `Array.prototype.slice` bug
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"
@cms
cms / newApply.js
Created August 17, 2010 08:21
newApply
// 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 August 18, 2010 08:10
Generic Array subclassing function using ECMA-Harmony Proxy features
// 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 September 8, 2010 07:39
BESEN wrong behavior of the logical && and || operators
// 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 September 11, 2010 08:27
BESEN bug on String index named properties
/**
* 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 October 6, 2010 08:04
Added reviver support to Asen's 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})*"',