Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    RestartMV
    @RestartMV
    well that's useful anyway
    I guess in theory you could enter the array indicies in as a string but that seems pretty hideous as a workaround and isn't the type of general purpose thing that tons of people would like
    Julian Gonggrijp
    @jgonggrijp
    @RestartMV The initial version of Underscore took inspiration from a couple of other libraries, one of which actually did use string shorthands for all kinds of things. I guess that besides ugliness, it's also rather inefficient because of the additional parsing required, but it's still interesting. https://web.archive.org/web/20081201075555/https://osteele.com/sources/javascript/functional/
    Dylan Reichstadt
    @dylanlive
    Hey All! Just wanted to pop by and say it seems cnn.com/activate isn't loading properly, and I see it's trying to fetch https://underscorejs.org/underscore-min.js and getting a 404. Not sure if that's something controlled by these maintainers
    Seems there's another request failing on that page, so it might not just be that. Anyway, night all!
    Julian Gonggrijp
    @jgonggrijp
    @dylanlive Thanks for the heads-up! It is controlled by the maintainers (and I renamed it to underscore-umd-min.js), but it's not supposed to be used in production. I'll contact CNN about it.
    Dylan Reichstadt
    @dylanlive
    Awesome, thanks! I tried to find their contact form but that was challenging :P
    Julian Gonggrijp
    @jgonggrijp
    @dylanlive I didn't find any such thing, did you? I ended up just sending them a tweet.
    Dylan Reichstadt
    @dylanlive
    Just https://www.cnn.com/feedback - but ironically that seems broken too lol. Though after hitting Submit it does look like a POST request is made. I reached out to them citing your tweet
    Julian Gonggrijp
    @jgonggrijp
    @dylanlive I just released 1.13.1, which puts back the underscore.js alias in git and on the website. That means that cnn.com/activate is un-broken for now, though I find it worrying that they haven't already switched to a CDN url.
    Dylan Reichstadt
    @dylanlive
    super delayed lol, but thank you. I agree. It seemed challenging to get in contact with them too
    Julian Gonggrijp
    @jgonggrijp
    Underscore was JavaScript Library of the Year 2021 according to w3techs.com! https://w3techs.com/blog/entry/web_technologies_of_the_year_2021 (third category on the page)
    dimtabu
    @taburetkin

    as i can see _.result() does not support invoke arguments and it makes it useless with arrow functions.

    const obj = {
       propertyA: 'foo',
       propertyB: obj => obj.propertyA,
       propertyC() { return this.propertyA; }
    }
    _.result(obj,'propertyB'); // will throw
    _.result(obj,'propertyC');

    so, i am interesting how you solve that in your projects? do you just do not use arrow functions or you implement some other _.result able to handle this ?

    Julian Gonggrijp
    @jgonggrijp
    @taburetkin I don't think I've encountered that situation before. If you had a _.powerResult which could handle that situation, how would it behave?
    dimtabu
    @taburetkin

    _.powerResult(obj, property, [invokeArgs], defaultValue):

    _.powerResult(obj, property, defaultValue) = obj.property.call(obj, obj)
    _.powerResult(obj, property, nonArrayInvokeArgument, defaultValue) = obj.property.call(obj, nonArrayInvokeArgument)
    _.powerResult(obj, property, [arg1, arg2], defaultValue) = obj.property.apply(obj, [arg1, arg2])

    when calling result with 2 or 3 params - behave mostly as usual except passing obj as first argument
    when calling result with 4 params - take third param as invoke argument/arguments.

    But i don't use _.result in my applications because i dislike that idea of invoking property of object and because of current limitation.
    It seems that this method was invented for some certain case.

    Personally i prefer to have invokeValue

    function invokeValue(value, invokeArgs, invokeContext) {
      ...
    }
    dimtabu
    @taburetkin
    and after some thoughts
    in additional to described powerResult signatures
    the main one should look like
    _.powerResult(obj, property, options)
    and options can be something like this
    {
     defaultValue: any, optional,
     invokeArgs: any, optional // array or single value shoud be handled separated
     invokeContext: any, optional
    }
    but there could be some cavetas between powerResult(obj, property, defaultValue) && powerResult(obj, property, options) :(
    maybe this could do the job: powerResult(obj, options, property)
    but as i told you before i am not using result at the moment because of its design.
    dimtabu
    @taburetkin
    or even powerResult(obj, options)
    Julian Gonggrijp
    @jgonggrijp
    @taburetkin Interesting, thanks for sharing.
    The usecase that _.result was conceived for, I think, is in OOP situations where a value may be obtained from an instance either through a fixed property value or a zero-argument method. It is used a lot under the hood of Backbone, for example for Model#urlRoot/Model#urlRoot(). I do personally use it occasionally for this purpose, even outside of Backbone.
    I have sometimes wondered whether the existing _.result should support passing arguments. It could be extended to accept a fourth argument.
    dimtabu
    @taburetkin
    as i can see in a backbone its a just suggar for case when some property may be ocasionally a method
    Julian Gonggrijp
    @jgonggrijp
    Right, I think that's what it was meant for originally.
    dimtabu
    @taburetkin
    in case of url it really better to have native getUrl method
    and pass arguments to it like querystring params, for example.
    but backbone is a backbone. i just point to some strange things (in my opinion)
    Julian Gonggrijp
    @jgonggrijp
    I agree _.result could be more general than it is. It's a bit difficult to retrofit, but maybe it's worth the hassle? I don't know.
    dimtabu
    @taburetkin
    i don't know too )))
    i just want to know how do you all handle that in your projects )
    Julian Gonggrijp
    @jgonggrijp
    I suspect that in the situations where I might have used _.powerResult, I just wrote custom code instead.
    In any case, there weren't that many of such occasions.
    dimtabu
    @taburetkin
    well, i just tired to copy paste it from project to project )))))
    Julian Gonggrijp
    @jgonggrijp
    By "it", you mean invokeValue?
    dimtabu
    @taburetkin
    some utils, include invokeValue
    actually i have some personal pack, so its not really copy paste
    but... it seems to me pretty common in most cases.
    Julian Gonggrijp
    @jgonggrijp
    Regarding invokeValue in particular, isn't that basically a standalone version of Function#apply with the parameters in a slightly different order?
    dimtabu
    @taburetkin
    not almost
    invokeValue(String) in my ideal world should not invoke
    and i have a solution for that case
    Julian Gonggrijp
    @jgonggrijp
    Does invokeValue(String) return String (the global object) or the empty string ('')?
    dimtabu
    @taburetkin
    my version does not invokes function which application knows as Known Ctors
    i put there all constructors
    Julian Gonggrijp
    @jgonggrijp
    So what does it do instead?
    dimtabu
    @taburetkin
    string, number, backbone.view, model
    just retun it as is
    this is for case when you may or may not set as property some of constructor and that property by design could be a function
    view.extend({
      childView: MyOtherView, // ok
      childView() {  // this is OK too
         if (condition1) {
            return View1;
         } else {
           return View2;
         }
       }
    })
    basicaly for such cases
    Julian Gonggrijp
    @jgonggrijp
    I see! That reminds me of jashkenas/backbone#4234. Not exactly the same problem, but similar.
    dimtabu
    @taburetkin
    few days ago i've put that in a lib
    https://github.com/taburetkin/yaff-entitybuilder
    some util pack which i use in marionette
    but actually it can be used in any framework
    Julian Gonggrijp
    @jgonggrijp
    Starred!
    dimtabu
    @taburetkin
    glad if you will find it usefull :)