<commit_before>var Segment = require('./segment');
var Field = require('./field');
var parser = require('L7');

module.exports = function (options) {
    if (!options || typeof options !== 'object') {
        throw new Error('Must define options.');
    }
    if (!options.messageType || !options.messageEvent) {
        throw new Error('Must define message type and message event');
    }

    options.delimiters = options.delimiters || {};

    this.segments = [];
    this.delimiters = {
        segment: options.delimiters.segment || '\r',
        field: options.delimiters.field || '|',
        component: options.delimiters.component || '^',
        repeat: options.delimiters.repeat || '~',
        escape: options.delimiters.escape || '\\',
        subComponent: options.delimiters.subComponent || '&'
    };

    addMessageHeader.bind(this)(options);

    this.add = function(segment) {
        var segmentName = segment.fields[0].repeats[0][0];

        if (segmentName === 'MSH') {
            throw new Error('Cannot add another message header. One is automatically added.');
        }

        this.segments.push(segment);
    };

    this.toString = function() {
        var segmentStrings = [];

        for (var i in this.segments) {
            segmentStrings.push(buildSegment.bind(this)(this.segments[i]));
        }

        return segmentStrings.join(this.delimiters.segment);
    };

    this.toQuery = function() {
        return parser.parse(this.toString());
    };

    function buildSegment(segment) {
        var fieldList = [];
        for (var i in segment.fields) {
            fieldList.push(buildField.bind(this)(segment.fields[i]));
        }
        return fieldList.join(this.delimiters.field);
    }

    function buildField(field) {
        var fieldStrings = [];
        for (var repeat in field.repeats) {
            fieldStrings.push(field.repeats[repeat].join(this.delimiters.component));
        }
        return fieldStrings.join(this.delimiters.repeat);
    }

    function addMessageHeader(options) {
        var segment = new Segment('MSH');
        segment.update(1, this.delimiters.component + 
            this.delimiters.repeat +
            this.delimiters.escape +
            this.delimiters.subComponent);
        segment.update(2, options.sendingApplication || '');
        segment.update(3, options.sendingFacility || '');
        segment.update(4, options.receivingApplication || '');
        segment.update(5, options.receivingFacility || '');

        var timestamp = getTimestamp();
        segment.update(6, timestamp);

        var messageTypeField = new Field();
        messageTypeField.update(0, options.messageType);
        messageTypeField.update(1, options.messageEvent);

        segment.update(8, messageTypeField);
        segment.update(9, options.messageId || '');
        segment.update(10, 'D');
        segment.update(11, options.version || '2.3');
        this.segments.push(segment);

        if (options.eventSegment === true) {
            addEventSegment.bind(this)(options.messageEvent, options.timestamp);
        }
    }

    function addEventSegment(event, timestamp) {
        var segment = new Segment('EVN');
        segment.update(1, event);
        segment.update(2, timestamp);
        this.segments.push(segment);
    }

    function getTimestamp() {
        var d = new Date();
        return d.getFullYear() + 
            ('0' + (d.getMonth() + 1)).slice(-2) +
            ('0' + d.getDate()).slice(-2) +
            ('0' + d.getHours()).slice(-2) +
            ('0' + d.getMinutes()).slice(-2);<commit_msg>Updated date to add 10 seconds in between each.<commit_after>var Segment = require('./segment');
var Field = require('./field');
var parser = require('L7');
var date = new Date();

module.exports = function (options) {
    if (!options || typeof options !== 'object') {
        throw new Error('Must define options.');
    }
    if (!options.messageType || !options.messageEvent) {
        throw new Error('Must define message type and message event');
    }

    options.delimiters = options.delimiters || {};

    this.segments = [];
    this.delimiters = {
        segment: options.delimiters.segment || '\r',
        field: options.delimiters.field || '|',
        component: options.delimiters.component || '^',
        repeat: options.delimiters.repeat || '~',
        escape: options.delimiters.escape || '\\',
        subComponent: options.delimiters.subComponent || '&'
    };

    addMessageHeader.bind(this)(options);

    this.add = function(segment) {
        var segmentName = segment.fields[0].repeats[0][0];

        if (segmentName === 'MSH') {
            throw new Error('Cannot add another message header. One is automatically added.');
        }

        this.segments.push(segment);
    };

    this.toString = function() {
        var segmentStrings = [];

        for (var i in this.segments) {
            segmentStrings.push(buildSegment.bind(this)(this.segments[i]));
        }

        return segmentStrings.join(this.delimiters.segment);
    };

    this.toQuery = function() {
        return parser.parse(this.toString());
    };

    function buildSegment(segment) {
        var fieldList = [];
        for (var i in segment.fields) {
            fieldList.push(buildField.bind(this)(segment.fields[i]));
        }
        return fieldList.join(this.delimiters.field);
    }

    function buildField(field) {
        var fieldStrings = [];
        for (var repeat in field.repeats) {
            fieldStrings.push(field.repeats[repeat].join(this.delimiters.component));
        }
        return fieldStrings.join(this.delimiters.repeat);
    }

    function addMessageHeader(options) {
        var segment = new Segment('MSH');
        segment.update(1, this.delimiters.component + 
            this.delimiters.repeat +
            this.delimiters.escape +
            this.delimiters.subComponent);
        segment.update(2, options.sendingApplication || '');
        segment.update(3, options.sendingFacility || '');
        segment.update(4, options.receivingApplication || '');
        segment.update(5, options.receivingFacility || '');

        var timestamp = getTimestamp();
        segment.update(6, timestamp);

        var messageTypeField = new Field();
        messageTypeField.update(0, options.messageType);
        messageTypeField.update(1, options.messageEvent);

        segment.update(8, messageTypeField);
        segment.update(9, options.messageId || '');
        segment.update(10, 'D');
        segment.update(11, options.version || '2.3');
        this.segments.push(segment);

        if (options.eventSegment === true) {
            addEventSegment.bind(this)(options.messageEvent, options.timestamp);
        }
    }

    function addEventSegment(event, timestamp) {
        var segment = new Segment('EVN');
        segment.update(1, event);
        segment.update(2, timestamp);
        this.segments.push(segment);
    }

    function getTimestamp() {
        date.setSeconds(date.getSeconds() + 10);

        return date.getFullYear() + 
            ('0' + (date.getMonth() + 1)).slice(-2) +
            ('0' + date.getDate()).slice(-2) +
            ('0' + date.getHours()).slice(-2) +
            ('0' + date.getMinutes()).slice(-2);
{'fraction_non_alphanumeric': 0.1455366922234392, 'fraction_numerical': 0.009036144578313253, 'mean_word_length': 2.4490084985835696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2837938', 'n_tokens_mistral': 2100, 'n_tokens_neox': 2046, 'n_words': 385}