summaryrefslogtreecommitdiff
path: root/src/util.js
blob: 4e23d9d3dc758ac7efbf0282c7ca67e69e52745b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'use strict';


export function create(obj, proto=Object.prototype) {
  const props = Object.keys(obj)
    .map((key) => [ key, { value: obj[key], enumerable: true } ])
    .reduce((acc, [ key, value ]) => ({ ...acc, [key]: value }), {});

  return Object.create(proto, props);
};


export function random_choice(collection, r) {
  const idx = Math.floor(collection.length * r);
  return collection[idx];
}