Skip to content

Instantly share code, notes, and snippets.

@texdc
Last active December 27, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save texdc/9314ee167c66c5801f5cd561588bc0f2 to your computer and use it in GitHub Desktop.
Save texdc/9314ee167c66c5801f5cd561588bc0f2 to your computer and use it in GitHub Desktop.
utility ("sugar") properties and functions inspired by Dart and C#
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
});
}
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