Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top