all files / semantic-graphql/src/ ArrayKeyedMap.js

100% Statements 7/7
100% Branches 0/0
100% Functions 3/3
100% Lines 6/6
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                18×                                                          
/*
  A Map that accepts unordered arrays of strings as its keys.
 
  const myMap = new ArrayKeyedMap();
  myMap.set(['foo', 'bar'], 'somevalue');
  myMap.get(['bar', 'foo']); // 'somevalue'
*/
 
const sep = Math.random().toString();
const hash = array => array.sort().join(sep);
// const unhash = key => key.split(sep);
 
class ArrayKeyedMap extends Map {
 
  get(array) {
    return super.get(hash(array));
  }
 
  set(array, value) {
    return super.set(hash(array), value);
  }
 
  has(array) {
    return super.has(hash(array));
  }
 
  /*
  delete(array) {
    return super.delete(hash(array));
  }
 
  forEach(cb, thisArg) {
    return super.forEach((value, key, that) => cb(value, unhash(key), that), thisArg);
  }
 
  map(cb, thisArg) {
    return super.map((value, key, that) => cb(value, unhash(key), that), thisArg);
  }
  */
}
 
module.exports = ArrayKeyedMap;