Source: polyfill/WeakSet.js

  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Watson Analytics
  4. //
  5. // (C) Copyright IBM Corp. 2015
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or
  8. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. module.exports = ( function( ObjectPolyfill, Symbol )
  10. {
  11. "use strict";
  12. // The property set by the first WeakSet that takes an object as key.
  13. // All WeakSets look at the same property
  14. var KEY_PROP = Symbol( "WeakSet" );
  15. var hOP = Object.prototype.hasOwnProperty;
  16. var nextId = 0;
  17. /**
  18. * Mimics the implementation of a native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet WeakSet}.
  19. * The only way to achieve this is by putting a property on the added value.
  20. * @class module:barejs/polyfill.WeakSet
  21. * @param {*} [_iterable]: Optional: an iterable whose values will be added to the WeakSet.
  22. */
  23. function WeakSet( _iterable )
  24. {
  25. if ( _iterable )
  26. {
  27. for ( var i = 0; i < _iterable.length; ++i )
  28. this.add( _iterable[i] );
  29. }
  30. ObjectPolyfill.defineProperty( this, "_values", { value: [] } );
  31. }
  32. return ( ObjectPolyfill.defineProperties( WeakSet.prototype,
  33. /** @lends module:barejs/polyfill.WeakSet# */
  34. {
  35. _values: { value: null },
  36. /**
  37. * Adds a value to a WeakSet.
  38. * @function
  39. * @param _value The value to add.
  40. * @returns {module:barejs/polyfill.WeakSet} The WeakSet (for chaining)
  41. */
  42. add: { value: function add( _value )
  43. {
  44. if ( !ObjectPolyfill.isObject( _value ) )
  45. throw new TypeError( "Invalid value used as WeakSet value" );
  46. if ( !hOP.call( _value, KEY_PROP ) )
  47. ObjectPolyfill.defineProperty( _value, KEY_PROP, { value: ++nextId } );
  48. if ( this._values.indexOf( _value[KEY_PROP] ) < 0 )
  49. this._values.push( _value[KEY_PROP] );
  50. return this;
  51. } },
  52. /**
  53. * Check if the WeakSet has an entry for the specified _value.
  54. * @function
  55. * @param {object} _value The object to check.
  56. * @returns {boolean} True if the WeakSet contains _value, false otherwise.
  57. */
  58. has: { value: function has( _value )
  59. {
  60. // If there is no WeakSet key property on the object, it has never been used as key for a WeakSet
  61. return ObjectPolyfill.isObject( _value ) && hOP.call( _value, KEY_PROP ) && ( this._values.indexOf( _value[KEY_PROP] ) >= 0 );
  62. } },
  63. /**
  64. * Remove the value from the WeakSet
  65. * @function
  66. * @param {object} _value The value to remove.
  67. * @returns {boolean} True if the value was deleted, false otherwise
  68. */
  69. "delete": { value: function _delete( _value )
  70. {
  71. var idx = -1;
  72. if ( ObjectPolyfill.isObject( _value ) && hOP.call( _value, KEY_PROP ) )
  73. idx = this._values.indexOf( _value[KEY_PROP] );
  74. if ( idx < 0 )
  75. return false;
  76. this._values.splice( idx, 1 );
  77. return true;
  78. } }
  79. } ), WeakSet );
  80. }( require( "./Object" ), require( "../Symbol" ) ) );