Provide clone utility method in "util" module

215 views
Skip to first unread message

Ilya Shaisultanov

unread,
May 1, 2013, 12:37:54 PM5/1/13
to nod...@googlegroups.com
Why not add a way to copy objects, for example like SugarJS does?

Util already has `inherits` helper and creating object copies is something that's not available natively and for newcomers there's a lot of gotchas, IMO, e.g. when someone tries to clone an object with nested properties and does a shallow clone, ending up with refs instead of copies.

Having this in util would save people time and it'd be code that's reviewed by node maintainers (=> trustworthy).

Just my 2 cents.

Luke Arduini

unread,
May 1, 2013, 12:42:12 PM5/1/13
to nod...@googlegroups.com
that's unnecessary bloat that can be handled fine as a module on npm.


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mikeal Rogers

unread,
May 1, 2013, 1:06:51 PM5/1/13
to nod...@googlegroups.com
The util module is for common utilities required by *other core modules* not utilities that are useful for node modules outside of core.

Node core is crazy small, its not reasonable to think you can build an application without installing additional modules with npm. Many great utility modules exist in npm. 

Underscore is the most depended on module in npm and it includes _.clone() which is a copy function. lodash is another module which includes all the functionality of underscore and then some and has deep copying support (underscore copy is shallow).

-Mikeal

Ilya Shaisultanov

unread,
May 1, 2013, 1:10:58 PM5/1/13
to nod...@googlegroups.com
Right, so util module is for internal stuff. Then it makes sense to not add stuff into it willy-nilly. Good to know.

Jake Verbaten

unread,
May 1, 2013, 1:37:42 PM5/1/13
to nod...@googlegroups.com
```js
function clone(x) {
  return util._extend(x, {})
}
```

Obvouisly `util._extend` is not documented so if you use it and it changes then thats your problem.

Ilya Shaisultanov

unread,
May 1, 2013, 1:40:24 PM5/1/13
to nod...@googlegroups.com
That only does a shallow copy, though, it's not going to work for nested objects. At this point I'd rather go with a module.

Luke Arduini

unread,
May 1, 2013, 1:50:25 PM5/1/13
to nod...@googlegroups.com
Yes!

On Wednesday, May 1, 2013, wrote:
Wouldn't this also constitute the same unnecessary bloat that people use constantly?


// NOTE: These type checking functions intentionally don't use `instanceof`
// because it is fragile and can be easily faked with `Object.create()`.
function isArray(ar) {
  return Array.isArray(ar) ||
         (typeof ar === 'object' && objectToString(ar) === '[object Array]');
}
exports.isArray = isArray;


function isRegExp(re) {
  return typeof re === 'object' && objectToString(re) === '[object RegExp]';
}
exports.isRegExp = isRegExp;


function isDate(d) {
  return typeof d === 'object' && objectToString(d) === '[object Date]';
}
exports.isDate = isDate;


function isError(e) {
  return typeof e === 'object' && objectToString(e) === '[object Error]';
}
exports.isError = isError;

exports.inherits = function(ctor, superCtor) {
  ctor.super_ = superCtor;
  ctor.prototype = Object.create(superCtor.prototype, {
    constructor: {
      value: ctor,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });
};

Mark Hahn

unread,
May 1, 2013, 2:26:51 PM5/1/13
to nod...@googlegroups.com
Deep copy:   obj2 = JSON.parse(JSON.stringify(obj1));

Of course you need to be careful about dates and recursion.

Ilya Shaisultanov

unread,
May 1, 2013, 2:29:33 PM5/1/13
to nod...@googlegroups.com
I actually use this for simple objects, though performance is not great.

Mikeal Rogers

unread,
May 1, 2013, 2:44:50 PM5/1/13
to nod...@googlegroups.com
For what its worth i sent about a half a day dealing with json serializing with circular references and copy issues. The solution ended up being removing underscore for lodash and doing _.clone(obj, true);

Highly recommended.

-Mikeal

On May 1, 2013, at 11:26AM, Mark Hahn <ma...@hahnca.com> wrote:

Deep copy:   obj2 = JSON.parse(JSON.stringify(obj1));

Of course you need to be careful about dates and recursion.


Isaac Schlueter

unread,
May 6, 2013, 12:04:42 PM5/6/13
to nodejs
@glanglais83

util.inherits() and the is* functions are all used by node core. is*
functions are used on the repl, and I don't consider a repl an
optional feature for a programming platform.

Wil Moore

unread,
May 7, 2013, 1:58:47 AM5/7/13
to nod...@googlegroups.com
On Wednesday, May 1, 2013 12:26:51 PM UTC-6, Mark Hahn wrote:
Deep copy:   obj2 = JSON.parse(JSON.stringify(obj1));

Just a side-note: `obj2` will skip function properties.


chakrit

unread,
May 8, 2013, 12:03:52 AM5/8/13
to nod...@googlegroups.com
Underscore and lodash already does pretty much of what you will ever want.

I'd say keep the core small and use your _ for whatever's extra util you need.

FWIW there is also https://github.com/chakrit/node-uneval so `eval(uneval(o))` might work for you (also handles functions).

Of course, all the caveats of eval/evil applies.
Reply all
Reply to author
Forward
0 new messages