Last active
December 27, 2016 18:29
-
-
Save texdc/9314ee167c66c5801f5cd561588bc0f2 to your computer and use it in GitHub Desktop.
utility ("sugar") properties and functions inspired by Dart and C#
This file contains 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
if (String.prototype.isEmpty == undefined) { | |
Object.defineProperty(String.prototype, 'isEmpty', { | |
get: function() { return this.trim() == ''; } | |
}); | |
} | |
if (String.prototype.isNotEmpty == undefined) { | |
Object.defineProperty(String.prototype, 'isNotEmpty', { | |
get: function() { return this.trim() != ''; } | |
}); | |
} | |
if (String.isNullOrEmpty == undefined) { | |
Object.defineProperty(String, 'isNullOrEmpty', { | |
value: (aString) => aString == null || (typeof aString === 'string' && aString.isEmpty), | |
writable: false | |
}); | |
} |
This file contains 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
var StringUtil = Object.create({}, { | |
isString: { value: function(aString) { return typeof aString === 'string'; }}, | |
isEmpty: { value: function(aString) { return this.isString(aString) && aString.trim() == ''; }}, | |
isNotEmpty: { value: function(aString) { return !this.isEmpty(aString); }}, | |
isNullOrEmpty: { value: function(aString) { return aString == null || this.isEmpty(aString); }}, | |
valueOrDefault: { value: function(aString, aDefault) { return this.isNullOrEmpty(aString) ? aDefault : aString; }} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment