From e4e2f747b30f443a5d981be0a96c8f04c5da5f62 Mon Sep 17 00:00:00 2001 From: djiao Date: Fri, 13 Feb 2015 23:34:04 -0500 Subject: [PATCH] Added examples to use stringify. Updated readme. --- README.md | 81 ++++++++++++++++++++++++++++------- lib/marc4js.js | 6 ++- lib/stringify.js | 10 ++++- samples/stringify_callback.js | 38 ++++++++++++++++ samples/stringify_stream.js | 27 ++++++++++++ test/stringify.js | 7 +++ 6 files changed, 150 insertions(+), 19 deletions(-) create mode 100644 samples/stringify_callback.js create mode 100644 samples/stringify_stream.js diff --git a/README.md b/README.md index 82727cf..c7f8044 100644 --- a/README.md +++ b/README.md @@ -11,20 +11,20 @@ npm install marc4js ## Examples -Examples can be found in the `samples` directory if source code is cloned from github. +Examples can be found in the `samples` directory if source code is cloned from github. ### Parsers -Parsers read different formats into marc4js.marc.Record objects. +Parsers read different formats into `marc4js.marc.Record` objects. #### Parse MARC file #### `marc4js.parse` -`marc4js.parse` parse a string in MARC format and creates `marc4js.marc.Record` objects. +`marc4js.parse` parse a string/Buffer in MARC format and creates `marc4js.marc.Record` objects. ##### Using the callback API -The parser receive a string/buffer and return am array of `marc4js.marc.Record` objects +The parser receive a string/Buffer and return am array of `marc4js.marc.Record` objects inside a user-provided callback. This example is available with the command `node samples/callback.js`. @@ -43,19 +43,11 @@ fs.readFile('samples/PGA-other-2.mrc', function(err, data) { ##### Using the stream API and pipe function The following example uses the stream API of the parser. Since the parser always emit -objects, therefore it is always in the non-flowing mode - meaning one can only attach +objects, it always uses the non-flowing mode - one can only attach a 'data' event listener, but not a 'readable' event listener. The object passed to the callback function in the 'data' event listener is a `marc4js.marc.Record` object. ```javascript -'use strict'; - -// require should -// npm install should -g - -// run this in the marc4js directory -// node samples/parse_stream.js - var marc4js = require('marc4js'); var fs = require('fs'); var should = require('should'); @@ -79,7 +71,7 @@ fs.readFile('samples/PGA-other-2.mrc', function(err, data) { One useful function of the Stream API is pipe to interact between multiple streams. You may use this function to pipe a stream.Readable string source to a stream.Writable -object destination. This example is available as node samples/parse_pipe.js. It reads a +object destination. This example is available as `node samples/parse_pipe.js`. It reads a file, parses its content and then transform it back to MARC string. ```javascript @@ -107,7 +99,7 @@ is.pipe(parser).pipe(stringifier).pipe(process.stdout); This example uses the pipe and the stream API to parse a marc file. -``` +```javascript var marc4js = require('marc4js'); var fs = require('fs'); var should = require('should'); @@ -139,8 +131,65 @@ Transformers transform the marc4js.marc.Record objects into other formats. ##### Using the callback API +The following example uses the callback API. The API can convert a single record +or an array of records into a string in MARC format. The following code snippet is +part of the example code. For the full version, see `samples/parse_pipe.js`. + +```javascript +var marc4js = require('marc4js'); + +// build a record from scratch +var record = new marc4js.marc.Record(); +// build the record ... + +// converts a single record +marc4js.stringify(record, {}, function(err, data) { + console.log(data); +}); + +// build an array of records +var records = []; +records.push(record); +var record2 = new marc4js.marc.Record(); +// build the record ... +records.push(record2); + +// converts an array of records +marc4js.stringify(records, {}, function(err, data) { + console.log(data); +}); +``` + ##### Using the stream API -##### Using the pipe function +Same as `marc4js.parse`, `marc4js.stringify` also provice usage through the stream API. +Complete version of code is in `samples/stringify_stream` + +```javascript +var marc4js = require('marc4js'); + +var stringifier = marc4js.stringify(); +var output = ''; +stringifier.on('data', function(chunk) { + output += chunk +}); +stringifier.on('error', function(err) { + console.log(err.message); +}); +stringifier.on('end', function() { + console.log(output); +}); +var records = []; +// build records ... +records.forEach(function(record) { + stringifier.write(record); +}); +stringifier.end(); +``` + +As shown in the parser examples, stringifier and other transformers can be used +in a pipe that is in the down stream of a parser pipe. For details see the example + `samples/parser_pipe.js` + diff --git a/lib/marc4js.js b/lib/marc4js.js index 1092f1e..f93824f 100644 --- a/lib/marc4js.js +++ b/lib/marc4js.js @@ -2,7 +2,11 @@ exports.marc = { Record: require('./marc/record'), - DataField: require('./marc/data_field') + DataField: require('./marc/data_field'), + ControlField: require('./marc/control_field'), + Leader: require('./marc/leader'), + Subfield: require('./marc/subfield'), + VariableField: require('./marc/variable_field') }; exports.parse = require('./parse'); diff --git a/lib/stringify.js b/lib/stringify.js index a9702ca..0678292 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -10,14 +10,20 @@ var Record = require('./marc/record'); var Leader = require('./marc/leader'); module.exports = function() { - var callback, called, chunks, data, options, stringifier; + var callback, chunks, data, options, stringifier; if (arguments.length === 3) { - data = arguments[0]; + if (Array.isArray(arguments[0])) { + data = arguments[0]; + } else { + data = [arguments[0]]; + } options = arguments[1]; callback = arguments[2]; } else if (arguments.length === 2) { if (Array.isArray(arguments[0])) { data = arguments[0]; + } else if (arguments[0] instanceof Record) { + data = [arguments[0]]; } else { options = arguments[0]; } diff --git a/samples/stringify_callback.js b/samples/stringify_callback.js new file mode 100644 index 0000000..5ac4448 --- /dev/null +++ b/samples/stringify_callback.js @@ -0,0 +1,38 @@ +'use strict'; + +// run this in the marc4js directory +// node samples/stringify_callback.js + +var marc4js = require('marc4js'); + +// build a record from scratch +var record = new marc4js.marc.Record(); +record.leader = new marc4js.marc.Leader('00307nam a2200085Ia 45e0'); +record.addVariableField(new marc4js.marc.ControlField("008", "080906s9999 xx 000 0 und d")); +record.addVariableField(new marc4js.marc.DataField("100", '1', ' ', [["a", "Biggers, Earl Derr."]])); +record.addVariableField(new marc4js.marc.DataField("245", '1', '0', [["a", "Charlie Chan Carries On"], ["h", "[electronic resource]"]])); +record.addVariableField(new marc4js.marc.DataField("500", ' ', ' ', [["a", "An ebook provided by Project Gutenberg Australia"]])); +record.addVariableField(new marc4js.marc.DataField("856", '4', '0', [["u", "http://gutenberg.net.au/ebooks07/0700761h.html "]])); + +// converts a single record +marc4js.stringify(record, {}, function(err, data) { + console.log(data); +}); + +// build an array of records +var records = []; +records.push(record); + +var record2 = new marc4js.marc.Record(); +record2.leader = new marc4js.marc.Leader('00287nam a2200085Ia 45e0'); +record2.addVariableField(new marc4js.marc.ControlField("008", "080906s9999 xx 000 0 und d")); +record2.addVariableField(new marc4js.marc.DataField("100", '1', ' ', [["a", "Wallace, Edgar."]])); +record2.addVariableField(new marc4js.marc.DataField("245", '1', '0', [["a", "Sanders"], ["h", "[electronic resource]"]])); +record2.addVariableField(new marc4js.marc.DataField("500", ' ', ' ', [["a", "An ebook provided by Project Gutenberg Australia"]])); +record2.addVariableField(new marc4js.marc.DataField("856", '4', '0', [["u", "http://gutenberg.net.au/ebooks07/0700771h.html "]])); +records.push(record2); + +// converts an array of records +marc4js.stringify(records, {}, function(err, data) { + console.log(data); +}); \ No newline at end of file diff --git a/samples/stringify_stream.js b/samples/stringify_stream.js new file mode 100644 index 0000000..e2ab230 --- /dev/null +++ b/samples/stringify_stream.js @@ -0,0 +1,27 @@ +'use strict'; + +// require should +// npm install should -g + +// run this in the marc4js directory +// node samples/stringify_stream.js + +var marc4js = require('marc4js'); + +var stringifier = marc4js.stringify(); +var output = ''; +stringifier.on('data', function(chunk) { + output += chunk +}); +stringifier.on('error', function(err) { + console.log(err.message); +}); +stringifier.on('end', function() { + console.log(output); +}); +var records = []; +// build records ... +records.forEach(function(record) { + stringifier.write(record); +}); +stringifier.end(); \ No newline at end of file diff --git a/test/stringify.js b/test/stringify.js index 9cf9be6..3b7cb18 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -53,6 +53,13 @@ describe('stringify', function () { }); }); + it('should stringify one record', function(done) { + stringify(records[0], function(err, output) { + expect(output).to.be.not.null; + done(); + }); + }); + it('should provide a flowing stream API', function(done) { var stringifier = stringify({objectMode: true}); var output = '';