Created
September 1, 2014 13:16
-
-
Save stepanselyuk/8903181f7e74b0a267dd to your computer and use it in GitHub Desktop.
isEmptyObject and matchRecursiveRegExp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MyApp.CommonFunctionsModule.isEmptyObject = function ( obj ) { | |
| var hasOwnProperty = Object.prototype.hasOwnProperty; | |
| // null and undefined are "empty" | |
| if ( obj == null ) return true; | |
| // Assume if it has a length property with a non-zero value | |
| // that that property is correct. | |
| if ( obj.length > 0 ) return false; | |
| if ( obj.length === 0 ) return true; | |
| // Otherwise, does it have any properties of its own? | |
| // Note that this doesn't handle | |
| // toString and valueOf enumeration bugs in IE < 9 | |
| for ( var key in obj ) { | |
| if ( hasOwnProperty.call( obj, key ) ) return false; | |
| } | |
| return true; | |
| }; | |
| /* | |
| * See http://blog.stevenlevithan.com/archives/javascript-match-recursive-regexp | |
| */ | |
| MyApp.CommonFunctionsModule.matchRecursiveRegExp = function ( str, left, right, flags ) { | |
| var f = flags || '', | |
| g = f.indexOf( "g" ) > -1, | |
| x = new RegExp( left + "|" + right, 'g' + f.replace( /g/g, '' ) ), | |
| l = new RegExp( left, f.replace( /g/g, '' ) ), | |
| a = [], | |
| t, s, m; | |
| do { | |
| t = 0; | |
| while ( m = x.exec( str ) ) { | |
| if ( l.test( m[0] ) ) { | |
| if ( !t++ ) s = x.lastIndex; | |
| } else if ( t ) { | |
| if ( !--t ) { | |
| a.push( str.slice( s, m.index ) ); | |
| if ( !g ) return a; | |
| } | |
| } | |
| } | |
| } while ( t && (x.lastIndex = s) ); | |
| return a; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment