<commit_before>import {
  ensureArray,
  extractProperty,
  forEach,
  isArray,
  mapToArray,
  parseXml
} from './utils'
import {
  isHostRunning,
  isVmHvm,
  isVmRunning,
  parseDateTime
} from './xapi'

// ===================================================================

const {
  defineProperties,
  freeze
} = Object

function link (obj, prop, idField = '$id') {
  const dynamicValue = obj[`$${prop}`]
  if (dynamicValue == null) {
    return dynamicValue // Properly handles null and undefined.
  }

  if (isArray(dynamicValue)) {
    return mapToArray(dynamicValue, idField)
  }

  return dynamicValue[idField]
}

// Parse a string date time to a Unix timestamp (in seconds).
//
// If there are no data or if the timestamp is 0, returns null.
function toTimestamp (date) {
  if (!date) {
    return null
  }

  const ms = parseDateTime(date).getTime()
  if (!ms) {
    return null
  }

  return Math.round(ms / 1000)
}<commit_msg>feat(toTimestamp): handle timestamps.<commit_after>import {
  ensureArray,
  extractProperty,
  forEach,
  isArray,
  mapToArray,
  parseXml
} from './utils'
import {
  isHostRunning,
  isVmHvm,
  isVmRunning,
  parseDateTime
} from './xapi'

// ===================================================================

const {
  defineProperties,
  freeze
} = Object

function link (obj, prop, idField = '$id') {
  const dynamicValue = obj[`$${prop}`]
  if (dynamicValue == null) {
    return dynamicValue // Properly handles null and undefined.
  }

  if (isArray(dynamicValue)) {
    return mapToArray(dynamicValue, idField)
  }

  return dynamicValue[idField]
}

// Parse a string date time to a Unix timestamp (in seconds).
//
// If the value is a number or can be converted as one, it is assumed
// to already be a timestamp and returned.
//
// If there are no data or if the timestamp is 0, returns null.
function toTimestamp (date) {
  if (!date) {
    return null
  }

  const timestamp = +date

  // Not NaN.
  if (timestamp === timestamp) { // eslint-disable-line no-self-compare
    return timestamp
  }

  const ms = parseDateTime(date)
  if (!ms) {
    return null
  }

  return Math.round(ms.getTime() / 1000)
}
{'fraction_non_alphanumeric': 0.17105871474803513, 'fraction_numerical': 0.004623208506703652, 'mean_word_length': 3.389452332657201, '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': '1970547', 'n_tokens_mistral': 698, 'n_tokens_neox': 669, 'n_words': 210}