2 * Copyright 2015 ByWater Solutions
4 * This file is part of Koha.
6 * Koha is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Koha is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Koha; if not, see <http://www.gnu.org/licenses>.
20 define( [ 'marc-record' ], function( MARC ) {
21 // Convert any characters for display
22 function _sanitize( text ) {
23 return text.replace( '$', '{dollar}' );
27 function _desanitize( text ) {
28 return text.replace( '{dollar}', '$' );
31 RecordToText: function( record ) {
33 var fields = record.fields();
35 for ( var i = 0; i < fields.length; i++ ) {
36 var field = fields[i];
38 if ( field.isControlField() ) {
39 lines.push( field.tagnumber() + ' ' + _sanitize( field.subfield( '@' ) ) );
41 var result = [ field.tagnumber() + ' ' ];
43 result.push( field.indicator(0) == ' ' ? '_' : field.indicator(0), ' ' );
44 result.push( field.indicator(1) == ' ' ? '_' : field.indicator(1), ' ' );
46 $.each( field.subfields(), function( i, subfield ) {
47 result.push( '$' + subfield[0] + ' ' + _sanitize( subfield[1] ) );
50 lines.push( result.join('') );
54 return lines.join('\n');
57 TextToRecord: function( text ) {
58 var record = new MARC.Record();
61 $.each( text.split('\n'), function( i, line ) {
62 var tagNumber = line.match( /^([A-Za-z0-9]{3}) / );
65 errors.push( { type: 'noTag', line: i } );
68 tagNumber = tagNumber[1];
70 if ( tagNumber < '010' ) {
71 var field = new MARC.Field( tagNumber, ' ', ' ', [ [ '@', _desanitize( line.substring( 4 ) ) ] ] );
73 record.addField( field );
75 var indicators = line.match( /^... ([0-9A-Za-z_]) ([0-9A-Za-z_])/ );
77 errors.push( { type: 'noIndicators', line: i } );
81 var field = new MARC.Field( tagNumber, ( indicators[1] == '_' ? ' ' : indicators[1] ), ( indicators[2] == '_' ? ' ' : indicators[2] ), [] );
83 var matcher = /[$|ǂ‡]([a-z0-9%]) /g;
88 while ( ( match = matcher.exec(line) ) ) {
89 subfields.push( { code: match[1], ch: match.index } );
92 $.each( subfields, function( i, subfield ) {
93 var next = subfields[ i + 1 ];
95 field.addSubfield( [ subfield.code, _desanitize( line.substring( subfield.ch + 3, next ? next.ch : line.length ) ) ] );
99 record.addField( field );
103 return errors.length ? { errors: errors } : record;