Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updating to include fn existence tests. Also addressing inclusion of …

….bind() capabilities.
  • Loading branch information...
commit b616cd10dc0760b578be0fbb7e6be78b168629f2 1 parent ec14294
@addyosmani authored
Showing with 127 additions and 70 deletions.
  1. +113 −70 helpers.js
  2. +14 −0 tests/spec/HelpersSpec.js
View
183 helpers.js
@@ -1,128 +1,171 @@
/*
String/Array helpers
For SocketStream's helpers.js
- Addy Osmani, 2011.
+ Addy Osmani, Aol, 2011.
*/
+/**
+ .bind() support
+**/
+
+if ( !Function.prototype.bind ) {
+ Function.prototype.bind = function( obj ) {
+ var slice = [].slice,
+ args = slice.call(arguments, 1),
+ self = this,
+ nop = function () {},
+ bound = function () {
+ return self.apply( this instanceof nop ? this : ( obj || {} ),
+ args.concat( slice.call(arguments) ) );
+ };
+
+ nop.prototype = self.prototype;
+ bound.prototype = new nop();
+
+ return bound;
+ };
+}
+
/**
Removes any duplicate entries from the current array
**/
-String.prototype.unique = function(b){
- var a = "", i, l = this.length,q="";
- for( i=0; i<l; i++ ) {
- if( a.indexOf( this[i], 0, b ) < 0 ) {
- a += this[i];
- }
- }
- return a;
-};
+if ( !String.prototype.unique ) {
+ String.prototype.unique = function(b){
+ var a = "", i, l = this.length,q="";
+ for( i=0; i<l; i++ ) {
+ if( a.indexOf( this[i], 0, b ) < 0 ) {
+ a += this[i];
+ }
+ }
+ return a;
+ };
+}
/**
Removes any duplicate entries from the current string
**/
-Array.prototype.unique = function( b ) {
- var a = [], i, l = this.length;
- for( i=0; i<l; i++ ) {
- if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
- }
- return a;
-};
+if ( !Array.prototype.unique ) {
+ Array.prototype.unique = function( b ) {
+ var a = [], i, l = this.length;
+ for( i=0; i<l; i++ ) {
+ if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
+ }
+ return a;
+ };
+}
/**
Returns the last character in the current string
**/
-String.prototype.last = function(){
- return this[this.length-1];
-};
+if ( !String.prototype.last ) {
+ String.prototype.last = function(){
+ return this[this.length-1];
+ };
+}
/**
Returns the last element in the current array
**/
-Array.prototype.last = function(){
- return this[this.length-1];
-};
+if ( !Array.prototype.last ) {
+ Array.prototype.last = function(){
+ return this[this.length-1];
+ };
+}
/**
Truncates the current string to the supplied length
**/
-String.prototype.truncate = function(length){
- if (this.length > length) {
- return this.slice(0, length - 3) + "...";
- }else {
- return this;
- }
-};
+if ( !String.prototype.truncate ) {
+ String.prototype.truncate = function(length){
+ if (this.length > length) {
+ return this.slice(0, length - 3) + "...";
+ }else {
+ return this;
+ }
+ };
+}
/**
Truncates the current array to the supplied length
**/
-Array.prototype.truncate = function(length){
- return this.slice(0, length);
+if ( !Array.prototype.truncate ) {
+ Array.prototype.truncate = function(length){
+ return this.slice(0, length);
+ }
}
/**
Returns a random character from the current string
**/
-String.prototype.random = function( r ) {
- var i = 0, l = this.length;
- if( !r ) { r = this.length; }
- else if( r > 0 ) { r = r % l; }
- else { i = r; r = l + r % l; }
- return this[ Math.floor( r * Math.random() - i ) ];
-};
+if ( !String.prototype.random ) {
+ String.prototype.random = function( r ) {
+ var i = 0, l = this.length;
+ if( !r ) { r = this.length; }
+ else if( r > 0 ) { r = r % l; }
+ else { i = r; r = l + r % l; }
+ return this[ Math.floor( r * Math.random() - i ) ];
+ };
+}
/**
Returns a random element from the current array
**/
-Array.prototype.random = function( r ) {
- var i = 0, l = this.length;
- if( !r ) { r = this.length; }
- else if( r > 0 ) { r = r % l; }
- else { i = r; r = l + r % l; }
- return this[ Math.floor( r * Math.random() - i ) ];
-};
+if ( !Array.prototype.random ) {
+ Array.prototype.random = function( r ) {
+ var i = 0, l = this.length;
+ if( !r ) { r = this.length; }
+ else if( r > 0 ) { r = r % l; }
+ else { i = r; r = l + r % l; }
+ return this[ Math.floor( r * Math.random() - i ) ];
+ };
+}
/**
Boolean check to find out if a supplied character is in the current string
**/
-String.prototype.contains = function(value) {
- var i = this.length;
- while (i--) {
- if (this[i] === value) return true;
- }
- return false;
-};
+if ( !String.prototype.contains ) {
+ String.prototype.contains = function(value) {
+ var i = this.length;
+ while (i--) {
+ if (this[i] === value) return true;
+ }
+ return false;
+ };
+}
/**
Boolean check to find out if a supplied element/string is in the current array
**/
-Array.prototype.contains = function(value) {
- var i = this.length;
- while (i--) {
- if (this[i] === value) return true;
- }
- return false;
-};
-
+if ( !Array.prototype.contains ) {
+ Array.prototype.contains = function(value) {
+ var i = this.length;
+ while (i--) {
+ if (this[i] === value) return true;
+ }
+ return false;
+ };
+}
/**
Boolean check to find out if an array contains any elements
**/
-Array.prototype.any = function(){
- return !(this && this.constructor==Array && this.length==0);
-};
-
+if ( !Array.prototype.any ) {
+ Array.prototype.any = function(){
+ return !(this && this.constructor==Array && this.length==0);
+ };
+}
/**
Sanitize content containing URLs or mailto/email references
**/
-String.prototype.sanitize = function(){
- return this.replace(/(([fh]+t+p+s?\:\/)+([^"'\s]+))/gi,"<a href=\"$1\" target=\"_blank\">$1<\/a>").
- replace(/([a-z0-9\-\.]+\@[a-z0-9\-]+([^"'\s]+))/gi,"<a href=\"mailto:$1\" target=\"_blank\">$1<\/a>");
-};
-
+if ( !String.prototype.sanitize ) {
+ String.prototype.sanitize = function(){
+ return this.replace(/(([fh]+t+p+s?\:\/)+([^"'\s]+))/gi,"<a href=\"$1\" target=\"_blank\">$1<\/a>").
+ replace(/([a-z0-9\-\.]+\@[a-z0-9\-]+([^"'\s]+))/gi,"<a href=\"mailto:$1\" target=\"_blank\">$1<\/a>");
+ };
+}
View
14 tests/spec/HelpersSpec.js
@@ -60,6 +60,20 @@ describe("Helpers", function(){
expect(testArray.any()).toBeTruthy();
});
+ it("should be able to ensure that a function is called with a particular this value", function(){
+
+ var a = 11;
+ var testMod = { a : 20, getA: function(){ return this.a;} };
+ testMod.getA();
+ var getA = testMod.getA;
+ var boundGetA = getA.bind(testMod);
+ boundGetA();
+
+ expect(testMod.getA()).toEqual(20);
+ expect(boundGetA()).toEqual(20);
+
+ });
+
it("should be able to sanitize a string containg URLs or mailto references", function(){
expect(testHTML.sanitize()).toEqual('<a href="http://www.github.com" target="_blank">http://www.github.com</a> and some other text');
expect(testMailto.sanitize()).toEqual('you might want to email mailto:<a href="mailto:test@github.com" target="_blank">test@github.com</a>');
Please sign in to comment.
Something went wrong with that request. Please try again.