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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, is it fully compatible with node.js? underscore was initially developed as client side lib
Oh, I didn't see a link it your answer =)
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.
Actually I know about underscore, I wonder are there alternatives.
Do you have any particular qualms about underscore, or are you just trying to scope out the field?
|
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

Comments

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 Comments

Thanks, but I don't think that it is the best decision to use undocumented internal function in terms of future compatibility.
I asked the node maintainers about it, they don't see it going anywhere any time soon. github.com/joyent/node/pull/4834
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 Comment

Thanks using it ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.