How to use traverse

Comprehensive traverse code examples:

How to use traverse.map:

33
34
35
36
37
38
39
40
41
42
43
    assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] });
};


exports.mapT = function () {
    var obj = { a : 1, b : 2, c : [ 3, 4 ] };
    var res = Traverse.map(obj, function (x) {
        if (typeof x === 'number' && x % 2 === 0) {
            this.update(x * 10);
        }
    });

How to use traverse.get:

5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
    var path = msg.callbacks[id];
    traverse.set(args, path, f(id));
});

forEach(msg.links || [], function (link) {
    var value = traverse.get(args, link.from);
    traverse.set(args, link.to, value);
});

return args;

How to use traverse.forEach:

11
12
13
14
15
16
17
18
19
20
21
    assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] });
};


exports.mutateT = function () {
    var obj = { a : 1, b : 2, c : [ 3, 4 ] };
    var res = Traverse.forEach(obj, function (x) {
        if (typeof x === 'number' && x % 2 === 0) {
            this.update(x * 10);
        }
    });

How to use traverse.clone:

55
56
57
58
59
60
61
62
63
64
65
    assert.deepEqual(res.c, [ 3, 4 ]);
};


exports.cloneT = function () {
    var obj = { a : 1, b : 2, c : [ 3, 4 ] };
    var res = Traverse.clone(obj);
    assert.deepEqual(obj, res);
    assert.ok(obj !== res);
    obj.a ++;
    assert.deepEqual(res.a, 1);

How to use traverse.set:

5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
Scrubber.prototype.unscrub = function (msg, f) {
    var args = msg.arguments || [];
    forEach(objectKeys(msg.callbacks || {}), function (sid) {
        var id = parseInt(sid, 10);
        var path = msg.callbacks[id];
        traverse.set(args, path, f(id));
    });
    
    forEach(msg.links || [], function (link) {
        var value = traverse.get(args, link.from);

How to use traverse.deepEqual:

0
1
2
3
4
5
6
7
8
9
10
var assert = require('assert');
var traverse = require('traverse');


exports.deepDates = function () {
    assert.ok(
        traverse.deepEqual(
            { d : new Date, x : [ 1, 2, 3 ] },
            { d : new Date, x : [ 1, 2, 3 ] }
        ),
        'dates should be equal'