<commit_before>import cleanObject from 'driveback-utils/cleanObject';
import { getProp } from 'driveback-utils/dotProp';
import each from 'driveback-utils/each';
import { warn } from 'driveback-utils/safeConsole';
import {
  VIEWED_PAGE,
  VIEWED_PRODUCT_DETAIL,
  COMPLETED_TRANSACTION,
  REFUNDED_TRANSACTION,
  VIEWED_CART,
  ADDED_PRODUCT,
  REMOVED_PRODUCT,
  VIEWED_CHECKOUT_STEP,
  COMPLETED_CHECKOUT_STEP,
  SEARCHED_PRODUCTS,
  VIEWED_PRODUCT_LISTING,
  VIEWED_CAMPAIGN,
  CLICKED_CAMPAIGN,
  VIEWED_PRODUCT,
  CLICKED_PRODUCT,
  VIEWED_EXPERIMENT,
} from './../../events/semanticEvents';

const CUSTOM_TYPE_NUMERIC = 'number';
const CUSTOM_TYPE_STRING = 'string';

export const pageProps = [
  'name',
  'category',
  'type',
  'breadcrumb',
  'url',
  'path',
  'queryString',
  'title',
  'hash',
];

export const websiteProps = [
  'type',
  'region',
  'regionId',
  'language',
  'currency',
  'environment',
];

export const productProps = [
  'id',
  'name',
  'currency',
  'unitSalePrice',
  'unitPrice',
  'category',
  'categoryId',
  'url',
  'imageUrl',
  'thumbnailUrl',
  'manufacturer',
  'skuCode',
  'stock',
  'voucher',
  'color',
  'size',
  'customDimensions',
  'customMetrics',
];

export const listingProps = [
  'listId',
  'listName',
  'categoryId',
  'category',
  'query',
  'resultCount',
  'pagesCount',
  'currentPage',
  'sortBy',
  'layout',
];

const lineItemProps = [
  ...productProps.map(productProp => ['product', productProp].join('.')),
  'quantity',
  'subtotal',
];

export const cartProps = [
  ...lineItemProps.map(lineItemProp => ['lineItems[]', lineItemProp].join('.')),
  'id',
  'total',
  'subtotal',
  'currency',
  'voucher',
  'voucherDiscount',
  'shippingCost',
  'paymentMethod',
];

export const transactionProps = [
  ...lineItemProps.map(lineItemProp => ['lineItems[]', lineItemProp].join('.')),
  'total',
  'subtotal',
  'currency',
  'voucher',
  'voucherDiscount',
  'shippingCost',
  'paymentMethod',
  'orderId',
  'isFirst',
];

export const campaignProps = [
  'id',
  'name',
  'category',
  'subcategory',
  'design',
  'position',
];

export const listItemProps = [
  ...productProps.map(productProp => ['product', productProp].join('.')),
  'position',
  'listId',
];

function extractCustoms(source, variableMapping, type) {
  const values = [];
  each(variableMapping, (key, sourceProp) => {
    let value = getProp(source, sourceProp);
    if (value !== undefined) {
      if (type === CUSTOM_TYPE_NUMERIC && typeof value !== 'number') {
        value = Number(value);
      } else if (type === CUSTOM_TYPE_STRING && typeof value !== 'string') {
        value = value.toString();
      }
      values.push({
        name: key,
        value,
      });
    }
  });
  return values;
}

const filterObject = (obj, propsSet, restrictedProps = []) => {
  const filteredObject = {};
  propsSet.forEach((prop) => {
    if (obj[prop] !== undefined && restrictedProps.indexOf(prop) < 0) {
      filteredObject[prop] = obj[prop];
    }
  });
  return filteredObject;
};

class Filters {
  constructor(dimensions, metrics, productDimensions, productMetrics) {
    this.dimensions = dimensions;
    this.metrics = metrics;
    this.productDimensions = productDimensions;
    this.productMetrics = productMetrics;
  }

  filterEventHit(event) {
    const mapping = {
      [VIEWED_PAGE]: this.filterViewedPage.bind(this),
      [VIEWED_PRODUCT_DETAIL]: this.filterViewedProductDetail.bind(this),
      [ADDED_PRODUCT]: this.filterAddedProduct.bind(this),
      [REMOVED_PRODUCT]: this.filterRemovedProduct.bind(this),
      [VIEWED_CART]: this.filterViewedCart.bind(this),
      [COMPLETED_TRANSACTION]: this.filterCompletedTransaction.bind(this),
      [REFUNDED_TRANSACTION]: this.filterRefundedTransaction.bind(this),
      [VIEWED_CHECKOUT_STEP]: this.filterViewedCheckoutStep.bind(this),
      [COMPLETED_CHECKOUT_STEP]: this.filterCompeltedCheckoutStep.bind(this),
      [VIEWED_PRODUCT_LISTING]: this.filterViewedProductListing.bind(this),
      [SEARCHED_PRODUCTS]: this.filterSearchedProducts.bind(this),
      [VIEWED_CAMPAIGN]: this.filterViewedCampaign.bind(this),
      [CLICKED_CAMPAIGN]: this.filterClickedCampaign.bind(this),
      [VIEWED_PRODUCT]: this.filterViewedProduct.bind(this),
      [CLICKED_PRODUCT]: this.filterClickedProduct.bind(this),
      [VIEWED_EXPERIMENT]: this.filterViewedExperiment.bind(this),
    };

    if (event.name && mapping[event.name]) {
      return mapping[event.name](event);
    }
    return this.filterCommonEvent(event);
  }

  filterProduct(product = {}) {
    const customDimensions = extractCustoms(product, this.productDimensions, CUSTOM_TYPE_STRING);
    const customMetrics = extractCustoms(product, this.productMetrics, CUSTOM_TYPE_NUMERIC);

    return filterObject(
      {
        ...product,
        id: (product.id) ? String(product.id) : undefined,
        categoryId: (product.categoryId) ? String(product.categoryId) : undefined,
        category: (product.category && Array.isArray(product.category)) ?
          product.category.join('/') : product.category,
        unitPrice: (product.unitPrice) ? Number(product.unitPrice) : undefined,
        unitSalePrice: (product.unitSalePrice) ? Number(product.unitSalePrice) : undefined,
        manufacturer: (product.manufacturer) ? product.manufacturer : product.brand,
        customDimensions,<commit_msg>streaming skuCode filtering<commit_after>import cleanObject from 'driveback-utils/cleanObject';
import { getProp } from 'driveback-utils/dotProp';
import each from 'driveback-utils/each';
import { warn } from 'driveback-utils/safeConsole';
import {
  VIEWED_PAGE,
  VIEWED_PRODUCT_DETAIL,
  COMPLETED_TRANSACTION,
  REFUNDED_TRANSACTION,
  VIEWED_CART,
  ADDED_PRODUCT,
  REMOVED_PRODUCT,
  VIEWED_CHECKOUT_STEP,
  COMPLETED_CHECKOUT_STEP,
  SEARCHED_PRODUCTS,
  VIEWED_PRODUCT_LISTING,
  VIEWED_CAMPAIGN,
  CLICKED_CAMPAIGN,
  VIEWED_PRODUCT,
  CLICKED_PRODUCT,
  VIEWED_EXPERIMENT,
} from './../../events/semanticEvents';

const CUSTOM_TYPE_NUMERIC = 'number';
const CUSTOM_TYPE_STRING = 'string';

export const pageProps = [
  'name',
  'category',
  'type',
  'breadcrumb',
  'url',
  'path',
  'queryString',
  'title',
  'hash',
];

export const websiteProps = [
  'type',
  'region',
  'regionId',
  'language',
  'currency',
  'environment',
];

export const productProps = [
  'id',
  'name',
  'currency',
  'unitSalePrice',
  'unitPrice',
  'category',
  'categoryId',
  'url',
  'imageUrl',
  'thumbnailUrl',
  'manufacturer',
  'skuCode',
  'stock',
  'voucher',
  'color',
  'size',
  'customDimensions',
  'customMetrics',
];

export const listingProps = [
  'listId',
  'listName',
  'categoryId',
  'category',
  'query',
  'resultCount',
  'pagesCount',
  'currentPage',
  'sortBy',
  'layout',
];

const lineItemProps = [
  ...productProps.map(productProp => ['product', productProp].join('.')),
  'quantity',
  'subtotal',
];

export const cartProps = [
  ...lineItemProps.map(lineItemProp => ['lineItems[]', lineItemProp].join('.')),
  'id',
  'total',
  'subtotal',
  'currency',
  'voucher',
  'voucherDiscount',
  'shippingCost',
  'paymentMethod',
];

export const transactionProps = [
  ...lineItemProps.map(lineItemProp => ['lineItems[]', lineItemProp].join('.')),
  'total',
  'subtotal',
  'currency',
  'voucher',
  'voucherDiscount',
  'shippingCost',
  'paymentMethod',
  'orderId',
  'isFirst',
];

export const campaignProps = [
  'id',
  'name',
  'category',
  'subcategory',
  'design',
  'position',
];

export const listItemProps = [
  ...productProps.map(productProp => ['product', productProp].join('.')),
  'position',
  'listId',
];

function extractCustoms(source, variableMapping, type) {
  const values = [];
  each(variableMapping, (key, sourceProp) => {
    let value = getProp(source, sourceProp);
    if (value !== undefined) {
      if (type === CUSTOM_TYPE_NUMERIC && typeof value !== 'number') {
        value = Number(value);
      } else if (type === CUSTOM_TYPE_STRING && typeof value !== 'string') {
        value = value.toString();
      }
      values.push({
        name: key,
        value,
      });
    }
  });
  return values;
}

const filterObject = (obj, propsSet, restrictedProps = []) => {
  const filteredObject = {};
  propsSet.forEach((prop) => {
    if (obj[prop] !== undefined && restrictedProps.indexOf(prop) < 0) {
      filteredObject[prop] = obj[prop];
    }
  });
  return filteredObject;
};

class Filters {
  constructor(dimensions, metrics, productDimensions, productMetrics) {
    this.dimensions = dimensions;
    this.metrics = metrics;
    this.productDimensions = productDimensions;
    this.productMetrics = productMetrics;
  }

  filterEventHit(event) {
    const mapping = {
      [VIEWED_PAGE]: this.filterViewedPage.bind(this),
      [VIEWED_PRODUCT_DETAIL]: this.filterViewedProductDetail.bind(this),
      [ADDED_PRODUCT]: this.filterAddedProduct.bind(this),
      [REMOVED_PRODUCT]: this.filterRemovedProduct.bind(this),
      [VIEWED_CART]: this.filterViewedCart.bind(this),
      [COMPLETED_TRANSACTION]: this.filterCompletedTransaction.bind(this),
      [REFUNDED_TRANSACTION]: this.filterRefundedTransaction.bind(this),
      [VIEWED_CHECKOUT_STEP]: this.filterViewedCheckoutStep.bind(this),
      [COMPLETED_CHECKOUT_STEP]: this.filterCompeltedCheckoutStep.bind(this),
      [VIEWED_PRODUCT_LISTING]: this.filterViewedProductListing.bind(this),
      [SEARCHED_PRODUCTS]: this.filterSearchedProducts.bind(this),
      [VIEWED_CAMPAIGN]: this.filterViewedCampaign.bind(this),
      [CLICKED_CAMPAIGN]: this.filterClickedCampaign.bind(this),
      [VIEWED_PRODUCT]: this.filterViewedProduct.bind(this),
      [CLICKED_PRODUCT]: this.filterClickedProduct.bind(this),
      [VIEWED_EXPERIMENT]: this.filterViewedExperiment.bind(this),
    };

    if (event.name && mapping[event.name]) {
      return mapping[event.name](event);
    }
    return this.filterCommonEvent(event);
  }

  filterProduct(product = {}) {
    const customDimensions = extractCustoms(product, this.productDimensions, CUSTOM_TYPE_STRING);
    const customMetrics = extractCustoms(product, this.productMetrics, CUSTOM_TYPE_NUMERIC);

    return filterObject(
      {
        ...product,
        id: (product.id) ? String(product.id) : undefined,
        skuCode: (product.skuCode) ? String(product.skuCode) : undefined,
        categoryId: (product.categoryId) ? String(product.categoryId) : undefined,
        category: (product.category && Array.isArray(product.category)) ?
          product.category.join('/') : product.category,
        unitPrice: (product.unitPrice) ? Number(product.unitPrice) : undefined,
        unitSalePrice: (product.unitSalePrice) ? Number(product.unitSalePrice) : undefined,
        manufacturer: (product.manufacturer) ? product.manufacturer : product.brand,
        customDimensions,
{'fraction_non_alphanumeric': 0.1474264705882353, 'fraction_numerical': 0.0001838235294117647, 'mean_word_length': 4.072727272727272, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '2550746', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3650, 'n_words': 607}