Skip to content

Commit

Permalink
Added examples to use stringify. Updated readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
djiao committed Feb 14, 2015
1 parent 6f62fbc commit e4e2f74
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 19 deletions.
81 changes: 65 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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`



6 changes: 5 additions & 1 deletion lib/marc4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
10 changes: 8 additions & 2 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
38 changes: 38 additions & 0 deletions samples/stringify_callback.js
Original file line number Diff line number Diff line change
@@ -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);
});
27 changes: 27 additions & 0 deletions samples/stringify_stream.js
Original file line number Diff line number Diff line change
@@ -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();
7 changes: 7 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down

0 comments on commit e4e2f74

Please sign in to comment.