<commit_before>// @flow

import React from 'react';
import { Transition } from 'react-transition-group';
import camelCase from 'lodash.camelcase';
import type {
  TransitionProps,
  TransitionConfig,
  TransitionStates,
} from '../types/index';

const getPrimitiveValue = (value, index): any =>
  Array.isArray(value) ? value[index] : value;

const naiveMemoize = (callback): Function => {
  const cache = {};

  return (...args: Array<any>): any => {
    const key = args.join('-');
    if (!cache.hasOwnProperty(key)) cache[key] = callback.apply(null, args);
    return cache[key];
  };
};

const getStyleString = (
  transition: string,
  currentStyle: string,
  style: string
): string =>
  transition === 'transform' && !!currentStyle
    ? `${currentStyle} ${style}`
    : style;

const transitionFactory = (
  transitionConfigs: Array<TransitionConfig>,
  staticStyles?: Object,
  defaultProps?: TransitionProps
) => {
  return class extends React.Component<TransitionProps> {
    static defaultProps = {
      timeout: 300,
      easing: 'ease-in-out',
      ...defaultProps,
    };

    constructor(props: TransitionProps) {
      super(props);
      const { timeout, easing, start, end } = props;

      // warm up cache
      this.getFinalStyle('entering', timeout, easing, start, end);
      this.getFinalStyle('entered', timeout, easing, start, end);
      this.getFinalStyle('exiting', timeout, easing, start, end);
      this.getFinalStyle('exited', timeout, easing, start, end);
    }

    getGlobalTimeout = naiveMemoize((timeout): number => {
      return Array.isArray(timeout) ? Math.max(...timeout) : timeout;
    });

    getTransitionProperty = (timeout, easing): string => {
      return transitionConfigs
        .map((config, index) => {
          const timeoutVal = getPrimitiveValue(timeout, index);
          const easingVal = getPrimitiveValue(easing, index);
          return `${config.transition} ${timeoutVal}ms ${easingVal}`;
        })
        .join(',');
    };

    getDefaultStyle = (timeout, easing, start): Object => {
      return {
        display: 'inline-block',
        transition: this.getTransitionProperty(timeout, easing),
        ...staticStyles,
        ...transitionConfigs.reduce((style, config, index) => {
          const startVal = getPrimitiveValue(start, index);
          const transitionName = camelCase(config.transition);

          style[transitionName] = getStyleString(
            transitionName,
            style[transitionName],
            config.getStartStyle(startVal)
          );
          return style;
        }, {}),
      };
    };

    getTransitionStates = (timeout, easing, start, end): TransitionStates => {
      return transitionConfigs.reduce(
        (styles, config, index) => {
          const startVal = getPrimitiveValue(start, index);
          const endVal = getPrimitiveValue(end, index);
          const transitionName = camelCase(config.transition);

          styles.entering[transitionName] = getStyleString(
            transitionName,
            styles.entering[transitionName],
            config.getStartStyle(startVal)
          );
          styles.entered[transitionName] = getStyleString(<commit_msg>fix(factory): don't have a default display style<commit_after>// @flow

import React from 'react';
import { Transition } from 'react-transition-group';
import camelCase from 'lodash.camelcase';
import type {
  TransitionProps,
  TransitionConfig,
  TransitionStates,
} from '../types/index';

const getPrimitiveValue = (value, index): any =>
  Array.isArray(value) ? value[index] : value;

const naiveMemoize = (callback): Function => {
  const cache = {};

  return (...args: Array<any>): any => {
    const key = args.join('-');
    if (!cache.hasOwnProperty(key)) cache[key] = callback.apply(null, args);
    return cache[key];
  };
};

const getStyleString = (
  transition: string,
  currentStyle: string,
  style: string
): string =>
  transition === 'transform' && !!currentStyle
    ? `${currentStyle} ${style}`
    : style;

const transitionFactory = (
  transitionConfigs: Array<TransitionConfig>,
  staticStyles?: Object,
  defaultProps?: TransitionProps
) => {
  return class extends React.Component<TransitionProps> {
    static defaultProps = {
      timeout: 300,
      easing: 'ease-in-out',
      ...defaultProps,
    };

    constructor(props: TransitionProps) {
      super(props);
      const { timeout, easing, start, end } = props;

      // warm up cache
      this.getFinalStyle('entering', timeout, easing, start, end);
      this.getFinalStyle('entered', timeout, easing, start, end);
      this.getFinalStyle('exiting', timeout, easing, start, end);
      this.getFinalStyle('exited', timeout, easing, start, end);
    }

    getGlobalTimeout = naiveMemoize((timeout): number => {
      return Array.isArray(timeout) ? Math.max(...timeout) : timeout;
    });

    getTransitionProperty = (timeout: number, easing: string): string => {
      return transitionConfigs
        .map((config, index) => {
          const timeoutVal = getPrimitiveValue(timeout, index);
          const easingVal = getPrimitiveValue(easing, index);
          return `${config.transition} ${timeoutVal}ms ${easingVal}`;
        })
        .join(',');
    };

    getDefaultStyle = (timeout: number, easing: string, start): Object => {
      return {
        transition: this.getTransitionProperty(timeout, easing),
        ...staticStyles,
        ...transitionConfigs.reduce((style, config, index) => {
          const startVal = getPrimitiveValue(start, index);
          const transitionName = camelCase(config.transition);

          style[transitionName] = getStyleString(
            transitionName,
            style[transitionName],
            config.getStartStyle(startVal)
          );
          return style;
        }, {}),
      };
    };

    getTransitionStates = (
      timeout: number,
      easing: string,
      start,
      end
    ): TransitionStates => {
      return transitionConfigs.reduce(
        (styles, config, index) => {
          const startVal = getPrimitiveValue(start, index);
          const endVal = getPrimitiveValue(end, index);
          const transitionName = camelCase(config.transition);

          styles.entering[transitionName] = getStyleString(
            transitionName,
            styles.entering[transitionName],
            config.getStartStyle(startVal)
          );
          styles.entered[transitionName] = getStyleString(
{'fraction_non_alphanumeric': 0.13020030816640987, 'fraction_numerical': 0.0009244992295839754, 'mean_word_length': 2.8914868105515588, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, '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': '5483893', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1782, 'n_words': 411}