Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

Add fuse.Class.mixins.comparable mixin for equals() and like() methods

#67

Add fuse.Class.mixins.comparable mixin for equals() and like() methods.

Do you guys like isEqual/isLike or equals/like ?

  fuse.Class.mixins.comparable = { };

  (function(mixin) {
    mixin.like = function like(value) {
      return String(this) === String(value);
    };

    mixin.equals = function equals(value) {
     return toString.call(this) === toString.call(value) && this.like(value);
    };

    fuse.Object.equals = function equals(object1, object2) {
      return mixin.equals.call(object1, object2);
    };

    fuse.Object.like = function like(object1, object2) {
      return mixin.like.call(object1, object2);
    };

    var like = nil, equals = nil;
  })(fuse.Class.mixins.comparable);


  // usage
  fuse.String.addMixins(fuse.Class.mixins.comparable);

  fuse.String("1").like(1);   // true
  fuse.String("1").equals(1); // false
  fuse.String("Oh hai").equals(fuse.String("Oh hai")); // true

Reported by John-David Dalton · June 1st, 2010 @ 10:10 AM

State: new
Milestone: none
Assigned to: Joe Gornick Joe Gornick

Activity

  1. Kit Goncharov
    Kit Goncharov
    • State changed from new to open
    • Assigned user changed from John-David Dalton to Kit Goncharov

    I was just thinking about implementing something like this. :) I can go ahead and get started, if that's okay with everyone.

    With re:to naming: although 'like' and 'equals' are shorter, I like 'isLike'/'isEqual' for API consistency, as both methods return booleans.

    June 1st, 2010 @ 10:24 AM

  2. T.J. Crowder
    T.J. Crowder

    FWIW, not being au fait with much of Fuse's API, I prefer equals and like. I've never been a fan of the isXyz naming style. If it's consistent with the rest of Fuse's naming, though, I'd go with consistency.

    On implementation, you don't want to do this:

    mixin.like = function like(value) {
        return String(this) == String(value);
    };
    

    That will create two completely separate function objects on IE, because it parses it as a function declaration (creating a named like function), then again later as a function expression (creating an anonymous function bound to the mixin.like property). You need to do this:

    mixin.like = like;
    function like(value) {
        return String(this) == String(value);
    }
    

    June 1st, 2010 @ 10:32 AM

  3. John-David Dalton
    John-David Dalton

    @T.J. Crowder the named expressions are cleaned up via the var like = null, equals = null;. I keep to that pattern so that I can easily parse out the named expressions and cleanup when devs choose to download a minified version.

    June 1st, 2010 @ 10:35 AM

  4. T.J. Crowder
    T.J. Crowder

    @JDD: What purpose does all of that serve, then? The functions that end up getting bound to the property are anonymous, why bother with the names at all if you're okay with using anonymous functions on the mixin? (Which, as you know, I don't advocate. Names are good.)

    June 1st, 2010 @ 10:41 AM

  5. John-David Dalton
    John-David Dalton

    While our API does use the is prefix for methods that return a boolean it also allows verbs/adverbs without a prefix like update(), up(), down(), next(), previous(). I think the boolean return might trump that though. I like the shorter API but for consistency I am leaning toward isLike and isEqual.

    June 1st, 2010 @ 10:42 AM

  6. Kit Goncharov
    Kit Goncharov

    @JDD: Oops. :P I should probably revert my cleanups, then...I misunderstood you earlier when we were talking about possibly removing that pattern. For my cleanups, I've been following TJ's pattern:

    //Previously...
    plugin.some = function some(...)
    ...
    var some = nil, ...
    
    //My cleanup
    function some(...)
    ...
    plugin.some = some;
    

    On-topic: James Coglan also has an interesting Comparable implementation in JS.Class: http://jsclass.jcoglan.com/comparable.html. It's extremely Ruby-like, but it shouldn't be too difficult to port to a more JavaScript-esque solution.

    June 1st, 2010 @ 10:43 AM

  7. John-David Dalton
    John-David Dalton

    @T.J. Crowder no it just clears the declarations created by JScript it doesn't clear the expressions.

    June 1st, 2010 @ 10:43 AM

  8. T.J. Crowder
    T.J. Crowder

    @JDD: I realize it's not touching the expression. But I've learned something here, I thought that IE skipped the name when processing the expression, but a quick test with VS2005 and MDE6 suggests that no, it actually does register the name on the expression as well as the declaration. That's interesting. Still don't think I'd do it (the custom minifier could handle either of them perfectly well, it's just how you write it), but it's interesting. Thanks for the IM about kangax's testing, too.

    June 1st, 2010 @ 10:56 AM

  9. John-David Dalton
    John-David Dalton

    Sam Leb also has a version http://github.com/samleb/prototype-fruits/blob/master/src/comparable.js but I think both James' and Sam's are a bit too complex for now. We can add the other methods at a later time if they are needed.

    June 1st, 2010 @ 10:57 AM

  10. Joe Gornick
    Joe Gornick

    +1 for equals and like.

    Yes the is* syntax is more consistent with other methods like isString, however, it works there because it's a known type we are comparing against.

    equals and like will definitely be used a lot and fall into the down, up, next, etc... style of method names. Plus, all other languages/API use the equals form.

    June 1st, 2010 @ 11:38 AM

  11. T.J. Crowder
    T.J. Crowder

    Plus, all other languages/API use the equals form

    For me, this is the persuasive argument. It may not be "all," but it's really close. :-) Java and C# both use it. Objective-C apparently uses isEqual with NSObject, but Apple's efforts notwithstanding, that's not nearly as popular as Java and C#. (Also, nit-picking, it should be isEqualTo if it's going to be in that form.)

    June 1st, 2010 @ 11:50 AM

  12. Joe Gornick
    Joe Gornick

    @T.J. Crowder good point, I should of prefaced "all" with Java and C#/.NET which are usually the two API's I like to compare against because of their popularity.

    June 1st, 2010 @ 11:55 AM

  13. Nick Stakenburg
    Nick Stakenburg

    isEqualTo might work better with isLike, but I prefer like/equals, nice and short. I don't think you really need the 'is' prefix like you need it in names like isEmpty, where without 'is' the name could mean more then one thing.

    You can write the ? this.like(value) : false; as && this.like(value);

    June 1st, 2010 @ 12:38 PM

  14. John-David Dalton
    John-David Dalton

    @Nick Thanks for the suggestion. Modified the example accordingly.

    June 1st, 2010 @ 02:31 PM

  15. John-David Dalton
    John-David Dalton
    • Title changed from Add fuse.Comparable mixin for equals() and like() methods to Add fuse.Class.mixins.comparable mixin for equals() and like() methods

    June 1st, 2010 @ 02:53 PM

  16. Kit Goncharov
    Kit Goncharov
    • Assigned user cleared.

    Sorry, I'll work on #65, #66, and #75 for now. :)

    June 9th, 2010 @ 03:48 PM

  17. Joe Gornick
    Joe Gornick
    • State changed from open to new
    • Assigned user set to John-David Dalton
    • Milestone order changed from 0 to 0

    September 18th, 2010 @ 01:36 PM

  18. Joe Gornick
    Joe Gornick
    • Assigned user changed from John-David Dalton to Joe Gornick

    September 19th, 2010 @ 05:54 PM

  19. Joe Gornick
    Joe Gornick
    • Milestone cleared.
    • Milestone order changed from 1 to 0

    September 29th, 2010 @ 09:30 AM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.