Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[FEATURE ember-metal-is-present] Add Ember.isPresent #5136

Merged
merged 1 commit into from

5 participants

@amiel

Maybe it's because I come from rails, but I miss having .present? as the inverse of .blank?. I find that I end up using ! Ember.isBlank() more than Ember.isBlank() by itself. I know this is a simple change, but I think the concept of presence is useful and more semantic. I first thought of this when working on jamesarosen/ember-cpm#36, and keep on finding times where it would make my code easier to understand.

@stefanpenner
Owner

this is a new feature, it must follow those guidelines: see http://emberjs.com/guides/configuring-ember/feature-flags/

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
@pangratz
pangratz added a note

There is no test case for this scenario ...

@amiel
amiel added a note

@pangratz Thanks, I've added a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@amiel amiel changed the title from Add Ember.isPresent to [FEATURE isPresent] Add Ember.isPresent
@amiel

@stefanpenner Sorry about that. I looked through CONTRIBUTING.md but not http://emberjs.com/guides/contributing/adding-new-features/.

Is guarding the import and Ember.isPresent = isPresent sufficient? or do I need to guard around the implementation and test as well. Thanks!

@stefanpenner
Owner

@amiel ya, someone should add that to contributing.MD as well (mind doing it?)

@rwjblue
Owner

@amiel - Would also be nice to have the commit prefixes in there (see #5135 (comment))...

@amiel

Please see #5137

@amiel amiel changed the title from [FEATURE isPresent] Add Ember.isPresent to [FEATURE ember-metal-is-present] Add Ember.isPresent
@amiel

hmmm, it looks like this failed at npm install :(

@amiel

It looks like the failure now is in defeaturify; am I doing something wrong with the feature flags?

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,
@rwjblue Owner
rwjblue added a note

Trailing commas are not valid JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
features.json
@@ -9,6 +9,7 @@
"ember-routing-will-change-hooks": null,
"ember-routing-consistent-resources": true,
"event-dispatcher-can-disable-event-manager": null
@rwjblue Owner
rwjblue added a note

Missing comma.

@amiel
amiel added a note

oh duh, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@amiel

@rjackson thanks. it looks like I had a couple of other feature flag issues as well (I didn't know import and export had to live outside my conditionals). I think they are all fixed, and the CI build just needs to be restarted again because of the npm issues :(

@trek
Owner

LGTM!

@rwjblue
Owner

@amiel - Can you add an entry to FEATURES.md also (just include a quick sentance or two and the the link to this PR)?

Other than that :+1:.

@amiel amiel [FEATURE ember-metal-is-present]
Add Ember.isPresent as inverse of Ember.isBlank
065921e
@amiel

Ok, done :)

@trek trek merged commit 02d69c8 into emberjs:master
@amiel amiel deleted the amiel:isPresent branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 10, 2014
  1. @amiel

    [FEATURE ember-metal-is-present]

    amiel authored
    Add Ember.isPresent as inverse of Ember.isBlank
This page is out of date. Refresh to see the latest.
View
8 FEATURES.md
@@ -75,3 +75,11 @@ for a detailed explanation.
Prior to this feature `home.index` route would not be created for the above resource.
Added in [#4251](https://github.com/emberjs/ember.js/pull/4251)
+
+* `ember-metal-is-present`
+
+ Adds `Ember.isPresent` as the inverse of `Ember.isBlank`. This convenience
+ method can lead to more semantic and clearer code.
+
+ Added in [#5136](https://github.com/emberjs/ember.js/pull/5136)
+
View
3  features.json
@@ -8,7 +8,8 @@
"ember-routing-linkto-target-attribute": null,
"ember-routing-will-change-hooks": null,
"ember-routing-consistent-resources": true,
- "event-dispatcher-can-disable-event-manager": null
+ "event-dispatcher-can-disable-event-manager": null,
+ "ember-metal-is-present": null
},
"debugStatements": [
"Ember.warn",
View
33 packages/ember-metal/lib/is_present.js
@@ -0,0 +1,33 @@
+import isBlank from 'ember-metal/is_blank';
+var isPresent;
+
+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
+ */
+ isPresent = function isPresent(obj) {
+ return !isBlank(obj);
+ };
+}
+
+export default isPresent;
View
5 packages/ember-metal/lib/main.js
@@ -81,6 +81,7 @@ 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';
+import isPresent from 'ember-metal/is_present';
// END IMPORTS
// BEGIN EXPORTS
@@ -230,6 +231,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 @@
+import isPresent from 'ember-metal/is_present';
+
+if (Ember.FEATURES.isEnabled('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");
+ });
+}
Something went wrong with that request. Please try again.