<commit_before>const randInt = (max) =>
  Math.floor(Math.random() * Math.floor(max));

const alphabet = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');

const randStr = (len = 3) => {
  let str = '';
  while(str.length < len) {
    str = `${str}${alphabet[randInt(alphabet.length - 1)]}`;
  }

  return str;
};

const genKey = (numProps, obj) => {
  const hashLen = Math.ceil(numProps / alphabet.length);
  let h = randStr(hashLen);
  const isTaken = (k, obj) => {
    if (obj instanceof Map) {
      return obj.has(k);
    }

    return h in obj;
  }

  while (isTaken(h, obj)) {
    h = randStr(hashLen);
  }

  return h;
};

const setMap = {
  description: 'Map.set()',
  f: (d) => {
    const m = new Map();
    d.forEach((dp) => m.set(genKey(d.length, m), dp));
    return m;
  }
};

const createMap = {
  description: 'new Map([props])',
  f: (d) => {
    return new Map(d.map((dp, i, arr) => [genKey(d.length, arr), dp]));
  }
};

const setObject = {
  description: '{}.prop =',
  f: (d) => {
    const m = {};
    d.forEach((dp) => m[genKey(d.length, m)] = dp);
    return m;
  }
};

const defineProperty = {
  description: 'Object.defineProperty({}, prop, desc)',
  f: (d) => {
    const m = {};
    d.forEach((dp) =>
      Object.defineProperty(m, genKey(d.length, m), {
        value: dp,
        enumerable: true
      })
    );
    return m;
  }
};

const defineProperties = {
  description: 'Object.defineProperties({}, props)',
  f: (d) => {
    const m = {};
    Object.defineProperties(m, d.reduce((props, dp) =>
      Object.assign(props, {
        [genKey(d.length, props)]: {
          value: dp,
          enumerable: true
        }
      }), {}));
    return m;
  }
};

const spread = {
  description: '{ ...props }',
  f: (d) => ({
    ...d
  })
};

module.exports = {
  name: 'map:creation',
  description: {
    long: 'Object literal vs. Map: creating a map.',
    short: 'Object literal vs. Map creation.'
  },
  functions: [
    createMap,
    defineProperty,
    defineProperties,
    setMap,
    setObject,
    spread
  ],
  testDataType: 'array'
};
<commit_msg>Pin property names to element indices.<commit_after>const setMap = {
  description: 'Map.set()',
  f: (d) => {
    const m = new Map();
    d.forEach((dp, i) => m.set(i, dp));
    return m;
  }
};

const createMap = {
  description: 'new Map([props])',
  f: (d) => {
    return new Map(d.map((dp, i, arr) => [i, dp]));
  }
};

const setObject = {
  description: '{}.prop =',
  f: (d) => {
    const m = {};
    d.forEach((dp, i) => m[i] = dp);
    return m;
  }
};

const defineProperty = {
  description: 'Object.defineProperty({}, prop, desc)',
  f: (d) => {
    const m = {};
    d.forEach((dp, i) =>
      Object.defineProperty(m, i, {
        value: dp,
        enumerable: true
      })
    );
    return m;
  }
};

const defineProperties = {
  description: 'Object.defineProperties({}, props)',
  f: (d) => {
    const m = {};
    Object.defineProperties(m, d.reduce((props, dp, i) =>
      Object.assign(props, {
        [i]: {
          value: dp,
          enumerable: true
        }
      }), {}));
    return m;
  }
};

const spread = {
  description: '{ ...props }',
  f: (d) => ({
    ...d
  })
};

module.exports = {
  name: 'map:creation',
  description: {
    long: 'Object literal vs. Map: creating a map.',
    short: 'Object literal vs. Map creation.'
  },
  functions: [
    createMap,
    defineProperty,
    defineProperties,
    setMap,
    setObject,
    spread
  ],
  testDataType: 'array'
};

{'fraction_non_alphanumeric': 0.19135104578858111, 'fraction_numerical': 0.0033917467495760316, 'mean_word_length': 2.4325897187196897, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 29, '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': '1667459', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1289, 'n_words': 297}