<commit_before>/* global CustomEvent, Event */

/**
 * Fires a DOM event.
 *
 * @param {Element} el Element to fire the event on.
 * @param {String} name Name of the event.
 * @param {Object} detail Custom data to pass as `detail` if the event is a Custom Event.
 */
module.exports.fireEvent = function (el, name, detail) {
  el.dispatchEvent(detail ? new CustomEvent(name, detail) : new Event(name));
};

/**
 * Throws an error given a message.
 *
 * @param {String} msg Error message.
 */
module.exports.error = function (msg) {
  throw new Error(msg);
};

/**
 * Emits a console warning given a message.
 *
 * @param {String} msg Warning message.
 */
module.exports.warn = function (msg) {
  console.warn(msg);
};

/**
 * Returns the default value of an attribute based on its attribute name.
 *
 * @param {String} attr The name of the attribute (e.g., the string `'position'`).
 * @returns {Object} The default value of the attribute.
 */
var getDefaultValue = function (attr) {
  // Special casing for attributes whose values get transformed to objects.
  if (attr === 'position' || attr === 'rotation' || attr === 'to') {
    return {x: 0, y: 0, z: 0};
  }

  if (attr === 'scale') {
    return {x: 1, y: 1, z: 1};
  }
};

/**
 * Given a coordinate in a string form "0 0 0"
 * It returns the coordinate parsed as an object
 * { x: 3, y: 4, z: -10} or the default value
 *
 * @param  {String} value        String to parse
 * @param  {Object} defaultValue Default value
 * @return {Object}              Parsed coordinate
 */
var parseCoordinate = module.exports.parseCoordinate =
function (value, defaultValue) {
  var def;
  var values = '';
  if (typeof value !== 'string') { return defaultValue; }
  if (typeof defaultValue === 'object') {
    if ('x' in defaultValue && 'y' in defaultValue && 'z' in defaultValue) {
      def = defaultValue;
    }
  }
  def = def || {x: 0, y: 0, z: 0};
  values = value.split(' ');
  return {
    x: parseFloat(values[0] || defaultValue.x),
    y: parseFloat(values[1] || defaultValue.y),
    z: parseFloat(values[2] || defaultValue.z)
  };
};

/**
 * Parse and transform an attribute value from its original string value.
 *
 * @param {String} attr The name of the attribute (e.g., the string `loop` for `loop="true"`).
 * @param {String} value The value of the attribute (e.g., the string `'true'` for `loop="true"`).
 * @param {(Boolean|Number|String|Object)} defaultValue The value to use if the attribute was missing or its value was empty (e.g., the boolean `false`).
 * @returns {(Boolean|Number|String|Object)} The parsed and coerced value of the attribute (e.g., the boolean `true` for `loop="true"`).
 */
module.exports.parseAttributeString = function (attr, value, defaultValue) {
  if (!attr) { return; }

  var valueLower = typeof value === 'string' ? value.toLowerCase() : value;
  // Internal default value (for position, rotation, scale...)
  var internalDefault = getDefaultValue(attr);
  defaultValue = defaultValue !== undefined ? defaultValue : internalDefault;
  // Type inference logic
  // If there's not a passed default value we fall back
  // to the internal default value type.
  var type = typeof defaultValue;

  switch (type) {
    case 'object':
      return parseCoordinate(value, defaultValue);
    case 'number':
      return parseFloat(value);
    case 'boolean':
      return valueLower === 'true';
    default:
      return value !== undefined ? value : null;
  }<commit_msg>fix `parseAttributeString` to correctly return default values<commit_after>/* global CustomEvent, Event */

/**
 * Fires a DOM event.
 *
 * @param {Element} el Element to fire the event on.
 * @param {String} name Name of the event.
 * @param {Object} detail Custom data to pass as `detail` if the event is a Custom Event.
 */
module.exports.fireEvent = function (el, name, detail) {
  el.dispatchEvent(detail ? new CustomEvent(name, detail) : new Event(name));
};

/**
 * Throws an error given a message.
 *
 * @param {String} msg Error message.
 */
module.exports.error = function (msg) {
  throw new Error(msg);
};

/**
 * Emits a console warning given a message.
 *
 * @param {String} msg Warning message.
 */
module.exports.warn = function (msg) {
  console.warn(msg);
};

/**
 * Returns the default value of an attribute based on its attribute name.
 *
 * @param {String} attr The name of the attribute (e.g., the string `'position'`).
 * @returns {Object|null} The default value of the attribute.
 */
var getDefaultValue = function (attr) {
  // Special casing for attributes whose values get transformed to objects.
  if (attr === 'position' || attr === 'rotation' || attr === 'to') {
    return {x: 0, y: 0, z: 0};
  }

  if (attr === 'scale') {
    return {x: 1, y: 1, z: 1};
  }

  return null;
};

/**
 * Given a coordinate in a string form "0 0 0"
 * It returns the coordinate parsed as an object
 * {x: 3, y: 4, z: -10} or the default value.
 *
 * @param  {String} value        String to parse.
 * @param  {Object} defaultValue Default value.
 * @return {Object}              Parsed coordinate.
 */
var parseCoordinate = module.exports.parseCoordinate =
function (value, defaultValue) {
  var def;
  var values = '';
  if (typeof value !== 'string') { return defaultValue; }
  if (defaultValue === null) { return value; }
  if (typeof defaultValue === 'object') {
    if ('x' in defaultValue && 'y' in defaultValue && 'z' in defaultValue) {
      def = defaultValue;
    }
  }
  def = def || {x: 0, y: 0, z: 0};
  values = value.split(' ');
  return {
    x: parseFloat(values[0] || defaultValue.x),
    y: parseFloat(values[1] || defaultValue.y),
    z: parseFloat(values[2] || defaultValue.z)
  };
};

/**
 * Parse and transform an attribute value from its original string value.
 *
 * @param {String} attr The name of the attribute (e.g., the string `loop` for `loop="true"`).
 * @param {String} value The value of the attribute (e.g., the string `'true'` for `loop="true"`).
 * @param {(Boolean|Number|String|Object)} defaultValue The value to use if the attribute was missing or its value was empty (e.g., the boolean `false`).
 * @returns {(Boolean|Number|String|Object)} The parsed and coerced value of the attribute (e.g., the boolean `true` for `loop="true"`).
 */
module.exports.parseAttributeString = function (attr, value, defaultValue) {
  if (!attr) { return; }

  var valueLower = typeof value === 'string' ? value.toLowerCase() : value;
  // Internal default value (for position, rotation, scale...)
  var internalDefault = getDefaultValue(attr);
  defaultValue = defaultValue !== undefined ? defaultValue : internalDefault;
  // Type inference logic
  // If there's not a passed default value we fall back
  // to the internal default value type.
  var type = typeof defaultValue;

  switch (type) {
    case 'object':
      return parseCoordinate(value, defaultValue);
    case 'number':
      return parseFloat(value);
    case 'boolean':
      return valueLower === 'true';
    default:
      return value !== undefined ? value : defaultValue;
  }
{'fraction_non_alphanumeric': 0.14072494669509594, 'fraction_numerical': 0.005401563610518834, 'mean_word_length': 3.416823603264281, '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': '1597420', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2089, 'n_words': 813}