Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ cfg.toXML(); // output as a XML string.
or from a command line:
```
to-json.js input.xml
to-yaml input.json
to-xml ipnut.yml
to-yaml.js input.json
to-xml.js ipnut.yml
```
or via STDIN
```
to-json.js < input.xml
to-yaml.js < input.json
to-xml.js < ipnut.yml
```
12 changes: 11 additions & 1 deletion bin/to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const Path = require('path');
const Configuration = require(Path.join(__dirname, '..', 'configuration.js'));

let input = process.argv[2];
let cfg = Configuration.fromFile(input);
let cfg;

if (input) {
// a filename argument was given.
cfg = Configuration.fromFile(input);

} else {
// no arguments given so read STDIN.
cfg = Configuration.fromSTDIN();

}

process.stdout.write(cfg.toJSON());
12 changes: 11 additions & 1 deletion bin/to-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const Path = require('path');
const Configuration = require(Path.join(__dirname, '..', 'configuration.js'));

let input = process.argv[2];
let cfg = Configuration.fromFile(input);
let cfg;

if (input) {
// a filename argument was given.
cfg = Configuration.fromFile(input);

} else {
// no arguments given so read STDIN.
cfg = Configuration.fromSTDIN();

}

process.stdout.write(cfg.toXML());
12 changes: 11 additions & 1 deletion bin/to-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const Path = require('path');
const Configuration = require(Path.join(__dirname, '..', 'configuration.js'));

let input = process.argv[2];
let cfg = Configuration.fromFile(input);
let cfg;

if (input) {
// a filename argument was given.
cfg = Configuration.fromFile(input);

} else {
// no arguments given so read STDIN.
cfg = Configuration.fromSTDIN();

}

process.stdout.write(cfg.toYAML());
33 changes: 28 additions & 5 deletions configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ const XML = require('fast-xml-parser');

const libPath = Path.join(__dirname, 'lib');
const Parsers = require(Path.join(libPath, 'parsers.js'));
const STDIN = require(Path.join(libPath, 'stdin.js'));


class Configuration extends Object {

constructor(input) {
constructor(raw, type) {
super();

Object.assign(this, input);
let parser = Parsers[type];
let inpStr = raw.toString();

let content = parser.parse(inpStr);

Object.assign(this, content);
}

toJSON() {
Expand All @@ -41,10 +47,27 @@ class Configuration extends Object {
let type = MIME.lookup(filename);
let raw = File.readFileSync(filename);

let parser = Parsers[type];
let content = parser.parse(raw.toString());
return new this(raw, type);
}

static fromSTDIN() {
let raw = this.STDIN.get();
let type;

if (Parsers.isJSON(raw)) {
type = MIME.lookup('json');
} else if (Parsers.isYAML(raw)) {
type = MIME.lookup('yml');
} else if (Parsers.isXML(raw)) {
type = MIME.lookup('xml');
}

return new this(raw, type);
}

return new this(content);
static get STDIN() {
// exposed for testability/stubbing.
return STDIN;
}

}
Expand Down
19 changes: 19 additions & 0 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ const Parsers = {
'text/yaml': YAML,
'application/json': JSON,
'application/xml': XML,

isJSON: function(str) {
let match = str.match(/^\{[\{\}\[\]\"\'\w\s\:\,]+[\}\s]$/gi);

return !!match && match.toString() === str;
},

isYAML: function(str) {
let match = str.match(/[\w\s\:\[\]^\{^\}^\<^\>\,\-]+/gi);

return !!match && match.toString() === str;
},

isXML: function(str) {
let match = str.match(/^[\<\>\w\s\/\=\'\"]+$/gi);

return !!match && match.toString() === str;
},

};


Expand Down
37 changes: 37 additions & 0 deletions lib/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

const File = require('fs');


class STDIN {

static get BUFSIZE() {
return this._bufSize || 256;
}

static set BUFSIZE(val) {
return this._bufSize = val;
}

static get() {
let len = 0;
let contents = '';
let file = process.stdin.fd;

let bufSize = this.BUFSIZE;
let buf = new Buffer(bufSize);

do {
len = File.readSync(file, buf, 0, bufSize, null);

let str = buf.toString();
contents += str.slice(0, len);

} while (len > 0);

return contents;
}

}


module.exports = STDIN;
108 changes: 105 additions & 3 deletions spec/configuration_spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

const Path = require('path');
const File = require('fs');

const YAML = require('yaml');
const XML = require('fast-xml-parser');

const MIME = require('mime-types');

const specPath = Path.join(__dirname);
const rootPath = Path.join(specPath, '..');
const supportPath = Path.join(specPath, 'support');
Expand All @@ -24,7 +28,7 @@ describe(Configuration.name, () => {
instance = Configuration.fromFile(jsonStub);
});

describe('the toJSON() function.', () => {
describe('the toJSON() function', () => {
beforeEach(() => {
result = instance.toJSON();
});
Expand All @@ -41,7 +45,7 @@ describe(Configuration.name, () => {

});

describe('the toXML() function.', () => {
describe('the toXML() function', () => {
beforeEach(() => {
result = instance.toXML();
});
Expand All @@ -58,7 +62,7 @@ describe(Configuration.name, () => {

});

describe('the toYAML() function.', () => {
describe('the toYAML() function', () => {
beforeEach(() => {
result = instance.toYAML();
});
Expand Down Expand Up @@ -157,4 +161,102 @@ describe(Configuration.name, () => {

});

describe('the fromSTDIN() static function', () => {
let input;
let stub;
let promise = Promise.resolve();
let file;

beforeEach(() => {
let baseLib = Configuration.STDIN;

input = File.readFileSync(stub);

spyOn(baseLib, 'get')
.and.returnValue(input.toString(), MIME.lookup(stub));
});

describe('given JSON input', () => {
stub = jsonStub;

it('parses the given input object', () => {
let res = Configuration.fromSTDIN();
let obj = res.anObject;

expect(obj.one).toEqual(1);
expect(obj.two).toEqual(2);
expect(obj.three).toEqual(3);
expect(obj.four).toEqual(4);

});

it('parses the given input array', () => {
let res = Configuration.fromSTDIN();
let ary = res.anArray;

expect(ary[0]).toEqual(1);
expect(ary[1]).toEqual(2);
expect(ary[2]).toEqual(3);
expect(ary[3]).toEqual(4);

});

});

describe('given YAML input', () => {
stub = yamlStub;

it('parses the given input object', () => {
let res = Configuration.fromSTDIN();
let obj = res.anObject;

expect(obj.one).toEqual(1);
expect(obj.two).toEqual(2);
expect(obj.three).toEqual(3);
expect(obj.four).toEqual(4);

});

it('parses the given input array', () => {
let res = Configuration.fromSTDIN();
let ary = res.anArray;

expect(ary[0]).toEqual(1);
expect(ary[1]).toEqual(2);
expect(ary[2]).toEqual(3);
expect(ary[3]).toEqual(4);

});

});

describe('given XML input', () => {
stub = xmlStub;

it('parses the given input object', () => {
let res = Configuration.fromSTDIN();
let obj = res.anObject;

expect(obj.one).toEqual(1);
expect(obj.two).toEqual(2);
expect(obj.three).toEqual(3);
expect(obj.four).toEqual(4);

});

it('parses the given input array', () => {
let res = Configuration.fromSTDIN();
let ary = res.anArray;

expect(ary[0]).toEqual(1);
expect(ary[1]).toEqual(2);
expect(ary[2]).toEqual(3);
expect(ary[3]).toEqual(4);

});

});

});

});
Loading