[Pkg-javascript-commits] [node-clean-yaml-object] 01/21: init commit
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Sep 7 09:52:17 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-clean-yaml-object.
commit b882a7f2c9e44661f940e9de692c316ad5c5e3e7
Author: James Talmage <james at talmage.io>
Date: Fri Jan 15 21:57:54 2016 -0500
init commit
---
.editorconfig | 15 ++++++++
.gitattributes | 1 +
.gitignore | 1 +
.travis.yml | 5 +++
index.js | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
license | 23 ++++++++++++
package.json | 29 ++++++++++++++
readme.md | 46 +++++++++++++++++++++++
test.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 344 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8f9d77e
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+root = 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
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
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..2a2f1c6
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 'stable'
+ - '0.12'
+ - '0.10'
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..402f583
--- /dev/null
+++ b/index.js
@@ -0,0 +1,117 @@
+'use strict';
+module.exports = function (object, filterFn) {
+ return cleanYamlObj(object, filterFn || defaultFilter, true, []);
+};
+
+// Copied verbatim from node-tap:
+// https://github.com/isaacs/node-tap/blob/e9fdcdec5914204e814f922e7e677aff6d1ffc9e/lib/test.js#L1076-L1159
+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();
+ }
+
+ 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)) {
+ 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)) {
+ return set;
+ }
+
+ setProp(k, object, set, seen, filter);
+
+ return set;
+ }, set);
+ }
+
+ return object;
+}
+
+/*
+ var stack = null;
+
+ ...
+
+ // put this in filter
+
+ if (isRoot && k === 'stack') {
+ stack = object[k];
+ return set;
+ }
+
+....
+
+ if (stack) {
+ newObj.stack = stack;
+ }
+ return newObj;
+
+ */
+
+function setProp(propName, source, target, seen, filter) {
+ if (seen.indexOf(source[propName]) === -1) {
+ target[propName] = cleanYamlObj(source[propName], filter, false, seen.concat([source]));
+ } else {
+ target[propName] = '[Circular]';
+ }
+}
+
+function defaultFilter(k, isRoot, object) {
+ if (isRoot && (k === 'todo' || k === 'skip')) {
+ return false;
+ }
+
+ return !(isRoot && k === 'at' && !object[k]);
+}
diff --git a/license b/license
new file mode 100644
index 0000000..8dc0833
--- /dev/null
+++ b/license
@@ -0,0 +1,23 @@
+The MIT License (MIT)
+
+Copyright (c) Isaac Z. Schlueter <i at izs.me>, James Talmage <james at talmage.io> (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.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..59a7fbb
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+ "name": "clean-yaml-object",
+ "version": "0.0.0",
+ "description": "My flawless module",
+ "license": "MIT",
+ "repository": "jamestalmage/clean-yaml-object",
+ "author": {
+ "name": "James Talmage",
+ "email": "james at talmage.io",
+ "url": "github.com/jamestalmage"
+ },
+ "engines": {
+ "node": ">=0.10.0"
+ },
+ "scripts": {
+ "test": "xo && ava"
+ },
+ "files": [
+ "index.js"
+ ],
+ "keywords": [
+ ""
+ ],
+ "dependencies": {},
+ "devDependencies": {
+ "ava": "^0.10.0",
+ "xo": "^0.12.1"
+ }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..7c5d3aa
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,46 @@
+# clean-yaml-object [![Build Status](https://travis-ci.org/jamestalmage/clean-yaml-object.svg?branch=master)](https://travis-ci.org/jamestalmage/clean-yaml-object)
+
+> My flawless module
+
+
+## Install
+
+```
+$ npm install --save clean-yaml-object
+```
+
+
+## Usage
+
+```js
+const cleanYamlObject = require('clean-yaml-object');
+
+cleanYamlObject('unicorns');
+//=> 'unicorns & rainbows'
+```
+
+
+## API
+
+### cleanYamlObject(input, [options])
+
+#### input
+
+Type: `string`
+
+Lorem ipsum.
+
+#### options
+
+##### foo
+
+Type: `boolean`
+Default: `false`
+
+Lorem ipsum.
+
+
+## License
+
+
+MIT © [Isaac Z. Schlueter](http://github.com/isaacs) [James Talmage](http://github.com/jamestalmage)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..07e80ad
--- /dev/null
+++ b/test.js
@@ -0,0 +1,107 @@
+import 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);
+ domain.create()
+ .on('error', e => {
+ t.same(
+ Object.getOwnPropertyNames(e).sort(),
+ ['domain', 'domainThrown', 'message', 'stack']
+ );
+ t.same(Object.keys(fn(e)).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.skip('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]');
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-clean-yaml-object.git
More information about the Pkg-javascript-commits
mailing list