all files / src/ component.js

100% Statements 24/24
100% Branches 20/20
100% Functions 3/3
100% Lines 23/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                  94× 94× 94×     93× 93×   93× 93×       93× 93×   93×   155× 124×   110×   31×   31×                 27×       141× 141×     93×      
export default {
  name: 'i18next',
  functional: true,
  props: {
    tag: {
      type: String,
      default: 'span'
    },
    path: {
      type: String,
      required: true
    },
    options: {
      type: Object
    }
  },
  render(h, { props, data, children, parent }) {
    const i18next = parent.$i18n;
    const $t = parent.$t.bind(parent);
    if (!i18next || !$t) {
      return h(props.tag, data, children);
    }
 
    const { path } = props;
    const options = props.options || {};
 
    const REGEXP = i18next.i18next.services.interpolator.regexp;
    const i18nextOptions = {
      ...options,
      interpolation: { prefix: '#$?', suffix: '?$#' }
    };
    const format = $t(path, i18nextOptions);
    const tchildren = [];
 
    format.split(REGEXP).reduce((memo, match, index) => {
      let child;
      if (index % 2 === 0) {
        if (match.length === 0) return memo;
 
        child = match;
      } else {
        const place = match.trim();
        // eslint-disable-next-line no-restricted-globals
        if (isNaN(parseFloat(place)) || !isFinite(place)) {
          children.forEach(e => {
            if (
              !child &&
              e.data.attrs &&
              e.data.attrs.place &&
              e.data.attrs.place === place
            ) {
              child = e;
            }
          });
        } else {
          child = children[parseInt(match, 10)];
        }
      }
 
      memo.push(child);
      return memo;
    }, tchildren);
 
    return h(props.tag, data, tchildren);
  }
};