3

Is there good utility library for working with objects and arrays.

For example functions like: extend, forEach, copying objects/arrays ect,

What is common in node.js environments? I wonder are there decent alternatives to underscore.js?

4 Answers 4

6

underscore.js is a pretty good default for this kind of stuff. Here's a thread on compatibility issues that may be handy.

Edit, upon you're request for something beyond underscore:

As far as I know, underscore has become the defacto standard when you're looking for additional array operations (much like jQuery for DOM manipulation). Joyent maintains a pretty thorough manifest of node.js compatible modules, and the only seemingly comparable utility would appear to be an experimental library called fjs with an emphasis on currying (and judging from the source, most of the functionality comes from extending underscore functions anyway). There might be something else out there, but as far as I know nothing with the penetration and maturity of underscore.

Yet another edit - here are a handful of older libraries if you're so curious, but their maintenance has fallen off a bit - valentine, wu.js, Functional, and Sugar. Functional and valentine may be a bit thinner; wu.js looks to be about the same and sugar is even fatter.

7
  • Thanks, is it fully compatible with node.js? underscore was initially developed as client side lib
    – WHITECOLOR
    Sep 13, 2012 at 19:35
  • Oh, I didn't see a link it your answer =)
    – WHITECOLOR
    Sep 13, 2012 at 19:36
  • Heh don't worry, it was an edit after the fact. I realized their might be some confusion as to where underscore can be used, so a clarification would be useful.
    – Bubbles
    Sep 13, 2012 at 19:37
  • Actually I know about underscore, I wonder are there alternatives.
    – WHITECOLOR
    Sep 13, 2012 at 19:39
  • 1
    Do you have any particular qualms about underscore, or are you just trying to scope out the field?
    – Bubbles
    Sep 13, 2012 at 19:41
5

lodash is a "drop-in replacement* for underscore.js" that you may also want to consider.

Lo-Dash v0.7.0 has been tested in at least Chrome 5-21, Firefox 1-15, IE 6-9, Opera 9.25-12, Safari 3-6, Node.js 0.4.8-0.8.8, Narwhal 0.3.2, RingoJS 0.8, and Rhino 1.7RC5

0
3

For extend specifically, you can use Node's built-in util._extend() function.

var
  extend = require('util')._extend,
  x = {a:1},
  y = extend({}, x);

Source code of Node's _extend function: https://github.com/joyent/node/blob/master/lib/util.js#L563

2
  • Thanks, but I don't think that it is the best decision to use undocumented internal function in terms of future compatibility.
    – WHITECOLOR
    Feb 26, 2013 at 8:46
  • I asked the node maintainers about it, they don't see it going anywhere any time soon. github.com/joyent/node/pull/4834
    – jimbo
    Feb 27, 2013 at 2:26
1

Have a look at Ramdajs: http://ramdajs.com/0.22.1/index.html

The primary distinguishing features of Ramda are:

  • Ramda emphasizes a purer functional style. Immutability and side-effect free functions are at the heart of its design philosophy. This can help you get the job done with simple, elegant code.

  • Ramda functions are automatically curried. This allows you to easily build up new functions from old ones simply by not supplying the final parameters.

  • The parameters to Ramda functions are arranged to make it convenient for currying. The data to be operated on is generally supplied last.

The last two points together make it very easy to build functions as sequences of simpler functions, each of which transforms the data and passes it along to the next. Ramda is designed to support this style of coding.

1
  • Thanks using it ;)
    – WHITECOLOR
    Sep 2, 2016 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.