[Pkg-javascript-commits] [node-object-inspect] 01/06: Imported Upstream version 1.1.0

Ross Gammon ross-guest at moszumanska.debian.org
Sat Nov 19 09:05:36 UTC 2016


This is an automated email from the git hooks/post-receive script.

ross-guest pushed a commit to branch master
in repository node-object-inspect.

commit 25bb9ad3c569edade07e1a4cc3a23b80739221d7
Author: Ross Gammon <rossgammon at mail.dk>
Date:   Sat Nov 19 07:44:16 2016 +0100

    Imported Upstream version 1.1.0
---
 .gitignore          |   1 +
 .travis.yml         |  52 ++++++++++++++
 LICENSE             |  18 +++++
 example/all.js      |  17 +++++
 example/circular.js |   4 ++
 example/fn.js       |   3 +
 example/inspect.js  |   7 ++
 index.js            | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json        |  46 +++++++++++++
 readme.markdown     |  59 ++++++++++++++++
 test/browser/dom.js |  15 ++++
 test/circular.js    |   9 +++
 test/deep.js        |   9 +++
 test/element.js     |  51 ++++++++++++++
 test/err.js         |  29 ++++++++
 test/fn.js          |  16 +++++
 test/has.js         |  31 +++++++++
 test/holes.js       |  15 ++++
 test/inspect.js     |   8 +++
 test/lowbyte.js     |  12 ++++
 test/undef.js       |  12 ++++
 test/values.js      |  94 +++++++++++++++++++++++++
 22 files changed, 700 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..b0273e1
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,52 @@
+language: node_js
+node_js:
+  - "iojs-v3.2"
+  - "iojs-v3.1"
+  - "iojs-v3.0"
+  - "iojs-v2.5"
+  - "iojs-v2.4"
+  - "iojs-v2.3"
+  - "iojs-v2.2"
+  - "iojs-v2.1"
+  - "iojs-v2.0"
+  - "iojs-v1.8"
+  - "iojs-v1.7"
+  - "iojs-v1.6"
+  - "iojs-v1.5"
+  - "iojs-v1.4"
+  - "iojs-v1.3"
+  - "iojs-v1.2"
+  - "iojs-v1.1"
+  - "iojs-v1.0"
+  - "0.12"
+  - "0.11"
+  - "0.10"
+  - "0.9"
+  - "0.8"
+  - "0.6"
+  - "0.4"
+before_install:
+  - '[ "${TRAVIS_NODE_VERSION}" = "0.6" ] || npm install -g npm at 1.4.28 && npm install -g npm'
+sudo: false
+matrix:
+  fast_finish: true
+  allow_failures:
+    - node_js: "iojs-v3.1"
+    - node_js: "iojs-v3.0"
+    - node_js: "iojs-v2.4"
+    - node_js: "iojs-v2.3"
+    - node_js: "iojs-v2.2"
+    - node_js: "iojs-v2.1"
+    - node_js: "iojs-v2.0"
+    - node_js: "iojs-v1.7"
+    - node_js: "iojs-v1.6"
+    - node_js: "iojs-v1.5"
+    - node_js: "iojs-v1.4"
+    - node_js: "iojs-v1.3"
+    - node_js: "iojs-v1.2"
+    - node_js: "iojs-v1.1"
+    - node_js: "iojs-v1.0"
+    - node_js: "0.11"
+    - node_js: "0.9"
+    - node_js: "0.6"
+    - node_js: "0.4"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ee27ba4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,18 @@
+This software is released under the MIT license:
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/example/all.js b/example/all.js
new file mode 100644
index 0000000..6d2d4be
--- /dev/null
+++ b/example/all.js
@@ -0,0 +1,17 @@
+var inspect = require('../');
+var holes = [ 'a', 'b' ];
+holes[4] = 'e', holes[6] = 'g';
+var obj = {
+    a: 1,
+    b: [ 3, 4, undefined, null ],
+    c: undefined,
+    d: null,
+    e: {
+        regex: /^x/i,
+        buf: new Buffer('abc'),
+        holes: holes
+    },
+    now: new Date
+};
+obj.self = obj;
+console.log(inspect(obj));
diff --git a/example/circular.js b/example/circular.js
new file mode 100644
index 0000000..1006d0c
--- /dev/null
+++ b/example/circular.js
@@ -0,0 +1,4 @@
+var inspect = require('../');
+var obj = { a: 1, b: [3,4] };
+obj.c = obj;
+console.log(inspect(obj));
diff --git a/example/fn.js b/example/fn.js
new file mode 100644
index 0000000..4c00ba6
--- /dev/null
+++ b/example/fn.js
@@ -0,0 +1,3 @@
+var inspect = require('../');
+var obj = [ 1, 2, function f (n) { return n + 5 }, 4 ];
+console.log(inspect(obj));
diff --git a/example/inspect.js b/example/inspect.js
new file mode 100644
index 0000000..b5ad4d1
--- /dev/null
+++ b/example/inspect.js
@@ -0,0 +1,7 @@
+var inspect = require('../');
+
+var d = document.createElement('div');
+d.setAttribute('id', 'beep');
+d.innerHTML = '<b>wooo</b><i>iiiii</i>';
+
+console.log(inspect([ d, { a: 3, b : 4, c: [5,6,[7,[8,[9]]]] } ]));
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..c34abd0
--- /dev/null
+++ b/index.js
@@ -0,0 +1,192 @@
+var hasMap = typeof Map === 'function' && Map.prototype;
+var mapSizeDescriptor = Object.getOwnPropertyDescriptor && hasMap ? Object.getOwnPropertyDescriptor(Map.prototype, 'size') : null;
+var mapSize = hasMap && mapSizeDescriptor && typeof mapSizeDescriptor.get === 'function' ? mapSizeDescriptor.get : null;
+var mapForEach = hasMap && Map.prototype.forEach;
+var hasSet = typeof Set === 'function' && Set.prototype;
+var setSizeDescriptor = Object.getOwnPropertyDescriptor && hasSet ? Object.getOwnPropertyDescriptor(Set.prototype, 'size') : null;
+var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'function' ? setSizeDescriptor.get : null;
+var setForEach = hasSet && Set.prototype.forEach;
+
+module.exports = function inspect_ (obj, opts, depth, seen) {
+    if (!opts) opts = {};
+    
+    var maxDepth = opts.depth === undefined ? 5 : opts.depth;
+    if (depth === undefined) depth = 0;
+    if (depth >= maxDepth && maxDepth > 0
+    && obj && typeof obj === 'object') {
+        return '[Object]';
+    }
+    
+    if (seen === undefined) seen = [];
+    else if (indexOf(seen, obj) >= 0) {
+        return '[Circular]';
+    }
+    
+    function inspect (value, from) {
+        if (from) {
+            seen = seen.slice();
+            seen.push(from);
+        }
+        return inspect_(value, opts, depth + 1, seen);
+    }
+    
+    if (typeof obj === 'string') {
+        return inspectString(obj);
+    }
+    else if (typeof obj === 'function') {
+        var name = nameOf(obj);
+        return '[Function' + (name ? ': ' + name : '') + ']';
+    }
+    else if (obj === null) {
+        return 'null';
+    }
+    else if (isSymbol(obj)) {
+        var symString = Symbol.prototype.toString.call(obj);
+        return typeof obj === 'object' ? 'Object(' + symString + ')' : symString;
+    }
+    else if (isElement(obj)) {
+        var s = '<' + String(obj.nodeName).toLowerCase();
+        var attrs = obj.attributes || [];
+        for (var i = 0; i < attrs.length; i++) {
+            s += ' ' + attrs[i].name + '="' + quote(attrs[i].value) + '"';
+        }
+        s += '>';
+        if (obj.childNodes && obj.childNodes.length) s += '...';
+        s += '</' + String(obj.nodeName).toLowerCase() + '>';
+        return s;
+    }
+    else if (isArray(obj)) {
+        if (obj.length === 0) return '[]';
+        var xs = Array(obj.length);
+        for (var i = 0; i < obj.length; i++) {
+            xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
+        }
+        return '[ ' + xs.join(', ') + ' ]';
+    }
+    else if (isError(obj)) {
+        var parts = [];
+        for (var key in obj) {
+            if (!has(obj, key)) continue;
+            
+            if (/[^\w$]/.test(key)) {
+                parts.push(inspect(key) + ': ' + inspect(obj[key]));
+            }
+            else {
+                parts.push(key + ': ' + inspect(obj[key]));
+            }
+        }
+        if (parts.length === 0) return '[' + obj + ']';
+        return '{ [' + obj + '] ' + parts.join(', ') + ' }';
+    }
+    else if (typeof obj === 'object' && typeof obj.inspect === 'function') {
+        return obj.inspect();
+    }
+    else if (isMap(obj)) {
+        var parts = [];
+        mapForEach.call(obj, function (value, key) {
+            parts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
+        });
+        return 'Map (' + mapSize.call(obj) + ') {' + parts.join(', ') + '}';
+    }
+    else if (isSet(obj)) {
+        var parts = [];
+        setForEach.call(obj, function (value ) {
+            parts.push(inspect(value, obj));
+        });
+        return 'Set (' + setSize.call(obj) + ') {' + parts.join(', ') + '}';
+    }
+    else if (typeof obj === 'object' && !isDate(obj) && !isRegExp(obj)) {
+        var xs = [], keys = [];
+        for (var key in obj) {
+            if (has(obj, key)) keys.push(key);
+        }
+        keys.sort();
+        for (var i = 0; i < keys.length; i++) {
+            var key = keys[i];
+            if (/[^\w$]/.test(key)) {
+                xs.push(inspect(key) + ': ' + inspect(obj[key], obj));
+            }
+            else xs.push(key + ': ' + inspect(obj[key], obj));
+        }
+        if (xs.length === 0) return '{}';
+        return '{ ' + xs.join(', ') + ' }';
+    }
+    else return String(obj);
+};
+
+function quote (s) {
+    return String(s).replace(/"/g, '"');
+}
+
+function isArray (obj) { return toStr(obj) === '[object Array]' }
+function isDate (obj) { return toStr(obj) === '[object Date]' }
+function isRegExp (obj) { return toStr(obj) === '[object RegExp]' }
+function isError (obj) { return toStr(obj) === '[object Error]' }
+function isSymbol (obj) { return toStr(obj) === '[object Symbol]' }
+
+var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
+function has (obj, key) {
+    return hasOwn.call(obj, key);
+}
+
+function toStr (obj) {
+    return Object.prototype.toString.call(obj);
+}
+
+function nameOf (f) {
+    if (f.name) return f.name;
+    var m = f.toString().match(/^function\s*([\w$]+)/);
+    if (m) return m[1];
+}
+
+function indexOf (xs, x) {
+    if (xs.indexOf) return xs.indexOf(x);
+    for (var i = 0, l = xs.length; i < l; i++) {
+        if (xs[i] === x) return i;
+    }
+    return -1;
+}
+
+function isMap (x) {
+    if (!mapSize) {
+        return false;
+    }
+    try {
+        mapSize.call(x);
+        return true;
+    } catch (e) {}
+    return false;
+}
+
+function isSet (x) {
+    if (!setSize) {
+        return false;
+    }
+    try {
+        setSize.call(x);
+        return true;
+    } catch (e) {}
+    return false;
+}
+
+function isElement (x) {
+    if (!x || typeof x !== 'object') return false;
+    if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
+        return true;
+    }
+    return typeof x.nodeName === 'string'
+        && typeof x.getAttribute === 'function'
+    ;
+}
+
+function inspectString (str) {
+    var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
+    return "'" + s + "'";
+    
+    function lowbyte (c) {
+        var n = c.charCodeAt(0);
+        var x = { 8: 'b', 9: 't', 10: 'n', 12: 'f', 13: 'r' }[n];
+        if (x) return '\\' + x;
+        return '\\x' + (n < 0x10 ? '0' : '') + n.toString(16);
+    }
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..88a1a0b
--- /dev/null
+++ b/package.json
@@ -0,0 +1,46 @@
+{
+  "name": "object-inspect",
+  "version": "1.1.0",
+  "description": "string representations of objects in node and the browser",
+  "main": "index.js",
+  "devDependencies": {
+    "tape": "^4.2.2"
+  },
+  "scripts": {
+    "test": "tape test/*.js"
+  },
+  "testling": {
+    "files": [
+      "test/*.js",
+      "test/browser/*.js"
+    ],
+    "browsers": [
+      "ie/6..latest",
+      "chrome/latest",
+      "firefox/latest",
+      "safari/latest",
+      "opera/latest",
+      "iphone/latest",
+      "ipad/latest",
+      "android/latest"
+    ]
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/substack/object-inspect.git"
+  },
+  "homepage": "https://github.com/substack/object-inspect",
+  "keywords": [
+    "inspect",
+    "util.inspect",
+    "object",
+    "stringify",
+    "pretty"
+  ],
+  "author": {
+    "name": "James Halliday",
+    "email": "mail at substack.net",
+    "url": "http://substack.net"
+  },
+  "license": "MIT"
+}
diff --git a/readme.markdown b/readme.markdown
new file mode 100644
index 0000000..41959a4
--- /dev/null
+++ b/readme.markdown
@@ -0,0 +1,59 @@
+# object-inspect
+
+string representations of objects in node and the browser
+
+[![testling badge](https://ci.testling.com/substack/object-inspect.png)](https://ci.testling.com/substack/object-inspect)
+
+[![build status](https://secure.travis-ci.org/substack/object-inspect.png)](http://travis-ci.org/substack/object-inspect)
+
+# example
+
+## circular
+
+``` js
+var inspect = require('object-inspect');
+var obj = { a: 1, b: [3,4] };
+obj.c = obj;
+console.log(inspect(obj));
+```
+
+## dom element
+
+``` js
+var inspect = require('object-inspect');
+
+var d = document.createElement('div');
+d.setAttribute('id', 'beep');
+d.innerHTML = '<b>wooo</b><i>iiiii</i>';
+
+console.log(inspect([ d, { a: 3, b : 4, c: [5,6,[7,[8,[9]]]] } ]));
+```
+
+output:
+
+```
+[ <div id="beep">...</div>, { a: 3, b: 4, c: [ 5, 6, [ 7, [ 8, [ ... ] ] ] ] } ]
+```
+
+# methods
+
+``` js
+var inspect = require('object-inspect')
+```
+
+## var s = inspect(obj, opts={})
+
+Return a string `s` with the string representation of `obj` up to a depth of
+`opts.depth`.
+
+# install
+
+With [npm](https://npmjs.org) do:
+
+```
+npm install object-inspect
+```
+
+# license
+
+MIT
diff --git a/test/browser/dom.js b/test/browser/dom.js
new file mode 100644
index 0000000..18a3d70
--- /dev/null
+++ b/test/browser/dom.js
@@ -0,0 +1,15 @@
+var inspect = require('../../');
+var test = require('tape');
+
+test('dom element', function (t) {
+    t.plan(1);
+    
+    var d = document.createElement('div');
+    d.setAttribute('id', 'beep');
+    d.innerHTML = '<b>wooo</b><i>iiiii</i>';
+    
+    t.equal(
+        inspect([ d, { a: 3, b : 4, c: [5,6,[7,[8,[9]]]] } ]),
+        '[ <div id="beep">...</div>, { a: 3, b: 4, c: [ 5, 6, [ 7, [ 8, [Object] ] ] ] } ]'
+    );
+});
diff --git a/test/circular.js b/test/circular.js
new file mode 100644
index 0000000..28598a7
--- /dev/null
+++ b/test/circular.js
@@ -0,0 +1,9 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('circular', function (t) {
+    t.plan(1);
+    var obj = { a: 1, b: [3,4] };
+    obj.c = obj;
+    t.equal(inspect(obj), '{ a: 1, b: [ 3, 4 ], c: [Circular] }');
+});
diff --git a/test/deep.js b/test/deep.js
new file mode 100644
index 0000000..a8dbb58
--- /dev/null
+++ b/test/deep.js
@@ -0,0 +1,9 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('deep', function (t) {
+    t.plan(2);
+    var obj = [ [ [ [ [ [ 500 ] ] ] ] ] ];
+    t.equal(inspect(obj), '[ [ [ [ [ [Object] ] ] ] ] ]');
+    t.equal(inspect(obj, { depth: 2 }), '[ [ [Object] ] ]');
+});
diff --git a/test/element.js b/test/element.js
new file mode 100644
index 0000000..66df4b8
--- /dev/null
+++ b/test/element.js
@@ -0,0 +1,51 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('element', function (t) {
+    t.plan(1);
+    var elem = {
+        nodeName: 'div',
+        attributes: [ { name: 'class', value: 'row' } ],
+        getAttribute: function (key) {},
+        childNodes: []
+    };
+    var obj = [ 1, elem, 3 ];
+    t.deepEqual(inspect(obj), '[ 1, <div class="row"></div>, 3 ]');
+});
+    
+test('element no attr', function (t) {
+    t.plan(1);
+    var elem = {
+        nodeName: 'div',
+        getAttribute: function (key) {},
+        childNodes: []
+    };
+    var obj = [ 1, elem, 3 ];
+    t.deepEqual(inspect(obj), '[ 1, <div></div>, 3 ]');
+});
+
+test('element with contents', function (t) {
+    t.plan(1);
+    var elem = {
+        nodeName: 'div',
+        getAttribute: function (key) {},
+        childNodes: [ { nodeName: 'b' } ]
+    };
+    var obj = [ 1, elem, 3 ];
+    t.deepEqual(inspect(obj), '[ 1, <div>...</div>, 3 ]');
+});
+
+test('element instance', function (t) {
+    t.plan(1);
+    var h = global.HTMLElement;
+    global.HTMLElement = function (name, attr) {
+        this.nodeName = name;
+        this.attributes = attr;
+    };
+    global.HTMLElement.prototype.getAttribute = function () {};
+    
+    var elem = new(global.HTMLElement)('div', []);
+    var obj = [ 1, elem, 3 ];
+    t.deepEqual(inspect(obj), '[ 1, <div></div>, 3 ]');
+    global.HTMLElement = h;
+});
diff --git a/test/err.js b/test/err.js
new file mode 100644
index 0000000..0f31343
--- /dev/null
+++ b/test/err.js
@@ -0,0 +1,29 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('type error', function (t) {
+    t.plan(1);
+    var aerr = new TypeError;
+    aerr.foo = 555;
+    aerr.bar = [1,2,3];
+    
+    var berr = new TypeError('tuv');
+    berr.baz = 555;
+    
+    var cerr = new SyntaxError;
+    cerr.message = 'whoa';
+    cerr['a-b'] = 5;
+    
+    var obj = [
+        new TypeError,
+        new TypeError('xxx'),
+        aerr, berr, cerr
+    ];
+    t.equal(inspect(obj), '[ ' + [
+        '[TypeError]',
+        '[TypeError: xxx]',
+        '{ [TypeError] foo: 555, bar: [ 1, 2, 3 ] }',
+        '{ [TypeError: tuv] baz: 555 }',
+        '{ [SyntaxError: whoa] message: \'whoa\', \'a-b\': 5 }'
+    ].join(', ') + ' ]');
+});
diff --git a/test/fn.js b/test/fn.js
new file mode 100644
index 0000000..55357db
--- /dev/null
+++ b/test/fn.js
@@ -0,0 +1,16 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('function', function (t) {
+    t.plan(1);
+    var obj = [ 1, 2, function f (n) {}, 4 ];
+    t.equal(inspect(obj), '[ 1, 2, [Function: f], 4 ]');
+});
+
+test('function name', function (t) {
+    t.plan(1);
+    var f = function () {};
+    f.toString = function () { return 'function xxx () {}' };
+    var obj = [ 1, 2, f, 4 ];
+    t.equal(inspect(obj), '[ 1, 2, [Function: xxx], 4 ]');
+});
diff --git a/test/has.js b/test/has.js
new file mode 100644
index 0000000..e1970b3
--- /dev/null
+++ b/test/has.js
@@ -0,0 +1,31 @@
+var inspect = require('../');
+var test = require('tape');
+
+var withoutProperty = function (object, property, fn) {
+    var original;
+    if (Object.getOwnPropertyDescriptor) {
+        original = Object.getOwnPropertyDescriptor(object, property);
+    } else {
+        original = object[property];
+    }
+    delete object[property];
+    try {
+        fn();
+    } finally {
+        if (Object.getOwnPropertyDescriptor) {
+            Object.defineProperty(object, property, original);
+        } else {
+            object[property] = original;
+        }
+    }
+};
+
+test('when Object#hasOwnProperty is deleted', function (t) {
+    t.plan(1);
+    var arr = [1, , 3];
+    Array.prototype[1] = 2; // this is needed to account for "in" vs "hasOwnProperty"
+    withoutProperty(Object.prototype, 'hasOwnProperty', function () {
+        t.equal(inspect(arr), '[ 1, , 3 ]');
+    });
+	delete Array.prototype[1];
+});
diff --git a/test/holes.js b/test/holes.js
new file mode 100644
index 0000000..ae54de4
--- /dev/null
+++ b/test/holes.js
@@ -0,0 +1,15 @@
+var test = require('tape');
+var inspect = require('../');
+
+var xs = [ 'a', 'b' ];
+xs[5] = 'f';
+xs[7] = 'j';
+xs[8] = 'k';
+
+test('holes', function (t) {
+    t.plan(1);
+    t.equal(
+        inspect(xs),
+        "[ 'a', 'b', , , , 'f', , 'j', 'k' ]"
+    );
+});
diff --git a/test/inspect.js b/test/inspect.js
new file mode 100644
index 0000000..12e231a
--- /dev/null
+++ b/test/inspect.js
@@ -0,0 +1,8 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('inspect', function (t) {
+    t.plan(1);
+    var obj = [ { inspect: function () { return '!XYZ¡' } }, [] ];
+    t.equal(inspect(obj), '[ !XYZ¡, [] ]');
+});
diff --git a/test/lowbyte.js b/test/lowbyte.js
new file mode 100644
index 0000000..debd59c
--- /dev/null
+++ b/test/lowbyte.js
@@ -0,0 +1,12 @@
+var test = require('tape');
+var inspect = require('../');
+
+var obj = { x: 'a\r\nb', y: '\5! \x1f \022' };
+
+test('interpolate low bytes', function (t) {
+    t.plan(1);
+    t.equal(
+        inspect(obj),
+        "{ x: 'a\\r\\nb', y: '\\x05! \\x1f \\x12' }"
+    );
+});
diff --git a/test/undef.js b/test/undef.js
new file mode 100644
index 0000000..833238f
--- /dev/null
+++ b/test/undef.js
@@ -0,0 +1,12 @@
+var test = require('tape');
+var inspect = require('../');
+
+var obj = { a: 1, b: [ 3, 4, undefined, null ], c: undefined, d: null };
+
+test('undef and null', function (t) {
+    t.plan(1);
+    t.equal(
+        inspect(obj),
+        '{ a: 1, b: [ 3, 4, undefined, null ], c: undefined, d: null }'
+    );
+});
diff --git a/test/values.js b/test/values.js
new file mode 100644
index 0000000..af10dbe
--- /dev/null
+++ b/test/values.js
@@ -0,0 +1,94 @@
+var inspect = require('../');
+var test = require('tape');
+
+test('values', function (t) {
+    t.plan(1);
+    var obj = [ {}, [], { 'a-b': 5 } ];
+    t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
+});
+
+test('has', function (t) {
+    t.plan(1);
+    var has = Object.prototype.hasOwnProperty;
+    delete Object.prototype.hasOwnProperty;
+    t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
+    Object.prototype.hasOwnProperty = has;
+});
+
+test('indexOf seen', function (t) {
+    t.plan(1);
+    var xs = [ 1, 2, 3, {} ];
+    xs.push(xs);
+    
+    var seen = [];
+    seen.indexOf = undefined;
+    
+    t.equal(
+        inspect(xs, {}, 0, seen),
+        '[ 1, 2, 3, {}, [Circular] ]'
+    );
+});
+
+test('seen seen', function (t) {
+    t.plan(1);
+    var xs = [ 1, 2, 3 ];
+    
+    var seen = [ xs ];
+    seen.indexOf = undefined;
+    
+    t.equal(
+        inspect(xs, {}, 0, seen),
+        '[Circular]'
+    );
+});
+
+test('seen seen seen', function (t) {
+    t.plan(1);
+    var xs = [ 1, 2, 3 ];
+    
+    var seen = [ 5, xs ];
+    seen.indexOf = undefined;
+    
+    t.equal(
+        inspect(xs, {}, 0, seen),
+        '[Circular]'
+    );
+});
+
+test('symbols', { skip: typeof Symbol !== 'function' }, function (t) {
+    var sym = Symbol('foo');
+    t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
+    t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
+    t.end();
+});
+
+test('Map', { skip: typeof Map !== 'function' }, function (t) {
+    var map = new Map();
+    map.set({ a: 1 }, ['b']);
+    map.set(3, NaN);
+    var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
+    t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
+    t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
+
+    var nestedMap = new Map();
+    nestedMap.set(nestedMap, map);
+    t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
+
+    t.end();
+});
+
+test('Set', { skip: typeof Set !== 'function' }, function (t) {
+    var set = new Set();
+    set.add({ a: 1 });
+    set.add(['b']);
+    var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
+    t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
+    t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
+
+    var nestedSet = new Set();
+    nestedSet.add(set);
+    nestedSet.add(nestedSet);
+    t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
+
+    t.end();
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-object-inspect.git



More information about the Pkg-javascript-commits mailing list