diff --git a/README.md b/README.md index c3ee68f..8a00afe 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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(); ``` @@ -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 @@ -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 @@ -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 @@ -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) { @@ -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 @@ -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 @@ -183,11 +183,11 @@ 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 (``) is not included in the output. -* `root`: default is `true`. If `false`, the root `` 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 (``) is not included in the output. +- `root`: default is `true`. If `false`, the root `` element is not included in the output. ##### MijTransformer @@ -195,7 +195,5 @@ Outputs [MARC-in-JSON](https://rossfsinger.com/blog/2010/09/a-proposal-to-serial 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. diff --git a/index.js b/index.js index 29a3317..9f74365 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib/marc4js'); \ No newline at end of file +export { default } from "./lib/marc4js.js"; diff --git a/lib/marc/control_field.js b/lib/marc/control_field.js index f4644c6..fe764a3 100644 --- a/lib/marc/control_field.js +++ b/lib/marc/control_field.js @@ -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); @@ -16,8 +14,8 @@ Object.defineProperties(ControlField.prototype, { }, set: function (val) { this._data = val; - } - } + }, + }, }); ControlField.prototype.marshal = function () { @@ -28,4 +26,4 @@ ControlField.prototype.toString = function () { return this.marshal(); }; -module.exports = ControlField; +export default ControlField; diff --git a/lib/marc/data_field.js b/lib/marc/data_field.js index f3afc01..23b0a57 100644 --- a/lib/marc/data_field.js +++ b/lib/marc/data_field.js @@ -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 { @@ -26,7 +24,7 @@ Object.defineProperties(DataField.prototype, { }, set: function (val) { this._indicator1 = val; - } + }, }, indicator2: { get: function () { @@ -34,7 +32,7 @@ Object.defineProperties(DataField.prototype, { }, set: function (val) { this._indicator2 = val; - } + }, }, subfields: { get: function () { @@ -42,28 +40,30 @@ Object.defineProperties(DataField.prototype, { }, 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); @@ -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; }; @@ -98,4 +98,4 @@ DataField.prototype.toString = function () { return this.marshal(); }; -module.exports = DataField; +export default DataField; diff --git a/lib/marc/leader.js b/lib/marc/leader.js index f5bd439..4bc2e87 100644 --- a/lib/marc/leader.js +++ b/lib/marc/leader.js @@ -1,9 +1,7 @@ -'use strict'; - -var Util = require("../util"); +import * as Util from "../util.js"; var Leader = function (str) { - if (typeof str !== 'undefined') { + if (typeof str !== "undefined") { this.unmarshal(str); } }; @@ -16,7 +14,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._recordLength = val; - } + }, }, recordStatus: { get: function () { @@ -24,7 +22,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._recordStatus = val; - } + }, }, typeOfRecord: { get: function () { @@ -32,7 +30,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._typeOfRecord = val; - } + }, }, implDefined1: { get: function () { @@ -40,7 +38,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._implDefined1 = val; - } + }, }, implDefined2: { get: function () { @@ -48,7 +46,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._implDefined2 = val; - } + }, }, charCodingScheme: { get: function () { @@ -56,7 +54,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._charCodingScheme = val; - } + }, }, indicatorCount: { get: function () { @@ -64,7 +62,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._indicatorCount = val; - } + }, }, subfieldCodeLength: { get: function () { @@ -72,7 +70,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._subfieldCodeLength = val; - } + }, }, baseAddressOfData: { get: function () { @@ -80,7 +78,7 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._baseAddressOfData = val; - } + }, }, entryMap: { get: function () { @@ -88,8 +86,8 @@ Object.defineProperties(Leader.prototype, { }, set: function (val) { this._entryMap = val; - } - } + }, + }, }); Leader.prototype.unmarshal = function (ldr) { @@ -130,15 +128,22 @@ Leader.prototype.unmarshal = function (ldr) { }; Leader.prototype.marshal = function () { - return Util.formatInteger(this.recordLength, 5) + - this.recordStatus + this.typeOfRecord + this.implDefined1 + - this.charCodingScheme + this.indicatorCount + this.subfieldCodeLength + - Util.formatInteger(this.baseAddressOfData, 5) + this.implDefined2 + this.entryMap; + return ( + Util.formatInteger(this.recordLength, 5) + + this.recordStatus + + this.typeOfRecord + + this.implDefined1 + + this.charCodingScheme + + this.indicatorCount + + this.subfieldCodeLength + + Util.formatInteger(this.baseAddressOfData, 5) + + this.implDefined2 + + this.entryMap + ); }; Leader.prototype.toString = function () { return this.marshal(); }; - -module.exports = Leader; +export default Leader; diff --git a/lib/marc/record.js b/lib/marc/record.js index 2fdfe04..afd05fa 100644 --- a/lib/marc/record.js +++ b/lib/marc/record.js @@ -1,6 +1,4 @@ -'use strict'; - -var Util = require('../util.js'); +import * as Util from "../util.js"; var Record = function () { this._controlFields = []; @@ -15,7 +13,7 @@ Object.defineProperties(Record.prototype, { }, set: function (val) { this._leader = val; - } + }, }, controlFields: { get: function () { @@ -23,7 +21,7 @@ Object.defineProperties(Record.prototype, { }, set: function (val) { this._controlFields = val; - } + }, }, dataFields: { get: function () { @@ -31,13 +29,13 @@ Object.defineProperties(Record.prototype, { }, set: function (val) { this._dataFields = val; - } + }, }, variableFields: { get: function () { return this.controlFields.concat(this.dataFields); - } - } + }, + }, }); Record.prototype.addVariableField = function (field) { @@ -60,8 +58,8 @@ Record.prototype.findDataField = function (tag) { Record.prototype.marshal = function () { var buffers = []; - var encoding = 'utf8'; // fot now, marc4js only supports utf-8 output - this.leader.charCodingScheme = 'a'; + var encoding = "utf8"; // fot now, marc4js only supports utf-8 output + this.leader.charCodingScheme = "a"; buffers.push(new Buffer(this.leader.marshal(), encoding)); @@ -72,7 +70,7 @@ Record.prototype.marshal = function () { // write directory this.variableFields.forEach(function (field) { var chunk = field.marshal(); - chunk = chunk + '\x1e'; + chunk = chunk + "\x1e"; var len = Buffer.byteLength(chunk, encoding); fields.push(new Buffer(chunk, encoding)); @@ -83,10 +81,10 @@ Record.prototype.marshal = function () { }); buffers = buffers.concat(directory); - buffers = buffers.concat(new Buffer('\x1e')); + buffers = buffers.concat(new Buffer("\x1e")); buffers = buffers.concat(fields); - buffers.push(new Buffer('\x1d')); + buffers.push(new Buffer("\x1d")); return Buffer.concat(buffers); }; -module.exports = Record; +export default Record; diff --git a/lib/marc/subfield.js b/lib/marc/subfield.js index 2434cf7..0ecf7a9 100644 --- a/lib/marc/subfield.js +++ b/lib/marc/subfield.js @@ -1,8 +1,6 @@ -'use strict'; - var Subfield = function (code, data) { - if (typeof code !== 'undefined') this._code = code; - if (typeof data !== 'undefined') this._data = data; + if (typeof code !== "undefined") this._code = code; + if (typeof data !== "undefined") this._data = data; }; // Define getters and setters @@ -13,7 +11,7 @@ Object.defineProperties(Subfield.prototype, { }, set: function (val) { this._code = val; - } + }, }, data: { get: function () { @@ -21,8 +19,8 @@ Object.defineProperties(Subfield.prototype, { }, set: function (val) { this._data = val; - } - } + }, + }, }); Subfield.prototype.find = function (pattern) { @@ -35,6 +33,6 @@ Subfield.prototype.toString = function () { Subfield.prototype.marshal = function () { return this._code + this._data; -} +}; -module.exports = Subfield; +export default Subfield; diff --git a/lib/marc/variable_field.js b/lib/marc/variable_field.js index a73804e..9b24899 100644 --- a/lib/marc/variable_field.js +++ b/lib/marc/variable_field.js @@ -1,7 +1,4 @@ -'use strict'; - -var VariableField = function () { -}; +var VariableField = function () {}; // Define getters and setters Object.defineProperties(VariableField.prototype, { @@ -11,8 +8,8 @@ Object.defineProperties(VariableField.prototype, { }, set: function (val) { this._tag = val; - } - } + }, + }, }); -module.exports = VariableField; +export default VariableField; diff --git a/lib/marc4js.js b/lib/marc4js.js index 23db7fa..e54fc7c 100644 --- a/lib/marc4js.js +++ b/lib/marc4js.js @@ -1,15 +1,23 @@ -'use strict'; +import Record from "./marc/record.js"; +import DataField from "./marc/data_field.js"; +import ControlField from "./marc/control_field.js"; +import Leader from "./marc/leader.js"; +import Subfield from "./marc/subfield.js"; +import MarcError from "./marc_error.js"; +import VariableField from "./marc/variable_field.js"; +import parse from "./parse.js"; +import transform from "./transform.js"; -exports.marc = { - Record: require('./marc/record'), - DataField: require('./marc/data_field'), - ControlField: require('./marc/control_field'), - Leader: require('./marc/leader'), - Subfield: require('./marc/subfield'), - VariableField: require('./marc/variable_field') +export default { + marc: { + Record, + DataField, + ControlField, + Leader, + Subfield, + MarcError, + VariableField, + parse, + transform, + }, }; - -exports.parse = require('./parse'); -exports.transform = require('./transform'); -exports.MarcError = require('./marc_error'); - diff --git a/lib/marc_error.js b/lib/marc_error.js index 02bf28e..898af31 100644 --- a/lib/marc_error.js +++ b/lib/marc_error.js @@ -1,5 +1,3 @@ -'use strict'; - var MarcError = function (message) { Error.captureStackTrace(this); this.message = message; @@ -8,4 +6,4 @@ var MarcError = function (message) { MarcError.prototype = Object.create(Error.prototype); -module.exports = MarcError; +export default MarcError; diff --git a/lib/parse.js b/lib/parse.js index db6265b..00789b9 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,24 +1,34 @@ -'use strict'; +import iso2709 from "./parse/iso2709_parser.js"; +import marc from "./parse/iso2709_parser.js"; +import text from "./parse/text_parser.js"; +import mrk from "./parse/mrk_parser.js"; +import marcxml from "./parse/marcxml_parser.js"; +import xml from "./parse/marcxml_parser.js"; +import json from "./parse/mij_parser.js"; +import mij from "./parse/mij_parser.js"; -module.exports = function() { +export default function parse() { var callback, called, records, data, options, parser; if (arguments.length === 3) { - data = (typeof arguments[0] === 'undefined' || arguments[0] === null ) ? "" : arguments[0]; + data = + typeof arguments[0] === "undefined" || arguments[0] === null + ? "" + : arguments[0]; options = arguments[1]; callback = arguments[2]; } else if (arguments.length === 2) { - if (typeof arguments[0] === 'string') { + if (typeof arguments[0] === "string") { data = arguments[0]; } else { options = arguments[0]; } - if (typeof arguments[1] === 'function') { + if (typeof arguments[1] === "function") { callback = arguments[1]; } else { options = arguments[1]; } } else if (arguments.length === 1) { - if (typeof arguments[0] === 'function') { + if (typeof arguments[0] === "function") { callback = arguments[0]; } else { options = arguments[0]; @@ -29,21 +39,21 @@ module.exports = function() { } var parsers = { - iso2709: './parse/iso2709_parser', - marc: './parse/iso2709_parser', - text: './parse/text_parser', - mrk: './parse/mrk_parser', - marcxml: './parse/marcxml_parser', - xml: './parse/marcxml_parser', - json: './parse/mij_parser', - mij: './parse/mij_parser' + iso2709, + marc, + text, + mrk, + marcxml, + xml, + json, + mij, }; - var format = options.format || options.fromFormat || 'iso2709'; - var Parser = require(parsers[format]); + var format = options.format || options.fromFormat || "iso2709"; + var Parser = parsers[format]; parser = new Parser(options); - if (typeof data !== 'undefined') { - process.nextTick(function() { + if (typeof data !== "undefined") { + process.nextTick(function () { parser.write(data); return parser.end(); }); @@ -52,21 +62,24 @@ module.exports = function() { called = false; records = []; - var handleData = function(record) { + var handleData = function (record) { records.push(record); }; - parser.on('data', handleData); - parser.once('error', function(err) { + parser.on("data", handleData); + parser.once("error", function (err) { called = true; - parser.removeListener('data', handleData); + parser.removeListener("data", handleData); return callback(err); }); - parser.once('end', function() { - if (!called) { - parser.removeListener('data', handleData); - return callback(null, records); - } - }.bind(parser)); + parser.once( + "end", + function () { + if (!called) { + parser.removeListener("data", handleData); + return callback(null, records); + } + }.bind(parser) + ); } return parser; -}; +} diff --git a/lib/parse/iso2709_parser.js b/lib/parse/iso2709_parser.js index f9e72d2..6e41e49 100644 --- a/lib/parse/iso2709_parser.js +++ b/lib/parse/iso2709_parser.js @@ -1,27 +1,25 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var MarcError = require('../marc_error'); -var Util = require('../util'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Record = require('../marc/record'); -var Leader = require('../marc/leader'); -var Subfield = require('../marc/subfield'); +import { Transform } from "stream"; +import util from "util"; +import MarcError from "../marc_error.js"; +import * as Util from "../util.js"; +import ControlField from "../marc/control_field.js"; +import DataField from "../marc/data_field.js"; +import Record from "../marc/record.js"; +import Leader from "../marc/leader.js"; +import Subfield from "../marc/subfield.js"; var Iso2709Parser = function (opts) { opts = opts || {}; opts.objectMode = true; // this has to be true. Emit only Record objects this.forceMARC8 = opts.forceMARC8; - if (typeof this.forceMARC8 === 'undefined') { + if (typeof this.forceMARC8 === "undefined") { this.forceMARC8 = false; } this.marc8converter = opts.marc8converter; // default encoding is utf8. if the marc file has marc8, // please use 'binary' encoding - this.encoding = opts.encoding || 'utf8'; + this.encoding = opts.encoding || "utf8"; Transform.call(this, opts); @@ -31,8 +29,8 @@ var Iso2709Parser = function (opts) { util.inherits(Iso2709Parser, Transform); Iso2709Parser.prototype._transform = function (chunk, encoding, callback) { - if (typeof chunk == 'string' || chunk instanceof String) { - chunk = new Buffer(chunk, 'binary'); + if (typeof chunk == "string" || chunk instanceof String) { + chunk = new Buffer(chunk, "binary"); } else { encoding = undefined; } @@ -77,11 +75,11 @@ Iso2709Parser.prototype._transform = function (chunk, encoding, callback) { return callback(); } catch (_error) { err = _error; - return this.emit('error', err); + return this.emit("error", err); } }; -Iso2709Parser.prototype._parse = function(data, encoding) { +Iso2709Parser.prototype._parse = function (data, encoding) { var record = new Record(); var leader = new Leader(); leader.unmarshal(data.toString(this.encoding, 0, 24)); @@ -90,7 +88,7 @@ Iso2709Parser.prototype._parse = function(data, encoding) { var g0, g1; var directoryLength = leader.baseAddressOfData - (24 + 1); - if ((directoryLength % 12) !== 0) { + if (directoryLength % 12 !== 0) { throw new MarcError("invalid directory"); } var size = directoryLength / 12; @@ -98,7 +96,7 @@ Iso2709Parser.prototype._parse = function(data, encoding) { var pos = directoryLength + 25; for (var i = 0; i < size; i++) { var offset = 24 + i * 12; - var tag = data.toString(this.encoding, offset, offset+3); + var tag = data.toString(this.encoding, offset, offset + 3); var field; if (Util.isControlField(tag)) { @@ -108,7 +106,10 @@ Iso2709Parser.prototype._parse = function(data, encoding) { chars.push(data[pos++]); } pos++; - field.data = this._convert(record.leader.charCodingScheme, chars, {G0: g0, G1: g1}); + field.data = this._convert(record.leader.charCodingScheme, chars, { + G0: g0, + G1: g1, + }); } else { // field = this._parseDataField(); field = new DataField(); @@ -125,7 +126,11 @@ Iso2709Parser.prototype._parse = function(data, encoding) { while (data[pos] !== 0x1f && data[pos] !== 0x1e) { chars.push(data[pos++]); } - subfield.data = this._convert(record.leader.charCodingScheme, chars, {G0: g0, G1: g1}); + subfield.data = this._convert( + record.leader.charCodingScheme, + chars, + { G0: g0, G1: g1 } + ); field.addSubfield(subfield); } pos++; @@ -134,7 +139,7 @@ Iso2709Parser.prototype._parse = function(data, encoding) { field.tag = tag; record.addVariableField(field); - if (field.tag === '066') { + if (field.tag === "066") { var g0g1 = this._charset(field); g0 = g0g1[0]; g1 = g0g1[1]; @@ -143,24 +148,28 @@ Iso2709Parser.prototype._parse = function(data, encoding) { return record; }; -Iso2709Parser.prototype._convert = function(charCodingScheme, data, opts) { +Iso2709Parser.prototype._convert = function (charCodingScheme, data, opts) { var value; - if (charCodingScheme === 'a') { - value = new Buffer(data).toString('utf8'); - } else if (charCodingScheme === ' ') { + if (charCodingScheme === "a") { + value = new Buffer(data).toString("utf8"); + } else if (charCodingScheme === " ") { if (this.marc8converter) { value = this.marc8converter(data, opts); } else { value = new Buffer(data).toString(this.encoding); } } else { - throw new MarcError("Unexpected character coding scheme. " + - "It has to be 'a' or ' ' but value is '" + charCodingScheme + "'"); + throw new MarcError( + "Unexpected character coding scheme. " + + "It has to be 'a' or ' ' but value is '" + + charCodingScheme + + "'" + ); } return value; }; -Iso2709Parser.prototype._charset = function(f066) { +Iso2709Parser.prototype._charset = function (f066) { var subfields = f066.subfields; var g0, g1; var G0_SET = [0x28, 0x2c, 0x24]; // ['(', ',', '$']; @@ -168,13 +177,13 @@ Iso2709Parser.prototype._charset = function(f066) { for (var i = 0; i < subfields.length; i++) { var subfield = subfields[i]; switch (subfield.code) { - case 'a': + case "a": g0 = subfield.data.charCodeAt(1); break; - case 'b': + case "b": g1 = subfield.data.charCodeAt(1); break; - case 'c': + case "c": var c1 = subfield.data.charCodeAt(0); if (G0_SET.indexOf(c1) >= 0) { g0 = subfield.data.charCodeAt(1); @@ -186,6 +195,6 @@ Iso2709Parser.prototype._charset = function(f066) { } } return [g0, g1]; -} +}; -module.exports = Iso2709Parser; +export default Iso2709Parser; diff --git a/lib/parse/marcxml_parser.js b/lib/parse/marcxml_parser.js index ebdb123..271fbfb 100644 --- a/lib/parse/marcxml_parser.js +++ b/lib/parse/marcxml_parser.js @@ -1,15 +1,13 @@ -'use strict'; +import sax from "sax"; +import util from "util"; +import { Transform } from "stream"; +import Record from "../marc/record.js"; +import ControlField from "../marc/control_field.js"; +import DataField from "../marc/data_field.js"; +import Subfield from "../marc/subfield.js"; +import Leader from "../marc/leader.js"; -var sax = require('sax'); -var util = require('util'); -var Transform = require('stream').Transform; -var Record = require('../marc/record'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Subfield = require('../marc/subfield'); -var Leader = require('../marc/leader'); - -function MarcxmlParser (opts) { +function MarcxmlParser(opts) { if (!(this instanceof MarcxmlParser)) return new MarcxmlParser(opts); opts = opts || {}; opts.objectMode = true; // this has to be true. Emit only Record objects @@ -22,33 +20,35 @@ function MarcxmlParser (opts) { this.resume_saxerror = opts.resume_saxerror || false; // See https://github.com/isaacs/sax-js for more info - this.stream = sax.createStream(strict /* strict mode - no by default */, {lowercase: true, xmlns: true }); - this.stream.on('error', this.handleSaxError.bind(this)); + this.stream = sax.createStream(strict /* strict mode - no by default */, { + lowercase: true, + xmlns: true, + }); + this.stream.on("error", this.handleSaxError.bind(this)); //this.stream.on('processinginstruction', this.handleProcessingInstruction.bind(this)); - this.stream.on('opentag', this.handleOpenTag.bind(this)); - this.stream.on('closetag',this.handleCloseTag.bind(this)); - this.stream.on('text', this.handleText.bind(this)); - this.stream.on('cdata', this.handleText.bind(this)); - this.stream.once('end', this.handleEnd.bind(this)); + this.stream.on("opentag", this.handleOpenTag.bind(this)); + this.stream.on("closetag", this.handleCloseTag.bind(this)); + this.stream.on("text", this.handleText.bind(this)); + this.stream.on("cdata", this.handleText.bind(this)); + this.stream.once("end", this.handleEnd.bind(this)); } util.inherits(MarcxmlParser, Transform); -MarcxmlParser.prototype.init = function (){ +MarcxmlParser.prototype.init = function () { this.stack = []; }; - -MarcxmlParser.prototype.handleEnd = function (){ - this.stream.removeListener('error', this.handleSaxError.bind(this)); - this.stream.removeListener('opentag', this.handleOpenTag.bind(this)); - this.stream.removeListener('closetag',this.handleCloseTag.bind(this)); - this.stream.removeListener('text', this.handleText.bind(this)); - this.stream.removeListener('cdata', this.handleText.bind(this)); +MarcxmlParser.prototype.handleEnd = function () { + this.stream.removeListener("error", this.handleSaxError.bind(this)); + this.stream.removeListener("opentag", this.handleOpenTag.bind(this)); + this.stream.removeListener("closetag", this.handleCloseTag.bind(this)); + this.stream.removeListener("text", this.handleText.bind(this)); + this.stream.removeListener("cdata", this.handleText.bind(this)); this.push(null); }; MarcxmlParser.prototype.handleSaxError = function (e) { - this.emit('error', e); + this.emit("error", e); if (this.resume_saxerror) { if (this.stream._parser) { this.stream._parser.error = null; @@ -57,20 +57,20 @@ MarcxmlParser.prototype.handleSaxError = function (e) { } }; -MarcxmlParser.prototype.handleError = function (e){ - this.emit('error', e); +MarcxmlParser.prototype.handleError = function (e) { + this.emit("error", e); }; -MarcxmlParser.prototype.handleOpenTag = function (node){ +MarcxmlParser.prototype.handleOpenTag = function (node) { var obj; switch (node.local) { - case 'record': + case "record": obj = new Record(); break; - case 'leader': + case "leader": obj = new Leader(); break; - case 'controlfield': + case "controlfield": obj = new ControlField(); if (node.attributes.tag) { obj.tag = node.attributes.tag.value; @@ -78,7 +78,7 @@ MarcxmlParser.prototype.handleOpenTag = function (node){ // TODO: throw an error } break; - case 'datafield': + case "datafield": obj = new DataField(); if (node.attributes.tag) { obj.tag = node.attributes.tag.value; @@ -96,7 +96,7 @@ MarcxmlParser.prototype.handleOpenTag = function (node){ // TODO: throw an error } break; - case 'subfield': + case "subfield": obj = new Subfield(); if (node.attributes.code) { obj.code = node.attributes.code.value; @@ -107,10 +107,10 @@ MarcxmlParser.prototype.handleOpenTag = function (node){ default: break; } - if (typeof obj !== 'undefined') this.stack.push(obj); + if (typeof obj !== "undefined") this.stack.push(obj); }; -MarcxmlParser.prototype.handleCloseTag = function (el){ +MarcxmlParser.prototype.handleCloseTag = function (el) { var obj = this.stack.pop(); if (obj instanceof Subfield) { var field = this.stack.pop(); @@ -129,7 +129,7 @@ MarcxmlParser.prototype.handleCloseTag = function (el){ } }; -MarcxmlParser.prototype.handleText = function (text){ +MarcxmlParser.prototype.handleText = function (text) { var obj = this.stack.pop(); if (obj instanceof Subfield || obj instanceof ControlField) { obj.data = text; @@ -150,4 +150,4 @@ MarcxmlParser.prototype._transform = function (data, encoding, done) { } }; -module.exports = MarcxmlParser; \ No newline at end of file +export default MarcxmlParser; diff --git a/lib/parse/mij_parser.js b/lib/parse/mij_parser.js index 14a5317..917e5a1 100644 --- a/lib/parse/mij_parser.js +++ b/lib/parse/mij_parser.js @@ -1,15 +1,13 @@ -'use strict'; +import clarinet from "clarinet"; +import util from "util"; +import { Transform } from "stream"; +import Record from "../marc/record.js"; +import ControlField from "../marc/control_field.js"; +import DataField from "../marc/data_field.js"; +import Subfield from "../marc/subfield.js"; +import Leader from "../marc/leader.js"; -var clarinet = require('clarinet'); -var util = require('util'); -var Transform = require('stream').Transform; -var Record = require('../marc/record'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Subfield = require('../marc/subfield'); -var Leader = require('../marc/leader'); - -function MijParser (opts) { +function MijParser(opts) { if (!(this instanceof MijParser)) return new MijParser(opts); opts = opts || {}; opts.objectMode = true; // this has to be true. Emit only Record objects @@ -20,34 +18,36 @@ function MijParser (opts) { // See https://github.com/dscape/clarinet for more info this.stream = clarinet.createStream({}); - this.stream.on('error', this.handleSaxError.bind(this)); - this.stream.on('openobject', this.handleOpenObject.bind(this)); - this.stream.on('closeobject',this.handleCloseObject.bind(this)); - this.stream.on('value', this.handleValue.bind(this)); - this.stream.on('key', this.handleKey.bind(this)); - this.stream.once('end', this.handleEnd.bind(this)); + this.stream.on("error", this.handleSaxError.bind(this)); + this.stream.on("openobject", this.handleOpenObject.bind(this)); + this.stream.on("closeobject", this.handleCloseObject.bind(this)); + this.stream.on("value", this.handleValue.bind(this)); + this.stream.on("key", this.handleKey.bind(this)); + this.stream.once("end", this.handleEnd.bind(this)); } util.inherits(MijParser, Transform); -MijParser.prototype.init = function (){ +MijParser.prototype.init = function () { this.stack = []; this.keyStack = []; - this.previous = ''; + this.previous = ""; this.arrayStack = []; }; - -MijParser.prototype.handleEnd = function (){ - this.stream.removeListener('error', this.handleSaxError.bind(this)); - this.stream.removeListener('openobject', this.handleOpenObject.bind(this)); - this.stream.removeListener('closeobject',this.handleCloseObject.bind(this)); - this.stream.removeListener('value', this.handleValue.bind(this)); - this.stream.removeListener('key', this.handleKey.bind(this)); +MijParser.prototype.handleEnd = function () { + this.stream.removeListener("error", this.handleSaxError.bind(this)); + this.stream.removeListener("openobject", this.handleOpenObject.bind(this)); + this.stream.removeListener( + "closeobject", + this.handleCloseObject.bind(this) + ); + this.stream.removeListener("value", this.handleValue.bind(this)); + this.stream.removeListener("key", this.handleKey.bind(this)); this.push(null); }; MijParser.prototype.handleSaxError = function (e) { - this.emit('error', e); + this.emit("error", e); if (this.options.resume_saxerror) { if (this.stream._parser) { this.stream._parser.error = null; @@ -56,29 +56,31 @@ MijParser.prototype.handleSaxError = function (e) { } }; -MijParser.prototype.handleError = function (e){ - this.emit('error', e); +MijParser.prototype.handleError = function (e) { + this.emit("error", e); }; -MijParser.prototype.handleOpenObject = function (key){ +MijParser.prototype.handleOpenObject = function (key) { this.previous = key; this.keyStack.push(key); var obj = null; - if (/\d{3}/.test(key)) { // field + if (/\d{3}/.test(key)) { + // field obj = parseInt(key) < 10 ? new ControlField() : new DataField(); obj.tag = key; - } else if (key.length == 1) { // subfield + } else if (key.length == 1) { + // subfield obj = new Subfield(); obj.code = key; - } else if (key == 'leader' || key == 'fields') { + } else if (key == "leader" || key == "fields") { obj = new Record(); } if (obj != null) this.stack.push(obj); }; -MijParser.prototype.handleCloseObject = function (){ +MijParser.prototype.handleCloseObject = function () { var key = this.keyStack.pop(); - if (key == 'subfields' || key == 'fields') { + if (key == "subfields" || key == "fields") { return; } var obj = this.stack.pop(); @@ -91,19 +93,22 @@ MijParser.prototype.handleCloseObject = function (){ record.addVariableField(obj); this.stack.push(record); } else if (obj instanceof Record) { - if (this.arrayStack.length == 0 || this.arrayStack[this.arrayStack.length-1] == '') { - this.previous = ''; + if ( + this.arrayStack.length == 0 || + this.arrayStack[this.arrayStack.length - 1] == "" + ) { + this.previous = ""; this.push(obj); } } }; -MijParser.prototype.handleValue = function(value) { - if (this.previous == 'ind1') { +MijParser.prototype.handleValue = function (value) { + if (this.previous == "ind1") { var field = this.stack.pop(); field.indicator1 = value; this.stack.push(field); - } else if (this.previous == 'ind2') { + } else if (this.previous == "ind2") { var field = this.stack.pop(); field.indicator2 = value; this.stack.push(field); @@ -115,7 +120,7 @@ MijParser.prototype.handleValue = function(value) { var field = this.stack.pop(); field.data = value; this.stack.push(field); - } else if (this.previous == 'leader') { + } else if (this.previous == "leader") { var leader = new Leader(value); var record = this.stack.pop(); record.leader = leader; @@ -123,7 +128,7 @@ MijParser.prototype.handleValue = function(value) { } }; -MijParser.prototype.handleKey = function (key){ +MijParser.prototype.handleKey = function (key) { this.previous = key; }; @@ -138,4 +143,4 @@ MijParser.prototype._transform = function (data, encoding, done) { } }; -module.exports = MijParser; \ No newline at end of file +export default MijParser; diff --git a/lib/parse/mrk_parser.js b/lib/parse/mrk_parser.js index 9511480..2ebaf61 100644 --- a/lib/parse/mrk_parser.js +++ b/lib/parse/mrk_parser.js @@ -1,13 +1,11 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var Util = require('../util'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Subfield = require('../marc/subfield'); -var Record = require('../marc/record'); -var Leader = require('../marc/leader'); +import { Transform } from "stream"; +import util from "util"; +import * as Util from "../util.js"; +import ControlField from "../marc/control_field.js"; +import DataField from "../marc/data_field.js"; +import Subfield from "../marc/subfield.js"; +import Record from "../marc/record.js"; +import Leader from "../marc/leader.js"; var MrkParser = function (opts) { opts = opts || {}; @@ -15,7 +13,7 @@ var MrkParser = function (opts) { Transform.call(this, opts); - this.spaceReplace = opts.spaceReplace || '\\'; + this.spaceReplace = opts.spaceReplace || "\\"; this.lines = []; // position of cursor @@ -26,19 +24,19 @@ var MrkParser = function (opts) { util.inherits(MrkParser, Transform); MrkParser.prototype._transform = function (chunk, encoding, callback) { - if (typeof chunk == 'string' || chunk instanceof String) { + if (typeof chunk == "string" || chunk instanceof String) { chunk = new Buffer(chunk, encoding); } var lines, i; - lines = (chunk.toString()).split('\n'); - for(i = 0; i < lines.length; i++) { + lines = chunk.toString().split("\n"); + for (i = 0; i < lines.length; i++) { var line = lines[i].trim(); this.row += 1; if (!line) { continue; } - if (line.substr(1, 3) === 'LDR') { + if (line.substr(1, 3) === "LDR") { if (this.lines.length > 0) { var record; try { @@ -55,7 +53,7 @@ MrkParser.prototype._transform = function (chunk, encoding, callback) { return callback(); }; -MrkParser.prototype._flush = function(callback) { +MrkParser.prototype._flush = function (callback) { if (this.lines.length > 0) { var record; try { @@ -72,30 +70,38 @@ MrkParser.prototype._flush = function(callback) { MrkParser.prototype._parse = function (lines) { var record = new Record(); var spaceReplace = this.spaceReplace; - lines.forEach(function(line) { + lines.forEach(function (line) { var tag = line.substr(1, 3); - if (tag === 'LDR') { + if (tag === "LDR") { record.leader = new Leader(line.substr(6)); } else { - var re = new RegExp(spaceReplace.replace('\\', '\\\\'), 'g'); + var re = new RegExp(spaceReplace.replace("\\", "\\\\"), "g"); var field; if (Util.isControlField(tag)) { field = new ControlField(); - field.data = line.substr(6).replace(re, ' '); + field.data = line.substr(6).replace(re, " "); } else { field = new DataField(); var data = line.substr(6); - field.indicator1 = data.charAt(0).replace(re, ' '); - field.indicator2 = data.charAt(1).replace(re, ' '); + field.indicator1 = data.charAt(0).replace(re, " "); + field.indicator2 = data.charAt(1).replace(re, " "); if (isNaN(field.indicator1)) { - throw new MarcError("Wrong indicator format. It has to be a number or a space"); + throw new MarcError( + "Wrong indicator format. It has to be a number or a space" + ); } if (isNaN(field.indicator2)) { - throw new MarcError("Wrong indicator format. It has to be a number or a space"); + throw new MarcError( + "Wrong indicator format. It has to be a number or a space" + ); } - data.substr(3).split('$').forEach(function(sf) { - field.addSubfield(new Subfield(sf.charAt(0), sf.substr(1))); - }); + data.substr(3) + .split("$") + .forEach(function (sf) { + field.addSubfield( + new Subfield(sf.charAt(0), sf.substr(1)) + ); + }); } field.tag = tag; record.addVariableField(field); @@ -104,5 +110,4 @@ MrkParser.prototype._parse = function (lines) { return record; }; - -module.exports = MrkParser; +export default MrkParser; diff --git a/lib/parse/text_parser.js b/lib/parse/text_parser.js index c43bc75..8fe314c 100644 --- a/lib/parse/text_parser.js +++ b/lib/parse/text_parser.js @@ -1,13 +1,11 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var Util = require('../util'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Subfield = require('../marc/subfield'); -var Record = require('../marc/record'); -var Leader = require('../marc/leader'); +import { Transform } from "stream"; +import util from "util"; +import * as Util from "../util.js"; +import ControlField from "../marc/control_field.js"; +import DataField from "../marc/data_field.js"; +import Subfield from "../marc/subfield.js"; +import Record from "../marc/record.js"; +import Leader from "../marc/leader.js"; var TextParser = function (opts) { opts = opts || {}; @@ -25,19 +23,19 @@ var TextParser = function (opts) { util.inherits(TextParser, Transform); TextParser.prototype._transform = function (chunk, encoding, callback) { - if (typeof chunk == 'string' || chunk instanceof String) { + if (typeof chunk == "string" || chunk instanceof String) { chunk = new Buffer(chunk, encoding); } var lines, i; - lines = (chunk.toString()).split('\n'); - for(i = 0; i < lines.length; i++) { + lines = chunk.toString().split("\n"); + for (i = 0; i < lines.length; i++) { var line = lines[i].trim(); this.row += 1; if (!line) { continue; } - if (line.substr(0, 3) === 'LDR') { + if (line.substr(0, 3) === "LDR") { if (this.lines.length > 0) { var record; try { @@ -54,7 +52,7 @@ TextParser.prototype._transform = function (chunk, encoding, callback) { return callback(); }; -TextParser.prototype._flush = function(callback) { +TextParser.prototype._flush = function (callback) { if (this.lines.length > 0) { var record; try { @@ -70,9 +68,9 @@ TextParser.prototype._flush = function(callback) { TextParser.prototype._parse = function (lines) { var record = new Record(); - lines.forEach(function(line) { + lines.forEach(function (line) { var tag = line.substr(0, 3); - if (tag === 'LDR') { + if (tag === "LDR") { record.leader = new Leader(line.substr(7)); } else { var field; @@ -85,14 +83,22 @@ TextParser.prototype._parse = function (lines) { field.indicator1 = data.charAt(0); field.indicator2 = data.charAt(1); if (isNaN(field.indicator1)) { - throw new MarcError("Wrong indicator format. It has to be a number or a space"); + throw new MarcError( + "Wrong indicator format. It has to be a number or a space" + ); } if (isNaN(field.indicator2)) { - throw new MarcError("Wrong indicator format. It has to be a number or a space"); + throw new MarcError( + "Wrong indicator format. It has to be a number or a space" + ); } - data.substr(4).split('$').forEach(function(sf) { - field.addSubfield(new Subfield(sf.charAt(0), sf.substr(1))); - }); + data.substr(4) + .split("$") + .forEach(function (sf) { + field.addSubfield( + new Subfield(sf.charAt(0), sf.substr(1)) + ); + }); } field.tag = tag; record.addVariableField(field); @@ -101,5 +107,4 @@ TextParser.prototype._parse = function (lines) { return record; }; - -module.exports = TextParser; +export default TextParser; diff --git a/lib/transform.js b/lib/transform.js index 4574e07..217d44c 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -1,8 +1,15 @@ -'use strict'; +import Record from "./marc/record.js"; -var Record = require('./marc/record'); +import iso2709 from "./transform/iso2709_transformer.js"; +import marc from "./transform/iso2709_transformer.js"; +import text from "./transform/text_transformer.js"; +import mrk from "./transform/mrk_transformer.js"; +import xml from "./transform/marcxml_transformer.js"; +import marcxml from "./transform/marcxml_transformer.js"; +import json from "./transform/mij_transformer.js"; +import mij from "./transform/mij_transformer.js"; -module.exports = function() { +export default function transform() { var callback, chunks, data, options, transformer; if (arguments.length === 3) { if (Array.isArray(arguments[0])) { @@ -20,13 +27,13 @@ module.exports = function() { } else { options = arguments[0]; } - if (typeof arguments[1] === 'function') { + if (typeof arguments[1] === "function") { callback = arguments[1]; } else { options = arguments[1]; } } else if (arguments.length === 1) { - if (typeof arguments[0] === 'function') { + if (typeof arguments[0] === "function") { callback = arguments[0]; } else if (Array.isArray(arguments[0])) { data = arguments[0]; @@ -38,21 +45,21 @@ module.exports = function() { options = {}; } var transformers = { - iso2709: './transform/iso2709_transformer', - marc: './transform/iso2709_transformer', - text: './transform/text_transformer', - mrk: './transform/mrk_transformer', - xml: './transform/marcxml_transformer', - marcxml: './transform/marcxml_transformer', - json: './transform/mij_transformer', - mij: './transform/mij_transformer' + iso2709, + marc, + text, + mrk, + xml, + marcxml, + json, + mij, }; - var format = options.format || options.toFormat || 'iso2709'; - var Transformer = require(transformers[format]); + var format = options.format || options.toFormat || "iso2709"; + var Transformer = transformers[format]; transformer = new Transformer(options); if (data) { - process.nextTick(function() { + process.nextTick(function () { var d, _i, _len; for (_i = 0, _len = data.length; _i < _len; _i++) { d = data[_i]; @@ -63,23 +70,23 @@ module.exports = function() { } if (callback) { chunks = []; - var handleReadable = function() { + var handleReadable = function () { var chunk, _results; _results = []; - while (chunk = transformer.read()) { + while ((chunk = transformer.read())) { _results.push(chunks.push(chunk)); } return _results; }; - transformer.on('readable', handleReadable); - transformer.once('error', function(err) { - transformer.removeListener('readable', handleReadable); + transformer.on("readable", handleReadable); + transformer.once("error", function (err) { + transformer.removeListener("readable", handleReadable); return callback(err); }); - transformer.once('end', function() { - transformer.removeListener('readable', handleReadable); - return callback(null, chunks.join('')); + transformer.once("end", function () { + transformer.removeListener("readable", handleReadable); + return callback(null, chunks.join("")); }); } return transformer; -}; \ No newline at end of file +} diff --git a/lib/transform/iso2709_transformer.js b/lib/transform/iso2709_transformer.js index 3fa2308..cf92be1 100644 --- a/lib/transform/iso2709_transformer.js +++ b/lib/transform/iso2709_transformer.js @@ -1,9 +1,5 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var Record = require('../marc/record'); -var Util = require('../util'); +import { Transform } from "stream"; +import util from "util"; var Iso2709Transformer = function (opts) { opts = opts || {}; @@ -15,12 +11,16 @@ var Iso2709Transformer = function (opts) { util.inherits(Iso2709Transformer, Transform); -Iso2709Transformer.prototype._transform = function (record, encoding, callback) { +Iso2709Transformer.prototype._transform = function ( + record, + encoding, + callback +) { this.push(record); return callback(); }; -Iso2709Transformer.prototype.write = function(chunk, encoding, callback) { +Iso2709Transformer.prototype.write = function (chunk, encoding, callback) { var record = chunk; try { chunk = record.marshal(); @@ -30,4 +30,4 @@ Iso2709Transformer.prototype.write = function(chunk, encoding, callback) { return Transform.prototype.write.call(this, chunk, encoding, callback); }; -module.exports = Iso2709Transformer; +export default Iso2709Transformer; diff --git a/lib/transform/marcxml_transformer.js b/lib/transform/marcxml_transformer.js index 5d1117a..bcbd776 100644 --- a/lib/transform/marcxml_transformer.js +++ b/lib/transform/marcxml_transformer.js @@ -1,10 +1,6 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var Record = require('../marc/record'); -var Util = require('../util'); -var builder = require('xmlbuilder'); +import { Transform } from "stream"; +import util from "util"; +import builder from "xmlbuilder"; var MarcxmlTransformer = function (opts) { opts = opts || {}; @@ -14,21 +10,25 @@ var MarcxmlTransformer = function (opts) { this.encoding = opts.encoding; this.starting = true; this.pretty = opts.pretty || true; - this.indent = opts.indent || ' '; - this.newline = opts.newline || '\n'; + this.indent = opts.indent || " "; + this.newline = opts.newline || "\n"; this.root = opts.root || true; this.declaration = opts.declaration || true; }; util.inherits(MarcxmlTransformer, Transform); -MarcxmlTransformer.prototype._transform = function (record, encoding, callback) { +MarcxmlTransformer.prototype._transform = function ( + record, + encoding, + callback +) { this.push(record); return callback(); }; -MarcxmlTransformer.prototype.write = function(record, encoding, callback) { - var header = ''; +MarcxmlTransformer.prototype.write = function (record, encoding, callback) { + var header = ""; if (this.starting) { this.starting = false; if (this.declaration) { @@ -36,28 +36,42 @@ MarcxmlTransformer.prototype.write = function(record, encoding, callback) { } if (this.root) { header += ''; - if (this.pretty) header += this.newline; + if (this.pretty) header += this.newline; } } - var root = builder.create('record'); - root.ele('leader', record.leader.marshal()); - record.controlFields.forEach(function(field) { - root.ele('controlfield', {'tag': field.tag}, field.data); + var root = builder.create("record"); + root.ele("leader", record.leader.marshal()); + record.controlFields.forEach(function (field) { + root.ele("controlfield", { tag: field.tag }, field.data); }); - record.dataFields.forEach(function(field) { - var ele = root.ele('datafield', {'tag': field.tag, 'ind1': field.indicator1, 'ind2': field.indicator2}); - field.subfields.forEach(function(subfield) { - ele.ele('subfield', {'code': subfield.code}, subfield.data); + record.dataFields.forEach(function (field) { + var ele = root.ele("datafield", { + tag: field.tag, + ind1: field.indicator1, + ind2: field.indicator2, + }); + field.subfields.forEach(function (subfield) { + ele.ele("subfield", { code: subfield.code }, subfield.data); }); }); var offset = this.root ? 1 : 0; - var xmlstr = root.toString({pretty: this.pretty, indent: this.indent, offset: offset, newline: this.newline}); - return Transform.prototype.write.call(this, header + xmlstr, encoding, callback); + var xmlstr = root.toString({ + pretty: this.pretty, + indent: this.indent, + offset: offset, + newline: this.newline, + }); + return Transform.prototype.write.call( + this, + header + xmlstr, + encoding, + callback + ); }; -MarcxmlTransformer.prototype._flush = function(callback) { +MarcxmlTransformer.prototype._flush = function (callback) { if (this.root) this.push("\n"); return callback(); }; -module.exports = MarcxmlTransformer; +export default MarcxmlTransformer; diff --git a/lib/transform/mij_transformer.js b/lib/transform/mij_transformer.js index 1e3c60b..e64e2fc 100644 --- a/lib/transform/mij_transformer.js +++ b/lib/transform/mij_transformer.js @@ -1,9 +1,5 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var Record = require('../marc/record'); -var Util = require('../util'); +import { Transform } from "stream"; +import util from "util"; var MijTransformer = function (opts) { if (!(this instanceof MijTransformer)) return new MijTransformer(options); @@ -24,42 +20,51 @@ MijTransformer.prototype._transform = function (record, encoding, callback) { return callback(); }; -MijTransformer.prototype.write = function(record, encoding, callback) { - var header = ''; +MijTransformer.prototype.write = function (record, encoding, callback) { + var header = ""; if (this.starting) { this.starting = false; if (this.asArray) { - header = '['; + header = "["; } } else { - header = ','; + header = ","; } var str = header; str += '{"leader":"' + record.leader.marshal() + '",'; str += '"fields":'; - str += '['; - var cfs = record.controlFields.map(function(field) { + str += "["; + var cfs = record.controlFields.map(function (field) { return '{"' + field.tag + '":"' + field.data + '"}'; }); - str += cfs.join(','); - var dfs = record.dataFields.map(function(field) { - var sfs = field.subfields.map(function(subfield) { + str += cfs.join(","); + var dfs = record.dataFields.map(function (field) { + var sfs = field.subfields.map(function (subfield) { return '{"' + subfield.code + '":"' + subfield.data + '"}'; }); - return '{"' + field.tag + '":{"subfields":[' + sfs.join(',') + '],"ind1":"' - + field.indicator1 + '","ind2":"' + field.indicator2 + '"}}'; + return ( + '{"' + + field.tag + + '":{"subfields":[' + + sfs.join(",") + + '],"ind1":"' + + field.indicator1 + + '","ind2":"' + + field.indicator2 + + '"}}' + ); }); if (dfs.length > 0) { - str += ',' + str += ","; } - str += dfs.join(','); - str += ']}'; + str += dfs.join(","); + str += "]}"; return Transform.prototype.write.call(this, str, encoding, callback); }; -MijTransformer.prototype._flush = function(callback) { - if (this.asArray) this.push(']'); +MijTransformer.prototype._flush = function (callback) { + if (this.asArray) this.push("]"); return callback(); }; -module.exports = MijTransformer; +export default MijTransformer; diff --git a/lib/transform/mrk_transformer.js b/lib/transform/mrk_transformer.js index 87f5822..a1c85e3 100644 --- a/lib/transform/mrk_transformer.js +++ b/lib/transform/mrk_transformer.js @@ -1,20 +1,13 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var os = require('os'); -var Util = require('../util'); -var Record = require('../marc/record'); -var Leader = require('../marc/leader'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Subfield = require('../marc/subfield'); +import { Transform } from "stream"; +import util from "util"; +import os from "os"; +import ControlField from "../marc/control_field.js"; var MrkTransformer = function (opts) { opts = opts || {}; this.objectMode = opts.objectMode || false; - this.encoding = opts.encoding || 'utf8'; - this.spaceReplace = opts.spaceReplace || '\\'; + this.encoding = opts.encoding || "utf8"; + this.spaceReplace = opts.spaceReplace || "\\"; Transform.call(this, opts); }; @@ -26,10 +19,10 @@ MrkTransformer.prototype._transform = function (record, encoding, callback) { return callback(); }; -MrkTransformer.prototype.write = function(chunk, encoding, callback) { +MrkTransformer.prototype.write = function (chunk, encoding, callback) { var record = chunk; var buffers = []; - if (typeof encoding === 'undefined') { + if (typeof encoding === "undefined") { encoding = this.encoding; } try { @@ -43,11 +36,23 @@ MrkTransformer.prototype.write = function(chunk, encoding, callback) { record.variableFields.forEach(function (field) { buffers.push(new Buffer("=" + field.tag + " ", encoding)); if (field instanceof ControlField) { - buffers.push(new Buffer(field.data.replace(/ /g, spaceReplace), encoding)); + buffers.push( + new Buffer(field.data.replace(/ /g, spaceReplace), encoding) + ); buffers.push(new Buffer(os.EOL)); } else { - buffers.push(new Buffer(field.indicator1.replace(' ', spaceReplace), encoding)); - buffers.push(new Buffer(field.indicator2.replace(' ', spaceReplace), encoding)); + buffers.push( + new Buffer( + field.indicator1.replace(" ", spaceReplace), + encoding + ) + ); + buffers.push( + new Buffer( + field.indicator2.replace(" ", spaceReplace), + encoding + ) + ); field.subfields.forEach(function (subfield) { buffers.push(new Buffer("$" + subfield.code, encoding)); buffers.push(new Buffer(subfield.data, encoding)); @@ -59,10 +64,10 @@ MrkTransformer.prototype.write = function(chunk, encoding, callback) { buffers.push(new Buffer(os.EOL)); chunk = Buffer.concat(buffers); } catch (err) { - console.log('error', err.stack); + console.log("error", err.stack); callback(err); } return Transform.prototype.write.call(this, chunk, encoding, callback); }; -module.exports = MrkTransformer; +export default MrkTransformer; diff --git a/lib/transform/text_transformer.js b/lib/transform/text_transformer.js index 2d2d15a..babe15c 100644 --- a/lib/transform/text_transformer.js +++ b/lib/transform/text_transformer.js @@ -1,19 +1,12 @@ -'use strict'; - -var Transform = require('stream').Transform; -var util = require('util'); -var os = require('os'); -var Util = require('../util'); -var Record = require('../marc/record'); -var Leader = require('../marc/leader'); -var ControlField = require('../marc/control_field'); -var DataField = require('../marc/data_field'); -var Subfield = require('../marc/subfield'); +import { Transform } from "stream"; +import util from "util"; +import os from "os"; +import ControlField from "../marc/control_field.js"; var TextTransformer = function (opts) { opts = opts || {}; this.objectMode = opts.objectMode || false; - this.encoding = opts.encoding || 'utf8'; + this.encoding = opts.encoding || "utf8"; Transform.call(this, opts); }; @@ -25,10 +18,10 @@ TextTransformer.prototype._transform = function (record, encoding, callback) { return callback(); }; -TextTransformer.prototype.write = function(chunk, encoding, callback) { +TextTransformer.prototype.write = function (chunk, encoding, callback) { var record = chunk; var buffers = []; - if (typeof encoding === 'undefined') { + if (typeof encoding === "undefined") { encoding = this.encoding; } try { @@ -58,10 +51,10 @@ TextTransformer.prototype.write = function(chunk, encoding, callback) { buffers.push(new Buffer(os.EOL)); chunk = Buffer.concat(buffers); } catch (err) { - console.log('error', err.stack); + console.log("error", err.stack); callback(err); } return Transform.prototype.write.call(this, chunk, encoding, callback); }; -module.exports = TextTransformer; +export default TextTransformer; diff --git a/lib/util.js b/lib/util.js index ed47ef2..164d267 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,29 +1,34 @@ -'use strict'; - -exports.isInt = function (val) { +export function isInt(val) { return /^\d+$/.test(val); -}; +} /** A formatter that pad 0 in front of an integer if the number of * digit is less than numDigits (default numDigits is 5). If * the number of digits in the integer is greater than numDigits, it will * return a string of length numDigits and all digits are 9s. */ -exports.formatInteger = function (val, numDigits) { - numDigits = typeof numDigits !== 'undefined' ? numDigits : 5; +export function formatInteger(val, numDigits) { + numDigits = typeof numDigits !== "undefined" ? numDigits : 5; var s = val + ""; var v = ""; if (s.length > numDigits) { - while (v.length < numDigits) v = '9' + v; + while (v.length < numDigits) v = "9" + v; } else { v = s; - while (v.length < numDigits) v = '0' + v; + while (v.length < numDigits) v = "0" + v; } return v; -}; +} -exports.isControlField = function (tag) { - if (tag.length == 3 && tag.charAt(0) == '0' && tag.charAt(1) == '0' && tag.charAt(2) >= '0' && tag.charAt(2) <= '9')// if (Integer.parseInt(tag) < 10) +export function isControlField(tag) { + if ( + tag.length == 3 && + tag.charAt(0) == "0" && + tag.charAt(1) == "0" && + tag.charAt(2) >= "0" && + tag.charAt(2) <= "9" + ) + // if (Integer.parseInt(tag) < 10) return true; return false; -}; +} diff --git a/package.json b/package.json index 9e3b4e9..4dd9a80 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "marc4js", "version": "0.0.10", "description": "a node.js module for handling MARC data", - "main": "index.js", + "exports": "./index.js", + "type": "module", "scripts": { "test": "mocha test --recursive --require test/helpers/chai.js " }, diff --git a/test/helpers/chai.js b/test/helpers/chai.js index d5d43a4..98245ec 100644 --- a/test/helpers/chai.js +++ b/test/helpers/chai.js @@ -1,8 +1,8 @@ -var chai = require('chai'); +import chai from "chai"; chai.config.includeStack = true; global.expect = chai.expect; global.AssertionError = chai.AssertionError; global.Assertion = chai.Assertion; -global.assert = chai.assert; \ No newline at end of file +global.assert = chai.assert; diff --git a/test/marc/control_field.js b/test/marc/control_field.js index c1b2dcc..4331f44 100644 --- a/test/marc/control_field.js +++ b/test/marc/control_field.js @@ -1,19 +1,19 @@ -var ControlField = require('../../lib/marc/control_field.js'); +import ControlField from "../../lib/marc/control_field.js"; -describe('ControlField', function () { +describe("ControlField", function () { var cf; before(function () { cf = new ControlField(); }); - it('should have id 1234', function () { + it("should have id 1234", function () { cf.id = 1234; expect(cf.id).equal(1234); }); - it('should have data abcd', function() { - cf.data = 'abcd'; - expect(cf.data).equal('abcd'); + it("should have data abcd", function () { + cf.data = "abcd"; + expect(cf.data).equal("abcd"); }); -}); \ No newline at end of file +}); diff --git a/test/marc/data_field.js b/test/marc/data_field.js index a796a44..3927b83 100644 --- a/test/marc/data_field.js +++ b/test/marc/data_field.js @@ -1,61 +1,67 @@ -var DataField = require('../../lib/marc/data_field'); -var Subfield = require('../../lib/marc/subfield'); +import DataField from "../../lib/marc/data_field.js"; +import Subfield from "../../lib/marc/subfield.js"; -describe('DataField', function () { +describe("DataField", function () { var dataField, subfield; before(function () { dataField = new DataField(); - dataField.tag = '246'; + dataField.tag = "246"; subfield = new Subfield(); - subfield.data = 'abcd'; - subfield.code = 'a'; + subfield.data = "abcd"; + subfield.code = "a"; }); - it('should has no subfields', function () { + it("should has no subfields", function () { expect(dataField.subfields).empty(); - expect(dataField.findSubfields('246')).empty(); + expect(dataField.findSubfields("246")).empty(); }); - it('should find /abc/', function () { + it("should find /abc/", function () { dataField.subfields = [subfield]; expect(dataField.find(/abc/)).equal(true); }); - it('should find return nil', function () { + it("should find return nil", function () { dataField.subfields = []; - expect(dataField.findSubfield('a')).to.be.an('undefined'); + expect(dataField.findSubfield("a")).to.be.an("undefined"); }); - it('should unmarshal a string', function () { + it("should unmarshal a string", function () { var df = new DataField(); - var s = '01\x1faNew York (N.Y.)\x1fvFiction.'; + var s = "01\x1faNew York (N.Y.)\x1fvFiction."; df.unmarshal(s); - expect(df.subfields[0].data).equal('New York (N.Y.)'); - expect(df.subfields[1].code).equal('v'); + expect(df.subfields[0].data).equal("New York (N.Y.)"); + expect(df.subfields[1].code).equal("v"); }); - it('should unmarshal a string and mashal it back', function () { + it("should unmarshal a string and mashal it back", function () { var df = new DataField(); - var s = '01\x1faNew York (N.Y.)\x1fvFiction.'; + var s = "01\x1faNew York (N.Y.)\x1fvFiction."; df.unmarshal(s); expect(df.marshal().length).equal(s.length); //expect(df.marshal().toString()).equal(s); }); - it('should create a valid data field with constructor', function() { - var df = new DataField("245", "0", "1", [['a', 'New York (N.Y.)'], ['v', 'Fiction.']]); - expect(df.indicator1).equal('0'); - expect(df.subfields[0].code).equal('a'); - expect(df.subfields[1].data).equal('Fiction.'); + it("should create a valid data field with constructor", function () { + var df = new DataField("245", "0", "1", [ + ["a", "New York (N.Y.)"], + ["v", "Fiction."], + ]); + expect(df.indicator1).equal("0"); + expect(df.subfields[0].code).equal("a"); + expect(df.subfields[1].data).equal("Fiction."); }); - it('it should marshal to a string', function() { - var df = new DataField("245", "0", "1", [['a', 'New York (N.Y.)'], ['v', 'Fiction.']]); - expect(df.marshal().length).equal('01\x1faNew York (N.Y.)\x1fvFiction.'.length); - expect(df.marshal()).equal('01\x1faNew York (N.Y.)\x1fvFiction.'); - }) - - + it("it should marshal to a string", function () { + var df = new DataField("245", "0", "1", [ + ["a", "New York (N.Y.)"], + ["v", "Fiction."], + ]); + expect(df.marshal().length).equal( + "01\x1faNew York (N.Y.)\x1fvFiction.".length + ); + expect(df.marshal()).equal("01\x1faNew York (N.Y.)\x1fvFiction."); + }); }); diff --git a/test/marc/leader.js b/test/marc/leader.js index b57bf01..c37afca 100644 --- a/test/marc/leader.js +++ b/test/marc/leader.js @@ -1,20 +1,20 @@ -var Leader = require('../../lib/marc/leader'); +import Leader from "../../lib/marc/leader.js"; -describe('Leader', function () { +describe("Leader", function () { var leader; before(function () { leader = new Leader(); }); - it('should unmarshal', function () { + it("should unmarshal", function () { leader.unmarshal("00714cam a2200205 a 4500"); expect(leader.subfieldCodeLength).equal(2); - expect(leader.charCodingScheme).equal('a'); + expect(leader.charCodingScheme).equal("a"); }); - it('should unmarshal and marshal', function() { - leader.unmarshal('00714cam a2200205 a 4500'); - expect(leader.marshal()).equal('00714cam a2200205 a 4500'); + it("should unmarshal and marshal", function () { + leader.unmarshal("00714cam a2200205 a 4500"); + expect(leader.marshal()).equal("00714cam a2200205 a 4500"); }); -}); \ No newline at end of file +}); diff --git a/test/marc/record.js b/test/marc/record.js index 6c66c2c..c4be6c4 100644 --- a/test/marc/record.js +++ b/test/marc/record.js @@ -1,32 +1,32 @@ -var Record = require('../../lib/marc/record'); -var DataField = require('../../lib/marc/data_field'); +import Record from "../../lib/marc/record.js"; +import DataField from "../../lib/marc/data_field.js"; -describe('Record', function () { +describe("Record", function () { var record; var df; beforeEach(function () { record = new Record(); df = new DataField(); - df.tag = '100'; - var s = '01\x1faNew York (N.Y.)\x1fvFiction.'; + df.tag = "100"; + var s = "01\x1faNew York (N.Y.)\x1fvFiction."; df.unmarshal(s); }); - it('should assign leader', function () { - record.leader = '00307nam a2200085Ia 45e0'; - expect(record.leader).equal('00307nam a2200085Ia 45e0'); + it("should assign leader", function () { + record.leader = "00307nam a2200085Ia 45e0"; + expect(record.leader).equal("00307nam a2200085Ia 45e0"); }); - it('should add a variable field', function() { + it("should add a variable field", function () { record.addVariableField(df); expect(record.dataFields.length).equal(1); }); - it('should find the first 100 subfield', function() { + it("should find the first 100 subfield", function () { record.addVariableField(df); - expect(record.findDataFields('100').length).equal(1); - expect(record.findDataField('100')).not.to.be.an('undefined'); + expect(record.findDataFields("100").length).equal(1); + expect(record.findDataField("100")).not.to.be.an("undefined"); }); -}); \ No newline at end of file +}); diff --git a/test/marc/subfield.js b/test/marc/subfield.js index 7f12834..cc64d49 100644 --- a/test/marc/subfield.js +++ b/test/marc/subfield.js @@ -1,24 +1,24 @@ -var Subfield = require('../../lib/marc/subfield'); +import Subfield from "../../lib/marc/subfield.js"; -describe('Subfield', function () { +describe("Subfield", function () { var subfield; before(function () { subfield = new Subfield(); }); - it('should has id 1234', function () { + it("should has id 1234", function () { subfield.id = 1234; expect(subfield.id).equal(1234); }); - it('should find /abc/', function() { - subfield.data = 'abcd'; + it("should find /abc/", function () { + subfield.data = "abcd"; expect(subfield.find(/abc/)).equal(true); }); - it('should not find /xyz/', function() { - subfield.data = 'abcd'; + it("should not find /xyz/", function () { + subfield.data = "abcd"; expect(subfield.find(/xyz/)).equal(false); }); -}); \ No newline at end of file +}); diff --git a/test/marc4js.js b/test/marc4js.js index f070a79..a37275d 100644 --- a/test/marc4js.js +++ b/test/marc4js.js @@ -1,15 +1,11 @@ -'use strict'; +import marc4js from "../lib/marc4js.js"; -var marc4js = require('../lib/marc4js'); +describe("marc4js", function () { + before(function () {}); -describe('marc4js', function () { - before(function () { - - }); - - it('should assign a record with id', function () { + it("should assign a record with id", function () { var record = new marc4js.marc.Record(); record.id = 1234; expect(record.id).equal(1234); }); -}); \ No newline at end of file +}); diff --git a/test/marc8.js b/test/marc8.js index 2c7a8de..34c228d 100644 --- a/test/marc8.js +++ b/test/marc8.js @@ -1,44 +1,67 @@ -var parse = require('../lib/parse'); -var transform = require('../lib/transform'); -var fs = require('fs'); -var marc8 = require('marc8'); +import parse from "../lib/parse.js"; +import fs from "fs"; +import marc8 from "marc8"; -describe('marc8', function () { - it('should parse marc8 encoded file', function(done) { - var utf8 = fs.readFileSync('test/data/utf8.mrc').toString(); - fs.readFile('test/data/marc8.mrc', {encoding: 'binary'}, function(err, data) { - parse(data, {fromFormat: 'iso2709', marc8: true, marc8converter: require('marc8')}, function(err, records) { - expect(records.length).to.equal(1); - var record = records[0]; - var fields = record.dataFields; - fields.forEach(function(field) { - if (field.tag == '240') { - expect(utf8.indexOf(field.subfields[0].data)).to.be.least(0); - } else if (field.tag == '008') { - expect(field.data).equal('840112s1962 pau b 00010 eng'); +describe("marc8", function () { + it("should parse marc8 encoded file", function (done) { + var utf8 = fs.readFileSync("test/data/utf8.mrc").toString(); + fs.readFile( + "test/data/marc8.mrc", + { encoding: "binary" }, + function (err, data) { + parse( + data, + { + fromFormat: "iso2709", + marc8: true, + marc8converter: marc8, + }, + function (err, records) { + expect(records.length).to.equal(1); + var record = records[0]; + var fields = record.dataFields; + fields.forEach(function (field) { + if (field.tag == "240") { + expect( + utf8.indexOf(field.subfields[0].data) + ).to.be.least(0); + } else if (field.tag == "008") { + expect(field.data).equal( + "840112s1962 pau b 00010 eng" + ); + } + }); + done(); } - }); - done(); - }); - }); + ); + } + ); }); - // TODO: This test is failing at the moment. Disable it for now. + // TODO: This test is failing at the moment. Disable it for now. // it('should parse marc8 encoded file with accented chars', function(done) { // fs.readFile('test/data/marc8_accented_chars.mrc', {encoding: 'binary'}, function(err, data) { - // parse(data, {fromFormat: 'iso2709', marc8: false, marc8converter: require('marc8')}, function(err, records) { + // parse(data, {fromFormat: 'iso2709', marc8: false, marc8converter: marc8 }, function(err, records) { // expect(records.length).equal(1); // }); // done(); // }); // }); - it('should parse a file with mixed marc8 and utf8 encoding', function(done) { - fs.readFile('test/data/marc8_utf8_mixed.mrc', {encoding: 'binary'}, function(err, data) { - parse(data, {fromFormat: 'iso2709', marc8converter: marc8}, function(err, records) { - expect(records.length).equal(6); - }); - done(); - }); + it("should parse a file with mixed marc8 and utf8 encoding", function (done) { + fs.readFile( + "test/data/marc8_utf8_mixed.mrc", + { encoding: "binary" }, + function (err, data) { + parse( + data, + { fromFormat: "iso2709", marc8converter: marc8 }, + function (err, records) { + expect(records.length).equal(6); + } + ); + done(); + } + ); }); }); diff --git a/test/parse.js b/test/parse.js index f47c607..0f8d1fe 100644 --- a/test/parse.js +++ b/test/parse.js @@ -1,33 +1,32 @@ -var parse = require('../lib/parse'); -var transform = require('../lib/transform'); -var fs = require('fs'); -var marc8 = require('marc8'); +import parse from "../lib/parse.js"; +import transform from "../lib/transform.js"; +import fs from "fs"; +import marc8 from "marc8"; -describe('parse', function () { +describe("parse", function () { var stream; - before(function () { - }); + before(function () {}); - it('should parse one record and identify fields', function (done) { - var stream = fs.createReadStream('test/data/sandburg.mrc'); - var parser = parse({objectMode: true}); + it("should parse one record and identify fields", function (done) { + var stream = fs.createReadStream("test/data/sandburg.mrc"); + var parser = parse({ objectMode: true }); var first; var count = 0; - parser.on('data', function (record) { + parser.on("data", function (record) { count += 1; if (count === 1) first = record; }); stream.pipe(parser); - parser.on('error', function (error) { + parser.on("error", function (error) { console.log("error: ", error); done(); }); - parser.on('end', function () { + parser.on("end", function () { expect(count).equal(1); expect(first.variableFields.length).to.equal(23); expect(first.controlFields.length).equal(4); @@ -35,31 +34,31 @@ describe('parse', function () { }); }); - it('should parse multiple records', function (done) { - var stream = fs.createReadStream('test/data/collection.mrc'); - var parser = parse({objectMode: true, fromFormat: 'marc'}); + it("should parse multiple records", function (done) { + var stream = fs.createReadStream("test/data/collection.mrc"); + var parser = parse({ objectMode: true, fromFormat: "marc" }); var count = 0; - parser.on('data', function () { + parser.on("data", function () { count += 1; }); stream.pipe(parser); - parser.on('error', function (error) { + parser.on("error", function (error) { console.log("error: ", error); done(); }); - parser.on('end', function () { + parser.on("end", function () { expect(count).equal(2); done(); }); }); - it('should work with callback API', function(done) { - var data = fs.readFileSync('test/data/collection.mrc'); - parse(data, {objectMode: true}, function(err, records) { + it("should work with callback API", function (done) { + var data = fs.readFileSync("test/data/collection.mrc"); + parse(data, { objectMode: true }, function (err, records) { if (err) { console.log(err); } else { @@ -69,24 +68,25 @@ describe('parse', function () { }); }); - it('should work with stream API', function(done) { - var data = '00783nam a2200217Ki 4500001000800000005001700008006001900025007001500044008004100059042000700100092001000107245005000117260007600167300004800243500005500291500003200346540005700378655002200435710002300457856008500480PG1060720101216083600.0m||||||||d||||||||cr||n |||muaua101213s2004 utu o eng d adc aeBook04aThe Real Mother Gooseh[electronic resource]. aSalt Lake City :bProject Gutenberg Literary Archive Foundation,c2004. a1 online resource :bmultiple file formats. aRecords generated from Project Gutenberg RDF data. aISO 639-2 language code: en aApplicable license: http://www.gutenberg.org/license 0aElectronic books.2 aProject Gutenberg.40uhttp://www.gutenberg.org/etext/10607yClick here to access a downloadable ebook.'; - var parser = parse({objectMode: true}); + it("should work with stream API", function (done) { + var data = + "00783nam a2200217Ki 4500001000800000005001700008006001900025007001500044008004100059042000700100092001000107245005000117260007600167300004800243500005500291500003200346540005700378655002200435710002300457856008500480PG1060720101216083600.0m||||||||d||||||||cr||n |||muaua101213s2004 utu o eng d adc aeBook04aThe Real Mother Gooseh[electronic resource]. aSalt Lake City :bProject Gutenberg Literary Archive Foundation,c2004. a1 online resource :bmultiple file formats. aRecords generated from Project Gutenberg RDF data. aISO 639-2 language code: en aApplicable license: http://www.gutenberg.org/license 0aElectronic books.2 aProject Gutenberg.40uhttp://www.gutenberg.org/etext/10607yClick here to access a downloadable ebook."; + var parser = parse({ objectMode: true }); var count = 0; - parser.on('data', function (record) { + parser.on("data", function (record) { var fields = record.dataFields; var field = fields[fields.length - 1]; - expect(field.indicator1).equal('4'); - expect(field.indicator2).equal('0'); + expect(field.indicator1).equal("4"); + expect(field.indicator2).equal("0"); count += 1; }); - parser.on('error', function (error) { + parser.on("error", function (error) { console.log("error: ", error); }); - parser.on('end', function () { + parser.on("end", function () { expect(count).equal(1); done(); }); @@ -95,127 +95,151 @@ describe('parse', function () { parser.end(); }); - it('should parse mrk format', function(done) { - var data = fs.readFileSync('test/data/collection.mrk'); - parse(data, {fromFormat: 'mrk'}, function(err, records) { + it("should parse mrk format", function (done) { + var data = fs.readFileSync("test/data/collection.mrk"); + parse(data, { fromFormat: "mrk" }, function (err, records) { if (err) { console.log(err); } else { expect(records.length).to.equal(2); - expect(records[1].leader.marshal()).equal('01832cmmaa2200349 a 4500'); + expect(records[1].leader.marshal()).equal( + "01832cmmaa2200349 a 4500" + ); } done(); }); }); - it('should parse text format', function(done) { - "use strict"; - var data = fs.readFileSync('test/data/collection.txt'); - parse(data, {fromFormat: 'text'}, function(err, records) { + it("should parse text format", function (done) { + var data = fs.readFileSync("test/data/collection.txt"); + parse(data, { fromFormat: "text" }, function (err, records) { if (err) { console.log(err); } else { expect(records.length).to.equal(2); - expect(records[1].leader.marshal()).equal('01832cmmaa2200349 a 4500'); + expect(records[1].leader.marshal()).equal( + "01832cmmaa2200349 a 4500" + ); } done(); }); }); - it('should parse MARCXML', function(done) { - var data = fs.readFileSync('test/data/sandburg.xml'); - parse(data.toString(), {fromFormat: 'marcxml'}, function(err, records) { - if (err) { - console.log(err); - } else { - expect(records.length).equal(1); - expect(records[0].leader.marshal()).equal('01142cam 2200301 a 4500'); - expect(records[0].dataFields[6].subfields[1].code).equal('d'); + it("should parse MARCXML", function (done) { + var data = fs.readFileSync("test/data/sandburg.xml"); + parse( + data.toString(), + { fromFormat: "marcxml" }, + function (err, records) { + if (err) { + console.log(err); + } else { + expect(records.length).equal(1); + expect(records[0].leader.marshal()).equal( + "01142cam 2200301 a 4500" + ); + expect(records[0].dataFields[6].subfields[1].code).equal( + "d" + ); + } + done(); } - done(); - }); - }); - - it('should parse MARCXML with multiple records and namespace', function(done) { - var data = fs.readFileSync('test/data/collection.xml'); - parse(data.toString(), {fromFormat: 'marcxml'}, function(err, records) { - if (err) { - console.log(err); - } else { - expect(records.length).equal(2); - expect(records[1].leader.marshal()).equal('01832cmma 2200349 a 4500'); + ); + }); + + it("should parse MARCXML with multiple records and namespace", function (done) { + var data = fs.readFileSync("test/data/collection.xml"); + parse( + data.toString(), + { fromFormat: "marcxml" }, + function (err, records) { + if (err) { + console.log(err); + } else { + expect(records.length).equal(2); + expect(records[1].leader.marshal()).equal( + "01832cmma 2200349 a 4500" + ); + } + done(); } - done(); - }); + ); }); - it('should parse marc in json', function(done) { - var data = fs.readFileSync('test/data/marc_in_json.json'); - parse(data.toString(), {fromFormat: 'json'}, function(err, records) { + it("should parse marc in json", function (done) { + var data = fs.readFileSync("test/data/marc_in_json.json"); + parse(data.toString(), { fromFormat: "json" }, function (err, records) { expect(records.length).equal(1); done(); }); }); - it('should parse marc-in-json with multiple records', function(done) { - var data = fs.readFileSync('test/data/collection.json'); - parse(data.toString(), {fromFormat: 'json'}, function(err, records) { + it("should parse marc-in-json with multiple records", function (done) { + var data = fs.readFileSync("test/data/collection.json"); + parse(data.toString(), { fromFormat: "json" }, function (err, records) { expect(records.length).equal(2); - expect(records[1].leader.marshal()).equal('01832cmma 2200349 a 4500'); - done(); - }); - }); - - it('should parse file with only utf8', function(done) { - var data = fs.readFileSync('test/data/utf8_only.mrc'); - parse(data, {fromFormat: 'iso2709', marc8converter: marc8}, function(err, records) { - expect(records.length).equal(6); - transform(records, {toFormat: 'text'}, function(err, output) { - expect(output.indexOf('黑澤明')).to.be.least(0); - expect(output.indexOf('Premio del Público 27°')).to.be.least(0); - done(); - }); - }); + expect(records[1].leader.marshal()).equal( + "01832cmma 2200349 a 4500" + ); + done(); + }); + }); + + it("should parse file with only utf8", function (done) { + var data = fs.readFileSync("test/data/utf8_only.mrc"); + parse( + data, + { fromFormat: "iso2709", marc8converter: marc8 }, + function (err, records) { + expect(records.length).equal(6); + transform( + records, + { toFormat: "text" }, + function (err, output) { + expect(output.indexOf("黑澤明")).to.be.least(0); + expect( + output.indexOf("Premio del Público 27°") + ).to.be.least(0); + done(); + } + ); + } + ); }); - it('should return empty array for a blank string as text', function(done) { - "use strict"; + it("should return empty array for a blank string as text", function (done) { var data = ""; - parse(data, {fromFormat: 'text'}, function(err, records) { + parse(data, { fromFormat: "text" }, function (err, records) { expect(records.length).to.equal(0); done(); }); }); - it('should return empty array for a blank string as marc', function(done) { - "use strict"; + it("should return empty array for a blank string as marc", function (done) { var data = ""; - parse(data, {fromFormat: 'marc'}, function(err, records) { + parse(data, { fromFormat: "marc" }, function (err, records) { expect(records.length).to.equal(0); done(); }); }); - it('should return empty array for a null variable as text', function(done) { - "use strict"; + it("should return empty array for a null variable as text", function (done) { var data = null; - parse(data, {fromFormat: 'text'}, function(err, records) { + parse(data, { fromFormat: "text" }, function (err, records) { expect(records.length).to.equal(0); done(); }); }); - it('should return empty array for a null variable as MARC', function(done) { - "use strict"; + it("should return empty array for a null variable as MARC", function (done) { var data = null; - parse(data, {fromFormat: 'marc'}, function(err, records) { + parse(data, { fromFormat: "marc" }, function (err, records) { expect(records.length).to.equal(0); done(); }); }); // //it('should throw an error when there is an error', function(done) { - // "use strict"; // var data = fs.readFileSync('test/data/collection-error.mrc'); // parse(data, {fromFormat: 'marc'}, function(err, records) { // expect(err).to.exist; diff --git a/test/transform.js b/test/transform.js index a9057b1..235b38d 100644 --- a/test/transform.js +++ b/test/transform.js @@ -1,259 +1,298 @@ -'use strict'; +import fs from "fs"; +import os from "os"; +import transform from "../lib/transform.js"; +import parse from "../lib/parse.js"; +import Record from "../lib/marc/record.js"; +import DataField from "../lib/marc/data_field.js"; +import ControlField from "../lib/marc/control_field.js"; +import Leader from "../lib/marc/leader.js"; -var fs = require('fs'); -var os = require('os'); -var transform = require('../lib/transform'); -var parse = require('../lib/parse'); -var Record = require('../lib/marc/record'); -var Subfield = require('../lib/marc/subfield'); -var DataField = require('../lib/marc/data_field'); -var ControlField = require('../lib/marc/control_field'); -var Leader = require('../lib/marc/leader'); - - -describe('transform', function () { +describe("transform", function () { var records; before(function () { records = []; var r1 = new Record(); - r1.leader = new Leader('00307nam a2200085Ia 45e0'); - r1.addVariableField(new ControlField("008", "080906s9999 xx 000 0 und d")); - r1.addVariableField(new DataField("100", '1', ' ', [["a", "Biggers, Earl Derr."]])); - r1.addVariableField(new DataField("245", '1', '0', [["a", "Charlie Chan Carries On"], ["h", "[electronic resource]"]])); - r1.addVariableField(new DataField("500", ' ', ' ', [["a", "An ebook provided by Project Gutenberg Australia"]])); - r1.addVariableField(new DataField("856", '4', '0', [["u", "http://gutenberg.net.au/ebooks07/0700761h.html"]])); + r1.leader = new Leader("00307nam a2200085Ia 45e0"); + r1.addVariableField( + new ControlField("008", "080906s9999 xx 000 0 und d") + ); + r1.addVariableField( + new DataField("100", "1", " ", [["a", "Biggers, Earl Derr."]]) + ); + r1.addVariableField( + new DataField("245", "1", "0", [ + ["a", "Charlie Chan Carries On"], + ["h", "[electronic resource]"], + ]) + ); + r1.addVariableField( + new DataField("500", " ", " ", [ + ["a", "An ebook provided by Project Gutenberg Australia"], + ]) + ); + r1.addVariableField( + new DataField("856", "4", "0", [ + ["u", "http://gutenberg.net.au/ebooks07/0700761h.html"], + ]) + ); records.push(r1); var r2 = new Record(); - r2.leader = new Leader('00287nam a2200085Ia 45e0'); - r2.addVariableField(new ControlField("008", "080906s9999 xx 000 0 und d")); - r2.addVariableField(new DataField("100", '1', ' ', [["a", "Wallace, Edgar."]])); - r2.addVariableField(new DataField("245", '1', '0', [["a", "Sanders"], ["h", "[electronic resource]"]])); - r2.addVariableField(new DataField("500", ' ', ' ', [["a", "An ebook provided by Project Gutenberg Australia"]])); - r2.addVariableField(new DataField("856", '4', '0', [["u", "http://gutenberg.net.au/ebooks07/0700771h.html"]])); + r2.leader = new Leader("00287nam a2200085Ia 45e0"); + r2.addVariableField( + new ControlField("008", "080906s9999 xx 000 0 und d") + ); + r2.addVariableField( + new DataField("100", "1", " ", [["a", "Wallace, Edgar."]]) + ); + r2.addVariableField( + new DataField("245", "1", "0", [ + ["a", "Sanders"], + ["h", "[electronic resource]"], + ]) + ); + r2.addVariableField( + new DataField("500", " ", " ", [ + ["a", "An ebook provided by Project Gutenberg Australia"], + ]) + ); + r2.addVariableField( + new DataField("856", "4", "0", [ + ["u", "http://gutenberg.net.au/ebooks07/0700771h.html"], + ]) + ); records.push(r2); }); - it('should stringify the records with callback', function(done) { - var data = fs.readFileSync('test/data/PGA_2records.mrc'); - transform(records, function(err, output) { + it("should stringify the records with callback", function (done) { + var data = fs.readFileSync("test/data/PGA_2records.mrc"); + transform(records, function (err, output) { expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); }); - it('should stringify one record', function(done) { - transform(records[0], function(err, output) { + it("should stringify one record", function (done) { + transform(records[0], function (err, output) { expect(output).to.be.not.null; done(); }); }); - it('should stringify with a flowing stream API', function(done) { - var transformer = transform({objectMode: true}); - var output = ''; - transformer.on('data', function(record) { + it("should stringify with a flowing stream API", function (done) { + var transformer = transform({ objectMode: true }); + var output = ""; + transformer.on("data", function (record) { output += record; }); - transformer.on('error', function(err) { + transformer.on("error", function (err) { console.log(err.message); }); - transformer.on('end', function() { - var data = fs.readFileSync('test/data/PGA_2records.mrc'); + transformer.on("end", function () { + var data = fs.readFileSync("test/data/PGA_2records.mrc"); expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); - records.forEach(function(record) { + records.forEach(function (record) { transformer.write(record); }); transformer.end(); - }); - it('should stringify with a non-flowing stream API', function(done) { - var transformer = transform({objectMode: false, toFormat: 'iso2709'}); - var output = ''; - transformer.on('readable', function() { + it("should stringify with a non-flowing stream API", function (done) { + var transformer = transform({ objectMode: false, toFormat: "iso2709" }); + var output = ""; + transformer.on("readable", function () { var record; - while (record = transformer.read()) { + while ((record = transformer.read())) { output += record; } }); - transformer.on('error', function(err) { + transformer.on("error", function (err) { console.log(err.message); }); - transformer.on('end', function() { - var data = fs.readFileSync('test/data/PGA_2records.mrc'); + transformer.on("end", function () { + var data = fs.readFileSync("test/data/PGA_2records.mrc"); expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); - records.forEach(function(record) { + records.forEach(function (record) { transformer.write(record); }); transformer.end(); }); - it('should pipe to destination', function(done) { - var transformer = transform({objectMode: true}); - var parser = parse({objectMode: true}); - var mrc = '00783nam a2200217Ki 4500001000800000005001700008006001900025007001500044008004100059042000700100092001000107245005000117260007600167300004800243500005500291500003200346540005700378655002200435710002300457856008500480PG1060720101216083600.0m||||||||d||||||||cr||n |||muaua101213s2004 utu o eng d adc aeBook04aThe Real Mother Gooseh[electronic resource]. aSalt Lake City :bProject Gutenberg Literary Archive Foundation,c2004. a1 online resource :bmultiple file formats. aRecords generated from Project Gutenberg RDF data. aISO 639-2 language code: en aApplicable license: http://www.gutenberg.org/license 0aElectronic books.2 aProject Gutenberg.40uhttp://www.gutenberg.org/etext/10607yClick here to access a downloadable ebook.'; - var ws = fs.createWriteStream('/tmp/the_real_mother_goose.mrc'); - var is = fs.createReadStream('test/data/the_real_mother_goose.mrc'); - return is.pipe(parser).pipe(transformer).pipe(ws).on('finish', function() { - return fs.readFile('/tmp/the_real_mother_goose.mrc', function(err, data) { - //expect(data.toString().length).equal(mrc.length); - expect(data.toString()).equal(mrc); - return fs.unlink('/tmp/the_real_mother_goose.mrc', done); + it("should pipe to destination", function (done) { + var transformer = transform({ objectMode: true }); + var parser = parse({ objectMode: true }); + var mrc = + "00783nam a2200217Ki 4500001000800000005001700008006001900025007001500044008004100059042000700100092001000107245005000117260007600167300004800243500005500291500003200346540005700378655002200435710002300457856008500480PG1060720101216083600.0m||||||||d||||||||cr||n |||muaua101213s2004 utu o eng d adc aeBook04aThe Real Mother Gooseh[electronic resource]. aSalt Lake City :bProject Gutenberg Literary Archive Foundation,c2004. a1 online resource :bmultiple file formats. aRecords generated from Project Gutenberg RDF data. aISO 639-2 language code: en aApplicable license: http://www.gutenberg.org/license 0aElectronic books.2 aProject Gutenberg.40uhttp://www.gutenberg.org/etext/10607yClick here to access a downloadable ebook."; + var ws = fs.createWriteStream("/tmp/the_real_mother_goose.mrc"); + var is = fs.createReadStream("test/data/the_real_mother_goose.mrc"); + return is + .pipe(parser) + .pipe(transformer) + .pipe(ws) + .on("finish", function () { + return fs.readFile( + "/tmp/the_real_mother_goose.mrc", + function (err, data) { + //expect(data.toString().length).equal(mrc.length); + expect(data.toString()).equal(mrc); + return fs.unlink( + "/tmp/the_real_mother_goose.mrc", + done + ); + } + ); }); - }); }); - it('should mrkify the records with callback', function(done) { - var data = fs.readFileSync('test/data/PGA_2records.mrk'); + it("should mrkify the records with callback", function (done) { + var data = fs.readFileSync("test/data/PGA_2records.mrk"); data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; - transform(records, {format: 'mrk'}, function(err, output) { + transform(records, { format: "mrk" }, function (err, output) { expect(output.length).equal(data.length); expect(output).equal(data); done(); }); }); - it('should mrkify one record', function(done) { - transform(records[0], {toFormat: 'mrk'}, function(err, output) { + it("should mrkify one record", function (done) { + transform(records[0], { toFormat: "mrk" }, function (err, output) { expect(output).to.be.not.null; done(); }); }); - it('should mrkify with a flowing stream API', function(done) { - var textifier = transform({objectMode: true, format: 'mrk'}); - var output = ''; - textifier.on('data', function(record) { + it("should mrkify with a flowing stream API", function (done) { + var textifier = transform({ objectMode: true, format: "mrk" }); + var output = ""; + textifier.on("data", function (record) { output += record; }); - textifier.on('error', function(err) { + textifier.on("error", function (err) { console.log(err.message); }); - textifier.on('end', function() { - var data = fs.readFileSync('test/data/PGA_2records.mrk'); + textifier.on("end", function () { + var data = fs.readFileSync("test/data/PGA_2records.mrk"); data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); - records.forEach(function(record) { + records.forEach(function (record) { textifier.write(record); }); textifier.end(); }); - it('should mrkify with a non-flowing stream API', function(done) { - var textifier = transform({objectMode: false, format: 'mrk'}); - var output = ''; - textifier.on('readable', function() { + it("should mrkify with a non-flowing stream API", function (done) { + var textifier = transform({ objectMode: false, format: "mrk" }); + var output = ""; + textifier.on("readable", function () { var record; - while (record = textifier.read()) { + while ((record = textifier.read())) { output += record; } }); - textifier.on('error', function(err) { + textifier.on("error", function (err) { console.log(err.message); }); - textifier.on('end', function() { - var data = fs.readFileSync('test/data/PGA_2records.mrk'); + textifier.on("end", function () { + var data = fs.readFileSync("test/data/PGA_2records.mrk"); data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); - records.forEach(function(record) { + records.forEach(function (record) { textifier.write(record); }); textifier.end(); }); - it('should textify the records with callback', function(done) { - var data = fs.readFileSync('test/data/PGA_2records.txt'); + it("should textify the records with callback", function (done) { + var data = fs.readFileSync("test/data/PGA_2records.txt"); data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; - transform(records, {format: 'text'}, function(err, output) { + transform(records, { format: "text" }, function (err, output) { expect(output.length).equal(data.length); expect(output).equal(data); done(); }); }); - it('should textify one record', function(done) { - transform(records[0], {toFormat: 'text'}, function(err, output) { + it("should textify one record", function (done) { + transform(records[0], { toFormat: "text" }, function (err, output) { expect(output).to.be.not.null; done(); }); }); - it('should textify with a flowing stream API', function(done) { - var textifier = transform({objectMode: true, format: 'text'}); - var output = ''; - textifier.on('data', function(record) { + it("should textify with a flowing stream API", function (done) { + var textifier = transform({ objectMode: true, format: "text" }); + var output = ""; + textifier.on("data", function (record) { output += record; }); - textifier.on('error', function(err) { + textifier.on("error", function (err) { console.log(err.message); }); - textifier.on('end', function() { - var data = fs.readFileSync('test/data/PGA_2records.txt'); + textifier.on("end", function () { + var data = fs.readFileSync("test/data/PGA_2records.txt"); data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); - records.forEach(function(record) { + records.forEach(function (record) { textifier.write(record); }); textifier.end(); }); - it('should textify with a non-flowing stream API', function(done) { - var textifier = transform({objectMode: false, format: 'text'}); - var output = ''; - textifier.on('readable', function() { + it("should textify with a non-flowing stream API", function (done) { + var textifier = transform({ objectMode: false, format: "text" }); + var output = ""; + textifier.on("readable", function () { var record; - while (record = textifier.read()) { + while ((record = textifier.read())) { output += record; } }); - textifier.on('error', function(err) { + textifier.on("error", function (err) { console.log(err.message); }); - textifier.on('end', function() { - var data = fs.readFileSync('test/data/PGA_2records.txt'); - data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; + textifier.on("end", function () { + var data = fs.readFileSync("test/data/PGA_2records.txt"); + data = data.toString().replace(/\r\n?/g, os.EOL) + os.EOL; expect(output.length).equal(data.length); expect(output).equal(data.toString()); done(); }); - records.forEach(function(record) { + records.forEach(function (record) { textifier.write(record); }); textifier.end(); }); - it('should marcxmlify with a non-flowing stream API', function(done) { - transform(records[0], {toFormat: 'marcxml'}, function(err, output) { + it("should marcxmlify with a non-flowing stream API", function (done) { + transform(records[0], { toFormat: "marcxml" }, function (err, output) { expect(output).to.be.not.null; done(); }); }); - it('should transform to marc-in-json format', function(done) { - transform(records, {toFormat: 'mij'}, function(err, output) { + it("should transform to marc-in-json format", function (done) { + transform(records, { toFormat: "mij" }, function (err, output) { expect(output).to.be.not.null; var obj = JSON.parse(output); - expect(obj[0].leader).equal('00307nam a2200085Ia 45e0'); + expect(obj[0].leader).equal("00307nam a2200085Ia 45e0"); expect(obj[1].fields.length).equal(5); done(); }); }); - -}); \ No newline at end of file +}); diff --git a/test/util.js b/test/util.js index c656ad8..17c30c8 100644 --- a/test/util.js +++ b/test/util.js @@ -1,7 +1,7 @@ -var Util = require('../lib/util'); +import * as Util from "../lib/util.js"; -describe('Util', function () { - it('should recognize integer with function isInt', function () { +describe("Util", function () { + it("should recognize integer with function isInt", function () { expect(Util.isInt(1234)).to.be.true(); expect(Util.isInt("1234")).to.be.true(); expect(Util.isInt(12.34)).to.be.false(); @@ -11,7 +11,7 @@ describe('Util', function () { expect(Util.isInt("")).to.be.false(); }); - it('should format integer', function() { - expect(Util.formatInteger(23, 5)).equal('00023'); + it("should format integer", function () { + expect(Util.formatInteger(23, 5)).equal("00023"); }); -}); \ No newline at end of file +});