create-set.js 724 B

123456789101112131415161718192021222324252627
  1. "use strict";
  2. var typeOf = require("@sinonjs/commons").typeOf;
  3. // This helper makes it convenient to create Set instances from a
  4. // collection, an overcomes the shortcoming that IE11 doesn't support
  5. // collection arguments
  6. //
  7. // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
  8. function createSet(array) {
  9. if (arguments.length > 0 && !Array.isArray(array)) {
  10. throw new TypeError(
  11. "createSet can be called with either no arguments or an Array"
  12. );
  13. }
  14. var items = typeOf(array) === "array" ? array : [];
  15. var set = new Set();
  16. items.forEach(function(item) {
  17. set.add(item);
  18. });
  19. return set;
  20. }
  21. module.exports = createSet;