<commit_before>'use strict'

var Buffer = require('buffer').Buffer
  , path = require('path')
  , fs = require('fs')
  , segmentsDir = path.join(__dirname, 'segments')
  , utils = require('./utils')
  , types = {}

/*!
 * Load our segment types
 */
fs.readdirSync(segmentsDir)
  .forEach(function(file) {
    if (path.extname(file) === '.js') {
      var segType = path.basename(file).replace('.js', '').toUpperCase()
      types[segType] = require(path.join(__dirname, 'segments', file)).fields
    }
  })

module.exports = Segment

/**
 * Constructor
 */
function Segment(data, delimiters) {
  if (!(this instanceof Segment))
    return new Segment(data, delimiters)

  if (delimiters) {
    this.delimiters = delimiters
  } else {
    this.delimiters = {
      segment: '\r',
      field: '|',
      component: '^',
      subcomponent: '&',
      repetition: '~',
      escape: '\\'
    }
  }

  this.parsed = this.parse(data)
}

/**
 * Registers a new variant
 *
 * Required Keys:
 *
 *  - `name` {String} The variant name
 *  - `fields` {Array} The fields this variant exposes
 *
 * @param {Object} variant The variant to register
 * @api public
 */
Segment.registerVariant = function(variant) {
  if (variant === null || typeof variant !== 'object')
    throw new Error('Variant must be an object')

  if (!variant.hasOwnProperty('name'))
    throw new Error('Variant must have a name')

  if (!variant.hasOwnProperty('fields'))
    throw new Error('Variant must have fields')

  var name = variant.name.toUpperCase()
  if (types.hasOwnProperty(name)) {
    types[name+'_'] = types[name]
  }
  types[name] = variant.fields
}

/**
 * Parses _data_ as a hl7 segment
 *
 * @param {Buffer|String} data The segment
 * @api public
 */
Segment.prototype.parse = function(data) {
  if (data && data.length > 1) { // account for trailing \r<commit_msg>segment: use TypeError for type check in registerVariant<commit_after>'use strict'

var Buffer = require('buffer').Buffer
  , path = require('path')
  , fs = require('fs')
  , segmentsDir = path.join(__dirname, 'segments')
  , utils = require('./utils')
  , types = {}

/*!
 * Load our segment types
 */
fs.readdirSync(segmentsDir)
  .forEach(function(file) {
    if (path.extname(file) === '.js') {
      var segType = path.basename(file).replace('.js', '').toUpperCase()
      types[segType] = require(path.join(__dirname, 'segments', file)).fields
    }
  })

module.exports = Segment

/**
 * Constructor
 */
function Segment(data, delimiters) {
  if (!(this instanceof Segment))
    return new Segment(data, delimiters)

  if (delimiters) {
    this.delimiters = delimiters
  } else {
    this.delimiters = {
      segment: '\r',
      field: '|',
      component: '^',
      subcomponent: '&',
      repetition: '~',
      escape: '\\'
    }
  }

  this.parsed = this.parse(data)
}

/**
 * Registers a new variant
 *
 * Required Keys:
 *
 *  - `name` {String} The variant name
 *  - `fields` {Array} The fields this variant exposes
 *
 * @param {Object} variant The variant to register
 * @api public
 */
Segment.registerVariant = function(variant) {
  if (variant === null || typeof variant !== 'object')
    throw new TypeError('Variant must be an object')

  if (!variant.hasOwnProperty('name'))
    throw new Error('Variant must have a name')

  if (!variant.hasOwnProperty('fields'))
    throw new Error('Variant must have fields')

  var name = variant.name.toUpperCase()
  if (types.hasOwnProperty(name)) {
    types[name+'_'] = types[name]
  }
  types[name] = variant.fields
}

/**
 * Parses _data_ as a hl7 segment
 *
 * @param {Buffer|String} data The segment
 * @api public
 */
Segment.prototype.parse = function(data) {
  if (data && data.length > 1) { // account for trailing \r
{'fraction_non_alphanumeric': 0.15941255006675567, 'fraction_numerical': 0.001068090787716956, 'mean_word_length': 3.276255707762557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '55459', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1201, 'n_words': 310}