Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to ESM #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
68 changes: 33 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ npm install marc4js

marc4js provides the following features

* An easy to use API that can handle large record sets.
* Uses Node.js stream API and pipe functions for parsing and writing ISO2709 format, MarcEdit text (mrk) format, MARC in JSON, and MARCXML.
* Offers callback functions for parsing and writing various formats.
* SAX based MARCXML parsing that doesn't in-memory storage of records while parsing. Able to parse large MARCXML file with ease.
* A MARC record object model for in-memory editing of MARC records, similar to the Marc4J object model
* Supports UTF-8 encoded marc files and MARC-8 encoded marc files (It requires [marc8](https://www.npmjs.com/package/marc8) to handle MARC-8 encoded files).
- An easy to use API that can handle large record sets.
- Uses Node.js stream API and pipe functions for parsing and writing ISO2709 format, MarcEdit text (mrk) format, MARC in JSON, and MARCXML.
- Offers callback functions for parsing and writing various formats.
- SAX based MARCXML parsing that doesn't in-memory storage of records while parsing. Able to parse large MARCXML file with ease.
- A MARC record object model for in-memory editing of MARC records, similar to the Marc4J object model
- Supports UTF-8 encoded marc files and MARC-8 encoded marc files (It requires [marc8](https://www.npmjs.com/package/marc8) to handle MARC-8 encoded files).

## Examples

Examples can be found in the the [marc4js_examples](https://github.com/jiaola/marc4js_examples). You can also find
examples in the test directory.
examples in the test directory.

## Usage

```javascript
var marc4js = require('marc4js');
import marc4js from "marc4js";
```

### Parsers
Expand All @@ -48,12 +48,9 @@ marc4js.parse(data, options, function(err, records) {

```javascript
var parser = marc4js.parse(options);
parser.on('data', function(record) {
});
parser.on('end', function() {
});
parser.on('error', function(err) {
});
parser.on("data", function (record) {});
parser.on("end", function () {});
parser.on("error", function (err) {});
parser.write(data);
parser.end();
```
Expand All @@ -67,7 +64,10 @@ the stream api is disabled, and is always set to `true`. Listening to the `reada

```javascript
var parser = marc4js.parse(options);
fs.createReadStream('/path/to/your/file').pipe(parser).pipe(transformer).pipe(process.stdout);
fs.createReadStream("/path/to/your/file")
.pipe(parser)
.pipe(transformer)
.pipe(process.stdout);
```

#### options
Expand All @@ -86,8 +86,8 @@ Parses MarcEdit text format (.mrk files). Used when `format` is `mrk`

Other options:

* `spaceReplace`: In MarcEdit mrk files, spaces in data field indicators or control fields are replace by `\`. By default
MrkPaser will convert `\` to space in those places. It can be configured with this option.
- `spaceReplace`: In MarcEdit mrk files, spaces in data field indicators or control fields are replace by `\`. By default
MrkPaser will convert `\` to space in those places. It can be configured with this option.

##### TextParser

Expand All @@ -102,7 +102,7 @@ The callback API will read all records in memory and return it in the callback f

Other options:

* `strict`: default is `false`. When in `strict` mode, the parser will fail if the XML is not well-formatted. For details, see the `strict` option in [sax-js](https://github.com/isaacs/sax-js).
- `strict`: default is `false`. When in `strict` mode, the parser will fail if the XML is not well-formatted. For details, see the `strict` option in [sax-js](https://github.com/isaacs/sax-js).

##### MijParser

Expand All @@ -129,12 +129,9 @@ marc4js.transform(records, options, function(err, output) {

```javascript
var transformer = marc4js.transform(options);
transformer.on('readable', function(output) {
});
transformer.on('end', function() {
});
transformer.on('error', function(err) {
});
transformer.on("readable", function (output) {});
transformer.on("end", function () {});
transformer.on("error", function (err) {});
transformer.write(record); // one record
// or to write an array of records
// records.forEach(function(record) {
Expand All @@ -151,7 +148,10 @@ if flowing mode is used.

```javascript
var transformer = marc4js.transform(options);
fs.createReadStream('/path/to/your/file').pipe(parser).pipe(transformer).pipe(process.stdout);
fs.createReadStream("/path/to/your/file")
.pipe(parser)
.pipe(transformer)
.pipe(process.stdout);
```

#### options
Expand All @@ -171,7 +171,7 @@ Outputs MarcEdit text format (.mrk files). Used when `format` is `mrk`

Other options:

* `spaceReplace`: by default space in data field indicators and control fields are replaced with `\`. But it can be configured with this option.
- `spaceReplace`: by default space in data field indicators and control fields are replaced with `\`. But it can be configured with this option.

##### TextTransformer

Expand All @@ -183,19 +183,17 @@ Outputs MarcEdit text format (.mrk files). Used when `format` is `marcxml` or `x

Other options:

* `pretty`: default is `true`. Output XML in pretty format. If set to false, new indentation and line-breakers in outputs.
* `indent`: default is `' '` (two spaces). Used to indent lines in pretty format.
* `newline`: default is `\n`. Used in pretty format.
* `declaration`: default is `true`. If set to `false`, the XML declaration line (`<?xml versiont ...>`) is not included in the output.
* `root`: default is `true`. If `false`, the root `<collection>` element is not included in the output.
- `pretty`: default is `true`. Output XML in pretty format. If set to false, new indentation and line-breakers in outputs.
- `indent`: default is `' '` (two spaces). Used to indent lines in pretty format.
- `newline`: default is `\n`. Used in pretty format.
- `declaration`: default is `true`. If set to `false`, the XML declaration line (`<?xml versiont ...>`) is not included in the output.
- `root`: default is `true`. If `false`, the root `<collection>` element is not included in the output.

##### MijTransformer

Outputs [MARC-in-JSON](https://rossfsinger.com/blog/2010/09/a-proposal-to-serialize-marc-in-json/) string. Used when `format` is `json` or `mij`.

Other options:

* asArray: default is `true`. By default the output will be in an JSON array format, even if there is only one record.
If this option set to false, the output will not write the enclosing brackets `[` and `]` at the beginning and end of the output.


- asArray: default is `true`. By default the output will be in an JSON array format, even if there is only one record.
If this option set to false, the output will not write the enclosing brackets `[` and `]` at the beginning and end of the output.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/marc4js');
export { default } from "./lib/marc4js.js";
14 changes: 6 additions & 8 deletions lib/marc/control_field.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

var VariableField = require('./variable_field');
import VariableField from "./variable_field.js";

var ControlField = function (tag, data) {
if (typeof tag !== 'undefined') this._tag = tag;
if (typeof data !== 'undefined') this._data = data;
if (typeof tag !== "undefined") this._tag = tag;
if (typeof data !== "undefined") this._data = data;
};
ControlField.prototype = Object.create(VariableField.prototype);

Expand All @@ -16,8 +14,8 @@ Object.defineProperties(ControlField.prototype, {
},
set: function (val) {
this._data = val;
}
}
},
},
});

ControlField.prototype.marshal = function () {
Expand All @@ -28,4 +26,4 @@ ControlField.prototype.toString = function () {
return this.marshal();
};

module.exports = ControlField;
export default ControlField;
50 changes: 25 additions & 25 deletions lib/marc/data_field.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';

var VariableField = require('./variable_field');
var Subfield = require('./subfield');
var MarcError = require('../marc_error');
import VariableField from "./variable_field.js";
import Subfield from "./subfield.js";
import MarcError from "../marc_error.js";

var DataField = function (tag, ind1, ind2, subfields) {
if (typeof tag !== 'undefined') this._tag = tag;
if (typeof ind1 !== 'undefined') this._indicator1 = ind1;
if (typeof ind2 !== 'undefined') this._indicator2 = ind2;
if (typeof subfields !== 'undefined') {
this._subfields = subfields.map(function(f) {
if (typeof tag !== "undefined") this._tag = tag;
if (typeof ind1 !== "undefined") this._indicator1 = ind1;
if (typeof ind2 !== "undefined") this._indicator2 = ind2;
if (typeof subfields !== "undefined") {
this._subfields = subfields.map(function (f) {
return new Subfield(f[0], f[1]);
});
} else {
Expand All @@ -26,44 +24,46 @@ Object.defineProperties(DataField.prototype, {
},
set: function (val) {
this._indicator1 = val;
}
},
},
indicator2: {
get: function () {
return this._indicator2;
},
set: function (val) {
this._indicator2 = val;
}
},
},
subfields: {
get: function () {
return this._subfields;
},
set: function (val) {
this._subfields = val;
}
}
},
},
});

DataField.prototype.unmarshal = function (data) {
this.indicator1 = data.charAt(0);
this.indicator2 = data.charAt(1);
this.subfields = data.substr(3).split('\x1f').map(function(sf) {
var code = sf.substr(0, 1);
if (code < 0) throw new MarcError("unexpected end of data field");
var subfield = new Subfield();
subfield.code = code;
subfield.data = sf.substr(1);
return subfield;
});
this.subfields = data
.substr(3)
.split("\x1f")
.map(function (sf) {
var code = sf.substr(0, 1);
if (code < 0) throw new MarcError("unexpected end of data field");
var subfield = new Subfield();
subfield.code = code;
subfield.data = sf.substr(1);
return subfield;
});
};

DataField.prototype.addSubfield = function (subfield) {
this.subfields.push(subfield);
};


DataField.prototype.removeSubfield = function (subfield) {
if (this.subfields.indexOf(subfield) != -1) {
this.subfields.splice(this.subfields.indexOf(subfield), 1);
Expand All @@ -89,7 +89,7 @@ DataField.prototype.find = function (pattern) {
DataField.prototype.marshal = function () {
var str = this.indicator1 + this.indicator2;
this.subfields.forEach(function (subfield, index, array) {
str = str + '\x1f' + subfield.marshal();
str = str + "\x1f" + subfield.marshal();
});
return str;
};
Expand All @@ -98,4 +98,4 @@ DataField.prototype.toString = function () {
return this.marshal();
};

module.exports = DataField;
export default DataField;
Loading