<commit_before>/**
 *
 * @licstart  The following is the entire license notice for the JavaScript code in this file. 
 *
 * Validate and fix MARC records
 *
 * Copyright (c) 2014-2016 University Of Helsinki (The National Library Of Finland)
 *
 * This file is part of marc-record-validate
 *
 * marc-record-validate is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this file.
 *
 **/

/* istanbul ignore next: umd wrapper */
(function (root, factory) {

  'use strict';

  if (typeof define === 'function' && define.amd) {
    define(['es6-polyfills/lib/polyfills/object'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('es6-polyfills/lib/polyfills/object'));
  }

}(this, factory));

function factory(Object)
{

  'use strict';

  function clone(obj)
  {
    return typeof obj === 'object' ? JSON.parse(JSON.stringify(obj)) : obj;
  }

  function createMessage(type, message, field)
  {
    return Object.assign(
      {
        type: type,
        message: message
      },
      typeof field !== 'object' ? {} : {
        field: field
      }
    );
  }
  
  return {
    validate: {
      debug: function(message, field)
      {
        return createMessage('debug', message, clone(field));
      },
      info: function(message, field)
      {
        return createMessage('info', message, clone(field));
      },
      warning: function(message, field)
      {
        return createMessage('warning', message, clone(field));
      },
      error: function(message, field)
      {
        return createMessage('error', message, clone(field));
      }
    },
    fix: {
      modifyLeader: function(record, value)
      {

        var result = {
          type: 'modifyLeader',
          oldValue: record.leader,
          newValue: value
        };

        record.leader = value;

        return result;

      },
      addField: function(record, field)
      {

        record.insertField(field);

        return {
          type: 'addField',
          field: clone(field)
        };

      },
      removeField: function(record, field)
      {

        record.fields.splice(record.fields.indexOf(field), 1);

        return {
          type: 'removeField',
          field: clone(field)
        };

      },
      addSubfield: function(field, subfield)
      {
        if (!field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a variable field');
        } else {

          field.subfields.push(subfield);
          
          return {
            type: 'addSubfield',
            field: clone(field),
            subfield: clone(subfield)
          };

        }
      },
      removeSubfield: function(field, subfield)
      {
        if (!field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a variable field');
        } else {

          field.subfields.splice(field.subfields.indexOf(subfield), 1);
          
          return {
            type: 'removeSubfield',
            field: clone(field),
            subfield: clone(subfield)
          };

        }
      },
      moveField: function(record, field, index_new)
      {

        var index_current = record.fields.indexOf(field);

        record.fields.splice(index_new, 0, field);

        if (index_current < index_new) {
          record.fields.splice(record.fields.indexOf(field), 1);
        } else {
          record.fields.splice(record.fields.lastIndexOf(field), 1);
        }        

        return {
          'type': 'moveField',
          'field': clone(field),
          'old': index_current,
          'new': index_new
        };

      },
      modifyFieldTag: function(field, tag)
      {

        var field_original = clone(field);

        field.tag = tag;

        return {
          'type': 'modifyField',
          'old': field_original,
          'new': clone(field)
        };

      },
      modifyFieldValue: function(field, value)
      {

        var field_original;

        if (field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a control field');
        } else {

          field_original = clone(field);
          field.value = value;

          return {
            'type': 'modifyField',
            'old': field_original,
            'new': clone(field)
          };

        }

      },
      modifySubfieldCode: function(field, subfield, code)
      {

        var field_original = clone(field);

        subfield.code = code;

        return {
          'type': 'modifyField',
          'old': field_original,
          'new': clone(field)
        };

      },
      modifySubfieldValue: function(field, subfield, value)
      {

        var field_original = clone(field);

        subfield.value = value;

        return {
          'type': 'modifyField',
          'old': field_original,
          'new': clone(field)
        };

      },
      modifySubfields: function(field, modifyCallback)
      {

        var field_original;

        if (!field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a variable field');
        } else {

          field_original = clone(field);
          
          field.subfields.forEach(modifyCallback);
          
          return {
            'type': 'modifyField',
            'old': field_original,
            'new': clone(field)
          };

        }

      }
    }
  };

}
<commit_msg>Fixes moving to wrong index<commit_after>/**
 *
 * @licstart  The following is the entire license notice for the JavaScript code in this file. 
 *
 * Validate and fix MARC records
 *
 * Copyright (c) 2014-2016 University Of Helsinki (The National Library Of Finland)
 *
 * This file is part of marc-record-validate
 *
 * marc-record-validate is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this file.
 *
 **/

/* istanbul ignore next: umd wrapper */
(function (root, factory) {

  'use strict';

  if (typeof define === 'function' && define.amd) {
    define(['es6-polyfills/lib/polyfills/object'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('es6-polyfills/lib/polyfills/object'));
  }

}(this, factory));

function factory(Object)
{

  'use strict';

  function clone(obj)
  {
    return typeof obj === 'object' ? JSON.parse(JSON.stringify(obj)) : obj;
  }

  function createMessage(type, message, field)
  {
    return Object.assign(
      {
        type: type,
        message: message
      },
      typeof field !== 'object' ? {} : {
        field: field
      }
    );
  }
  
  return {
    validate: {
      debug: function(message, field)
      {
        return createMessage('debug', message, clone(field));
      },
      info: function(message, field)
      {
        return createMessage('info', message, clone(field));
      },
      warning: function(message, field)
      {
        return createMessage('warning', message, clone(field));
      },
      error: function(message, field)
      {
        return createMessage('error', message, clone(field));
      }
    },
    fix: {
      modifyLeader: function(record, value)
      {

        var result = {
          type: 'modifyLeader',
          oldValue: record.leader,
          newValue: value
        };

        record.leader = value;

        return result;

      },
      addField: function(record, field)
      {

        record.insertField(field);

        return {
          type: 'addField',
          field: clone(field)
        };

      },
      removeField: function(record, field)
      {

        record.fields.splice(record.fields.indexOf(field), 1);

        return {
          type: 'removeField',
          field: clone(field)
        };

      },
      addSubfield: function(field, subfield)
      {
        if (!field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a variable field');
        } else {

          field.subfields.push(subfield);
          
          return {
            type: 'addSubfield',
            field: clone(field),
            subfield: clone(subfield)
          };

        }
      },
      removeSubfield: function(field, subfield)
      {
        if (!field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a variable field');
        } else {

          field.subfields.splice(field.subfields.indexOf(subfield), 1);
          
          return {
            type: 'removeSubfield',
            field: clone(field),
            subfield: clone(subfield)
          };

        }
      },
      moveField: function(record, field, index_new)
      {

        var index_current = record.fields.indexOf(field);

        if (index_new >= record.fields.length) {
          record.fields.push(field);
        } else {
          record.fields.splice(index_new, 0, field);
        }

        if (index_current < index_new) {
          record.fields.splice(record.fields.indexOf(field), 1);
        } else {
          record.fields.splice(record.fields.lastIndexOf(field), 1);
        }

        return {
          'type': 'moveField',
          'field': clone(field),
          'old': index_current,
          'new': record.fields.indexOf(field)
        };

      },
      modifyFieldTag: function(field, tag)
      {

        var field_original = clone(field);

        field.tag = tag;

        return {
          'type': 'modifyField',
          'old': field_original,
          'new': clone(field)
        };

      },
      modifyFieldValue: function(field, value)
      {

        var field_original;

        if (field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a control field');
        } else {

          field_original = clone(field);
          field.value = value;

          return {
            'type': 'modifyField',
            'old': field_original,
            'new': clone(field)
          };

        }

      },
      modifySubfieldCode: function(field, subfield, code)
      {

        var field_original = clone(field);

        subfield.code = code;

        return {
          'type': 'modifyField',
          'old': field_original,
          'new': clone(field)
        };

      },
      modifySubfieldValue: function(field, subfield, value)
      {

        var field_original = clone(field);

        subfield.value = value;

        return {
          'type': 'modifyField',
          'old': field_original,
          'new': clone(field)
        };

      },
      modifySubfields: function(field, modifyCallback)
      {

        var field_original;

        if (!field.hasOwnProperty('subfields')) {
          throw new Error('Field is not a variable field');
        } else {

          field_original = clone(field);
          
          field.subfields.forEach(modifyCallback);
          
          return {
            'type': 'modifyField',
            'old': field_original,
            'new': clone(field)
          };

        }

      }
    }
  };

}

{'fraction_non_alphanumeric': 0.11413043478260869, 'fraction_numerical': 0.0025957170668397143, 'mean_word_length': 1.8948109884949518, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5058784', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3368, 'n_words': 899}