Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[FEATURE ember-metal-is-present]

Add Ember.isPresent as inverse of Ember.isBlank
  • Loading branch information...
commit 4eaef8ecc985ed417e9b52184c532b853ac8d181 1 parent a47897e
@amiel authored
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
30 packages/ember-metal/lib/is_present.js
@@ -0,0 +1,30 @@
+import isBlank from 'ember-metal/is_blank';
+
+if (Ember.FEATURES.isEnabled('ember-metal-is-present')) {
+ /**
+ 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
27 packages/ember-metal/tests/is_present_test.js
@@ -0,0 +1,27 @@
+if (Ember.FEATURES.isEnabled('ember-metal-is-present')) {
+ 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");
+ });
+}
Please sign in to comment.
Something went wrong with that request. Please try again.