Permalink
Browse files

[FEATURE isPresent] Add Ember.isPresent

as inverse of Ember.isBlank
  • Loading branch information...
1 parent 3edd7bd commit 0a7ddc68c332d18744a59d2f7092c1ec143cd453 @amiel committed Jul 9, 2014
View
1 features.json
@@ -9,6 +9,7 @@
"ember-routing-will-change-hooks": null,
"ember-routing-consistent-resources": true,
"event-dispatcher-can-disable-event-manager": null
+ "ember-metal-is-present": null,
},
"debugStatements": [
"Ember.warn",
View
28 packages/ember-metal/lib/is_present.js
@@ -0,0 +1,28 @@
+import isBlank from 'ember-metal/is_blank';
+
+/**
+ A value is present if it not `isBlank`.
+
+ ```javascript
+ Ember.isPresent(); // false
+ Ember.isPresent(null); // false
+ Ember.isPresent(undefined); // false
+ Ember.isPresent(''); // false
+ Ember.isPresent([]); // false
+ Ember.isPresent('\n\t'); // false
+ Ember.isPresent(' '); // false
+ Ember.isPresent({}); // true
+ Ember.isPresent('\n\t Hello'); // true
+ Ember.isPresent('Hello world'); // true
+ Ember.isPresent([1,2,3]); // true
+ ```
+
+ @method isPresent
+ @for Ember
+ @param {Object} obj Value to test
+ @return {Boolean}
+ @since 1.7.0
+ */
+export default function isPresent(obj) {
+ return !isBlank(obj);
+}
View
8 packages/ember-metal/lib/main.js
@@ -81,6 +81,10 @@ import libraries from "ember-metal/libraries";
import {isNone, none} from 'ember-metal/is_none';
import {isEmpty, empty} from 'ember-metal/is_empty';
import isBlank from 'ember-metal/is_blank';
+
+if (Ember.FEATURES.isEnabled('ember-metal-is-present')) {
+ import isPresent from 'ember-metal/is_present';
+}
// END IMPORTS
// BEGIN EXPORTS
@@ -230,6 +234,10 @@ Ember.empty = empty;
Ember.isBlank = isBlank;
+if (Ember.FEATURES.isEnabled('ember-metal-is-present')) {
+ Ember.isPresent = isPresent;
+}
+
Ember.merge = merge;
/**
View
25 packages/ember-metal/tests/is_present_test.js
@@ -0,0 +1,25 @@
+import isPresent from 'ember-metal/is_present';
+
+QUnit.module("Ember.isPresent");
+
+test("Ember.isPresent", function() {
+ var string = "string", fn = function() {},
+ object = {length: 0};
+
+ equal(false, isPresent(), "for no params");
+ equal(false, isPresent(null), "for null");
+ equal(false, isPresent(undefined), "for undefined");
+ equal(false, isPresent(""), "for an empty String");
+ equal(false, isPresent(" "), "for a whitespace String");
+ equal(false, isPresent("\n\t"), "for another whitespace String");
+ equal(true, isPresent("\n\t Hi"), "for a String with whitespaces");
+ equal(true, isPresent(true), "for true");
+ equal(true, isPresent(false), "for false");
+ equal(true, isPresent(string), "for a String");
+ equal(true, isPresent(fn), "for a Function");
+ equal(true, isPresent(0), "for 0");
+ equal(false, isPresent([]), "for an empty Array");
+ equal(true, isPresent({}), "for an empty Object");
+ equal(false, isPresent(object), "for an Object that has zero 'length'");
+ equal(true, isPresent([1,2,3]), "for a non-empty array");
+});

0 comments on commit 0a7ddc6

Please sign in to comment.