<commit_before>import R from 'ramda';

const makeStyleReducer = (key, isStore = false) => function styleReducer(styles) {
  let result = {};
  const storeKey = (isStore) ? 'props' : 'state';
  if (this[storeKey] && this[storeKey][key]) {
    result = this.styles.keys[key].reduce(
      (prev, cur) => {
        const p = prev;
        if (this.styles.styles[cur].hasOwnProperty(key)) {
          p[cur] = this.styles.styles[cur][key];
        }
        return p;
      }, {}
    );
  }
  return R.mergeWith(R.merge, styles, result);
};

const Stylizer = {
  assignStyles: (component, styles) => {
    const c = component;

    if (!c.styles) {
      c.styles = {
        styles,
        keys: {},
        getters: [],
      };
    }
    // assign default getter
    c.styles.getters.push(Stylizer.getters.normal.bind(c));
    // assign default getStyle method
    c.getStyle = Stylizer.funcs.getStyle.bind(c);
  },
  hoverable: (component, keys) => {
    const c = component;

    if (!c.styles) {
      Stylizer.assignStyles(c, {});
    }
    if (!c.state) {
      c.state = {};
    }
    c.state.hover = false;
    c.onMouseOver = Stylizer.funcs.onMouseOver;
    c.onMouseOut = Stylizer.funcs.onMouseOut;
    c.handleOver = c.onMouseOver.bind(c);
    c.handleOut = c.onMouseOut.bind(c);

    c.styles.keys.hover = keys;
    c.styles.getters.push(Stylizer.getters.hover.bind(c));
  },
  selectable: (component, keys) => {
    const c = component;
    if (!c.styles) {
      Stylizer.assignStyles(c, {});
    }
    if (!c.state) {
      c.state = {};
    }
    c.state.selected = false;

    c.styles.keys.selected = keys;
    c.styles.getters.push(Stylizer.getters.selected.bind(c));
  },
  getters: {
    normal(styles) {
      return Object.keys(styles).reduce(
        (prev, cur) => {
          const p = prev;
          p[cur] = ((styles[cur].hasOwnProperty('normal')) ? styles[cur].normal : styles[cur]);
          return p;
        }, {}
      );
    },
    hover: makeStyleReducer('hover'),
    selected: makeStyleReducer('selected', true),
  },<commit_msg>feat(stylizer): add method dependOnProp, that apply styles dependent on property value<commit_after>import R from 'ramda';

const makeStyleReducer = (key, isStore = false) => function styleReducer(styles) {
  let result = {};
  const storeKey = (isStore) ? 'props' : 'state';
  if (this[storeKey] && this[storeKey][key]) {
    const val = this[storeKey][key];
    result = this.styles.keys[key].reduce(
      (prev, cur) => {
        let p = prev;
        const hasStyles = this.styles.styles[cur].hasOwnProperty(key);
        if (hasStyles) {
          const hasPropStyle = (
            typeof this.styles.styles[cur][key] === 'object' &&
            this.styles.styles[cur][key].hasOwnProperty(val)
          );
          let updateWith;
          if (hasPropStyle) {
            updateWith = this.styles.styles[cur][key][val];
          } else {
            updateWith = this.styles.styles[cur][key];
          }

          p = R.assoc(cur, updateWith, prev);
        }
        return p;
      }, {}
    );
  }
  return R.mergeWith(R.merge, styles, result);
};

const Stylizer = {
  init: (component) => {
    const c = component;
    if (!c.styles) {
      Stylizer.assignStyles(c, {});
    }
    if (!c.state) {
      c.state = {};
    }
  },
  assignStyles: (component, styles) => {
    const c = component;

    if (!c.styles) {
      c.styles = {
        styles,
        keys: {},
        getters: [],
      };
    }
    // assign default getters
    c.styles.getters.push(Stylizer.getters.normal.bind(c));
    // assign default getStyle method
    c.getStyle = Stylizer.funcs.getStyle.bind(c);
  },
  hoverable: (component, keys) => {
    const c = component;
    Stylizer.init(c);
    c.state.hover = false;

    c.onMouseOver = Stylizer.funcs.onMouseOver;
    c.onMouseOut = Stylizer.funcs.onMouseOut;
    c.handleOver = c.onMouseOver.bind(c);
    c.handleOut = c.onMouseOut.bind(c);

    c.styles.keys.hover = keys;
    c.styles.getters.push(Stylizer.getters.hover.bind(c));
  },
  selectable: (component, keys) => {
    const c = component;
    Stylizer.init(c);
    c.state.selected = false;

    c.styles.keys.selected = keys;
    c.styles.getters.push(Stylizer.getters.selected.bind(c));
  },
  dependOnProp: (component, propName, keys) => {
    const c = component;
    Stylizer.init(c);

    c.styles.keys[propName] = keys;
    c.styles.getters.push(Stylizer.getters.dependOnProp(c, propName));
  },
  getters: {
    normal(styles) {
      return Object.keys(styles).reduce(
        (prev, cur) => {
          const p = prev;
          p[cur] = ((styles[cur].hasOwnProperty('normal')) ? styles[cur].normal : styles[cur]);
          return p;
        }, {}
      );
    },
    hover: makeStyleReducer('hover'),
    selected: makeStyleReducer('selected', true),
    dependOnProp: (component, propName) => makeStyleReducer(propName, true).bind(component),
  },
{'fraction_non_alphanumeric': 0.16103580821363545, 'fraction_numerical': 0.0, 'mean_word_length': 2.4261954261954264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '3178940', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1682, 'n_words': 299}