pax_global_header00006660000000000000000000000064126463605000014514gustar00rootroot0000000000000052 comment=37e3fd40076d7e6f92b9ca9a09a4cf600e6bac24 clean-yaml-object-0.1.0/000077500000000000000000000000001264636050000150005ustar00rootroot00000000000000clean-yaml-object-0.1.0/.editorconfig000066400000000000000000000003471264636050000174610ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false clean-yaml-object-0.1.0/.gitattributes000066400000000000000000000000141264636050000176660ustar00rootroot00000000000000* text=auto clean-yaml-object-0.1.0/.gitignore000066400000000000000000000000421264636050000167640ustar00rootroot00000000000000node_modules .nyc_output coverage clean-yaml-object-0.1.0/.travis.yml000066400000000000000000000002131264636050000171050ustar00rootroot00000000000000language: node_js node_js: - 'stable' - '0.12' - '0.10' after_script: - 'cat ./coverage/lcov.info | ./node_modules/.bin/coveralls' clean-yaml-object-0.1.0/index.js000066400000000000000000000042001264636050000164410ustar00rootroot00000000000000'use strict'; module.exports = function (object, filterFn) { return cleanYamlObj(object, filterFn || defaultFilter, true, []); }; function cleanYamlObj(object, filter, isRoot, seen) { if (object === undefined) { return null; } if (typeof object === 'function') { return object.toString(); } if (Buffer.isBuffer(object)) { return 'Buffer\n' + object.toString('hex').split('') .reduce(function (set, c) { if (set.length && set[set.length - 1].length === 1) { set[set.length - 1] += c; if (set.length && set.length % 20 === 0) { set[set.length - 1] += '\n'; } else { set[set.length - 1] += ' '; } } else { set.push(c); } return set; }, []).join('').trim(); } if (object && typeof object === 'object') { if (object instanceof RegExp) { return object.toString(); } seen = seen.concat([object]); var isArray = Array.isArray(object); // Fill in any holes. This means we lose expandos, // but we were gonna lose those anyway. if (isArray) { object = Array.apply(null, object); } var isError = object && typeof object === 'object' && object instanceof Error; var set = isArray ? [] : {}; // name is typically not an ownProperty on an Error if (isError && object.name && !object.hasOwnProperty('name') && filter('name', isRoot, object, set)) { setProp('name', object, set, seen, filter); } var keys = Object.getOwnPropertyNames(object); return keys.reduce(function (set, k) { // magic property! if (isArray && k === 'length') { return set; } // Don't dump massive EventEmitter and Domain // objects onto the output, that's never friendly. if (isError && /^domain/.test(k)) { return set; } if (!filter(k, isRoot, object, set)) { return set; } setProp(k, object, set, seen, filter); return set; }, set); } return object; } function setProp(propName, source, target, seen, filter) { if (seen.indexOf(source[propName]) === -1) { target[propName] = cleanYamlObj(source[propName], filter, false, seen); } else { target[propName] = '[Circular]'; } } function defaultFilter() { return true; } clean-yaml-object-0.1.0/license000066400000000000000000000023131264636050000163440ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Isaac Z. Schlueter , James Talmage (github.com/jamestalmage), and Contributors Extracted from code in node-tap http://www.node-tap.org/ 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. clean-yaml-object-0.1.0/package.json000066400000000000000000000013211264636050000172630ustar00rootroot00000000000000{ "name": "clean-yaml-object", "version": "0.1.0", "description": "Clean up an object prior to serialization", "license": "MIT", "repository": "tapjs/clean-yaml-object", "author": { "name": "James Talmage", "email": "james@talmage.io", "url": "github.com/jamestalmage" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "xo && nyc --cache --reporter=lcov --reporter=text ava" }, "files": [ "index.js" ], "keywords": [ "serialize", "clean", "dedupe", "circular", "yaml", "json", "error" ], "dependencies": {}, "devDependencies": { "ava": "^0.10.0", "coveralls": "^2.11.6", "nyc": "^5.3.0", "xo": "^0.12.1" } } clean-yaml-object-0.1.0/readme.md000066400000000000000000000027411264636050000165630ustar00rootroot00000000000000# clean-yaml-object [![Build Status](https://travis-ci.org/tapjs/clean-yaml-object.svg?branch=master)](https://travis-ci.org/tapjs/clean-yaml-object) [![Coverage Status](https://coveralls.io/repos/tapjs/clean-yaml-object/badge.svg?branch=master&service=github)](https://coveralls.io/github/tapjs/clean-yaml-object?branch=master) > Clean up an object prior to serialization. Replaces circular references, pretty prints Buffers, and numerous other enhancements. Primarily designed to prepare Errors for serialization to JSON/YAML. Extracted from [`node-tap`](https://github.com/tapjs/node-tap) ## Install ``` $ npm install --save clean-yaml-object ``` ## Usage ```js const cleanYamlObject = require('clean-yaml-object'); cleanYamlObject(new Error('foo')); //=> {name: 'Error', message: 'foo', stack: ...} ``` ## API ### cleanYamlObject(input, [filterFn]) Returns a deep copy of `input` that is suitable for serialization. #### input Type: `*` Any object. #### filterFn Type: `callback(propertyName, isRoot, source, target)` Optional filter callback. Returning `true` will cause the property to be copied. Otherwise it will be skipped - `propertyName`: The property being copied. - `isRoot`: `true` only if `source` is the top level object passed to `copyYamlObject` - `source`: The source from which `source[propertyName]` will be copied. - `target`: The target object. ## License MIT © [Isaac Z. Schlueter](http://github.com/isaacs) [James Talmage](http://github.com/jamestalmage) clean-yaml-object-0.1.0/test.js000066400000000000000000000060171264636050000163210ustar00rootroot00000000000000import test from 'ava'; import fn from './'; import domain from 'domain'; test('undefined === null', t => t.is(fn(undefined), null)); test('fn === fn.toString()', t => { function toStr() { return 'foo'; } t.ok(/function toStr\(\) {[\s\n]+return 'foo'/m.test(fn(toStr))); }); test('Buffer outputs hex representation', t => { const arr = []; for (var i = 0; i < 50; i++) { arr[i] = i; } t.is(fn(new Buffer(arr)), [ 'Buffer', '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13', '14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27', '28 29 2a 2b 2c 2d 2e 2f 30 31' ].join('\n')); }); test('regExp === regExp.toString()', t => t.is(fn(/foo|bar/), '/foo|bar/')); test('Array holes are filled', t => { const array = ['a']; array[4] = 'c'; t.same(fn(array), ['a', null, null, null, 'c']); }); test.cb('Errors have their domain stripped', t => { t.plan(2); // These two extra properties show up in Node `0.10` const filter = k => !/^(type|arguments)/.test(k); domain.create() .on('error', e => { t.same( Object.getOwnPropertyNames(e).filter(filter).sort(), ['domain', 'domainThrown', 'message', 'stack'] ); t.same(Object.keys(fn(e, filter)).sort(), ['message', 'name', 'stack']); t.end(); }) .run(() => { setTimeout(() => { throw new Error('foo'); }, 0); }); }); test('exposes error properties', t => { const serialized = fn(new Error('foo')); const x = Object.keys(serialized); t.not(x.indexOf('name'), -1, `name should be exposed even though it's on the prototype`); t.not(x.indexOf('stack'), -1); t.not(x.indexOf('message'), -1); }); test('should destroy circular references', t => { const obj = {}; obj.child = {parent: obj}; const serialized = fn(obj); t.is(typeof serialized, 'object'); t.is(serialized.child.parent, '[Circular]'); }); test('should not affect the original object', t => { const obj = {}; obj.child = {parent: obj}; const serialized = fn(obj); t.not(serialized, obj); t.is(obj.child.parent, obj); }); test('should only destroy parent references', t => { const obj = {}; const common = {thing: obj}; obj.one = {firstThing: common}; obj.two = {secondThing: common}; const serialized = fn(obj); t.is(typeof serialized.one.firstThing, 'object'); t.is(typeof serialized.two.secondThing, 'object'); t.is(serialized.one.firstThing.thing, '[Circular]'); t.is(serialized.two.secondThing.thing, '[Circular]'); }); test('works if its own parent', t => { const obj = {}; obj.parent = obj; t.same(fn(obj), {parent: '[Circular]'}); }); test('should work on arrays', t => { const obj = {}; const common = [obj]; const x = [common]; const y = [['test'], common]; y[0][1] = y; obj.a = {x: x}; obj.b = {y: y}; const serialized = fn(obj); t.true(Array.isArray(serialized.a.x)); t.is(serialized.a.x[0][0], '[Circular]'); t.is(serialized.b.y[0][0], 'test'); t.is(serialized.b.y[1][0], '[Circular]'); t.is(serialized.b.y[0][1], '[Circular]'); }); test('custom filter', t => { t.same( fn({a: 'a', b: 'b', c: 'c'}, k => k === 'c'), {c: 'c'} ); });