From f0f76cc6b08849644a79a828e249be2d8896797a Mon Sep 17 00:00:00 2001 From: James Halliday Date: Fri, 18 Feb 2011 02:39:44 -0800 Subject: [PATCH] tests pass again with the rewrite --- package.json | 6 +- test/hash.js | 186 --------------------------------------------------- test/json.js | 32 ++++----- 3 files changed, 18 insertions(+), 206 deletions(-) delete mode 100644 test/hash.js diff --git a/package.json b/package.json index 8dbaaee..d9ef4b2 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,10 @@ { "name" : "traverse", "version" : "0.3.0", - "description" : "Traverse and transform objects by visiting every node on a recursive walk.", + "description" : "Traverse and transform objects by visiting every node on a recursive walk", "author" : "James Halliday", "license" : "MIT/X11", "main" : "./index", - "modules" : { - "web" : "./lib/web", - "hash" : "./lib/hash" - }, "repository" : { "type" : "git", "url" : "http://github.com/substack/js-traverse.git" diff --git a/test/hash.js b/test/hash.js deleted file mode 100644 index 18e1386..0000000 --- a/test/hash.js +++ /dev/null @@ -1,186 +0,0 @@ -#!/usr/bin/env node -var Hash = require('traverse/hash'); - -exports.map = function (assert) { - var ref = { a : 1, b : 2 }; - var items = Hash(ref).map(function (v) { return v + 1 }).items; - var hash = Hash.map(ref, function (v) { return v + 1 }); - assert.deepEqual(ref, { a : 1, b : 2 }); - assert.deepEqual(items, { a : 2, b : 3 }); - assert.deepEqual(hash, { a : 2, b : 3 }); -}; - -exports['cloned map'] = function (assert) { - var ref = { foo : [1,2], bar : [4,5] }; - var hash = Hash(ref).clone.map( - function (v) { v.unshift(v[0] - 1); return v } - ).items; - assert.deepEqual(ref.foo, [1,2]); - assert.deepEqual(ref.bar, [4,5]); - assert.deepEqual(hash.foo, [0,1,2]); - assert.deepEqual(hash.bar, [3,4,5]); -}; - -exports.reduce = function (assert) { - var ref = { foo : [1,2], bar : [4,5] }; - - var sum1 = Hash(ref).reduce(function (acc, v) { - return acc + v.length - }, 0); - assert.equal(sum1, 4); - - var sum2 = Hash.reduce(ref, function (acc, v) { - return acc + v.length - }, 0); - assert.equal(sum2, 4); -}; - -exports['filter and values'] = function (assert) { - var ref = { a : 5, b : 2, c : 7, 1337 : 'leet' }; - var items = Hash(ref).filter(function (v, k) { - return v > 5 || k > 5 - }).items; - var hash = Hash.filter(ref, function (v, k) { return v > 5 || k > 5 }); - assert.deepEqual(items, { 1337 : 'leet', c : 7 }); - assert.deepEqual(hash, { 1337 : 'leet', c : 7 }); - assert.deepEqual(ref, { a : 5, b : 2, c : 7, 1337 : 'leet' }); - assert.equal(Hash(ref).length, 4); -}; - -exports.length = function (assert) { - assert.equal(Hash({ a : 1, b : [2,3], c : 4 }).length, 3); - assert.equal(Hash({ a : 1, b : [2,3], c : 4 }).size, 3); - assert.equal(Hash.size({ a : 1, b : [2,3], c : 4 }), 3); -}; - -exports.concat = function (assert) { - var ref1 = { a : 1, b : 2 }; - var ref2 = { foo : 100, bar : 200 }; - var ref3 = { b : 3, c : 4, bar : 300 }; - - assert.deepEqual( - Hash.concat([ ref1, ref2 ]), - { a : 1, b : 2, foo : 100, bar : 200 } - ); - - assert.deepEqual( - Hash.concat([ ref1, ref2, ref3 ]), - { a : 1, b : 3, c : 4, foo : 100, bar : 300 } - ); -}; - -exports.update = function (assert) { - var ref = { a : 1, b : 2 }; - var items = Hash(ref).clone.update({ c : 3, a : 0 }).items; - assert.deepEqual(ref, { a : 1, b : 2 }); - assert.deepEqual(items, { a : 0, b : 2, c : 3 }); - - var hash = Hash.update(ref, { c : 3, a : 0 }); - assert.deepEqual(ref, hash); - assert.deepEqual(hash, { a : 0, b : 2, c : 3 }); -}; - -exports.merge = function (assert) { - var ref = { a : 1, b : 2 }; - var items = Hash(ref).merge({ b : 3, c : 3.14 }).items; - var hash = Hash.merge(ref, { b : 3, c : 3.14 }); - - assert.deepEqual(ref, { a : 1, b : 2 }); - assert.deepEqual(items, { a : 1, b : 3, c : 3.14 }); - assert.deepEqual(hash, { a : 1, b : 3, c : 3.14 }); -}; - -exports.extract = function (assert) { - var hash = Hash({ a : 1, b : 2, c : 3 }).clone; - var extracted = hash.extract(['a','b']); - assert.equal(extracted.length, 2); - assert.deepEqual(extracted.items, { a : 1, b : 2 }); -}; - -exports.exclude = function (assert) { - var hash = Hash({ a : 1, b : 2, c : 3 }).clone; - var extracted = hash.exclude(['a','b']); - assert.equal(extracted.length, 1); - assert.deepEqual(extracted.items, { c : 3 }); -}; - -exports.compact = function (assert) { - var hash = { - a : 1, - b : undefined, - c : false, - d : 4, - e : [ undefined, 4 ], - f : null - }; - var compacted = Hash(hash).compact; - assert.deepEqual( - { - a : 1, - b : undefined, - c : false, - d : 4, - e : [ undefined, 4 ], - f : null - }, - hash, 'compact modified the hash' - ); - assert.deepEqual( - compacted.items, - { - a : 1, - c : false, - d : 4, - e : [ undefined, 4 ], - f : null - } - ); - var h = Hash.compact(hash); - assert.deepEqual(h, compacted.items); -}; - -exports.valuesAt = function (assert) { - var h = { a : 4, b : 5, c : 6 }; - assert.equal(Hash(h).valuesAt('a'), 4); - assert.equal(Hash(h).valuesAt(['a'])[0], 4); - assert.deepEqual(Hash(h).valuesAt(['a','b']), [4,5]); - assert.equal(Hash.valuesAt(h, 'a'), 4); - assert.deepEqual(Hash.valuesAt(h, ['a']), [4]); - assert.deepEqual(Hash.valuesAt(h, ['a','b']), [4,5]); -}; - -exports.zip = function (assert) { - var xs = ['a','b','c']; - var ys = [1,2,3,4]; - var h = Hash(xs,ys); - assert.equal(h.length, 3); - assert.deepEqual(h.items, { a : 1, b : 2, c : 3 }); - - var zipped = Hash.zip(xs,ys); - assert.deepEqual(zipped, { a : 1, b : 2, c : 3 }); -}; - -exports.has = function (assert) { - var h = { a : 4, b : 5 }; - var hh = Hash(h); - - assert.ok(hh.has('a')); - assert.equal(hh.has('c'), false); - assert.ok(hh.has(['a','b'])); - assert.equal(hh.has(['a','b','c']), false); - - assert.ok(Hash.has(h, 'a')); - assert.equal(Hash.has(h, 'c'), false); - assert.ok(Hash.has(h, ['a','b'])); - assert.equal(Hash.has(h, ['a','b','c']), false); -}; - -exports.detect = function (assert) { - var h = { a : 5, b : 6, c : 7, d : 8 }; - var hh = Hash(h); - var gt6hh = hh.detect(function (x) { return x > 6 }); - assert.ok(gt6hh == 7 || gt6hh == 8); - var gt6h = Hash.detect(h, function (x) { return x > 6 }); - assert.ok(gt6h == 7 || gt6h == 8); - assert.equal(hh.detect(function (x) { return x > 100 }), undefined); -}; diff --git a/test/json.js b/test/json.js index 45e418e..9bb4875 100644 --- a/test/json.js +++ b/test/json.js @@ -8,39 +8,41 @@ exports['json test'] = function (assert) { var obj = { moo : function () {}, foo : [2,3,4, function () {}] }; var scrubbed = Traverse(obj).map(function (x) { - if (x instanceof Function) { + if (typeof x === 'function') { callbacks[id] = { id : id, f : x, path : this.path }; this.update('[Function]'); id++; } - }).value; + }); assert.equal( scrubbed.moo, '[Function]', 'obj.moo replaced with "[Function]"' ); + assert.equal( scrubbed.foo[3], '[Function]', 'obj.foo[3] replaced with "[Function]"' - ) - assert.equal( - JSON.stringify(scrubbed), - '{"moo":"[Function]","foo":[2,3,4,"[Function]"]}', - 'Full JSON string matches' ); - assert.equal( + + assert.eql(scrubbed, { + moo : '[Function]', + foo : [ 2, 3, 4, "[Function]" ] + }, 'Full JSON string matches'); + + assert.eql( typeof obj.moo, 'function', 'Original obj.moo still a function' ); - assert.equal( + + assert.eql( typeof obj.foo[3], 'function', 'Original obj.foo[3] still a function' ); - assert.equal( - sys.inspect(callbacks), - "{ '54': { id: 54, f: [Function], path: [ 'moo' ] }\n" - + ", '55': { id: 55, f: [Function], path: [ 'foo', '3' ] }\n}", - 'Check the generated callbacks list' - ); + + assert.eql(callbacks, { + 54: { id: 54, f : obj.moo, path: [ 'moo' ] }, + 55: { id: 55, f : obj.foo[3], path: [ 'foo', '3' ] }, + }, 'Check the generated callbacks list'); };