Graph database Analysis of the Steam Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.9 KiB

  1. ;(function() {
  2. 'use strict';
  3. /**
  4. * This utils aims to facilitate the manipulation of each instance setting.
  5. * Using a function instead of an object brings two main advantages: First,
  6. * it will be easier in the future to catch settings updates through a
  7. * function than an object. Second, giving it a full object will "merge" it
  8. * to the settings object properly, keeping us to have to always add a loop.
  9. *
  10. * @return {configurable} The "settings" function.
  11. */
  12. var configurable = function() {
  13. var i,
  14. l,
  15. data = {},
  16. datas = Array.prototype.slice.call(arguments, 0);
  17. /**
  18. * The method to use to set or get any property of this instance.
  19. *
  20. * @param {string|object} a1 If it is a string and if a2 is undefined,
  21. * then it will return the corresponding
  22. * property. If it is a string and if a2 is
  23. * set, then it will set a2 as the property
  24. * corresponding to a1, and return this. If
  25. * it is an object, then each pair string +
  26. * object(or any other type) will be set as a
  27. * property.
  28. * @param {*?} a2 The new property corresponding to a1 if a1
  29. * is a string.
  30. * @return {*|configurable} Returns itself or the corresponding
  31. * property.
  32. *
  33. * Polymorphism:
  34. * *************
  35. * Here are some basic use examples:
  36. *
  37. * > settings = new configurable();
  38. * > settings('mySetting', 42);
  39. * > settings('mySetting'); // Logs: 42
  40. * > settings('mySetting', 123);
  41. * > settings('mySetting'); // Logs: 123
  42. * > settings({mySetting: 456});
  43. * > settings('mySetting'); // Logs: 456
  44. *
  45. * Also, it is possible to use the function as a fallback:
  46. * > settings({mySetting: 'abc'}, 'mySetting'); // Logs: 'abc'
  47. * > settings({hisSetting: 'abc'}, 'mySetting'); // Logs: 456
  48. */
  49. var settings = function(a1, a2) {
  50. var o,
  51. i,
  52. l,
  53. k;
  54. if (arguments.length === 1 && typeof a1 === 'string') {
  55. if (data[a1] !== undefined)
  56. return data[a1];
  57. for (i = 0, l = datas.length; i < l; i++)
  58. if (datas[i][a1] !== undefined)
  59. return datas[i][a1];
  60. return undefined;
  61. } else if (typeof a1 === 'object' && typeof a2 === 'string') {
  62. return (a1 || {})[a2] !== undefined ? a1[a2] : settings(a2);
  63. } else {
  64. o = (typeof a1 === 'object' && a2 === undefined) ? a1 : {};
  65. if (typeof a1 === 'string')
  66. o[a1] = a2;
  67. for (i = 0, k = Object.keys(o), l = k.length; i < l; i++)
  68. data[k[i]] = o[k[i]];
  69. return this;
  70. }
  71. };
  72. /**
  73. * This method returns a new configurable function, with new objects
  74. *
  75. * @param {object*} Any number of objects to search in.
  76. * @return {function} Returns the function. Check its documentation to know
  77. * more about how it works.
  78. */
  79. settings.embedObjects = function() {
  80. var args = datas.concat(
  81. data
  82. ).concat(
  83. Array.prototype.splice.call(arguments, 0)
  84. );
  85. return configurable.apply({}, args);
  86. };
  87. // Initialize
  88. for (i = 0, l = arguments.length; i < l; i++)
  89. settings(arguments[i]);
  90. return settings;
  91. };
  92. /**
  93. * EXPORT:
  94. * *******
  95. */
  96. if (typeof this.sigma !== 'undefined') {
  97. this.sigma.classes = this.sigma.classes || {};
  98. this.sigma.classes.configurable = configurable;
  99. } else if (typeof exports !== 'undefined') {
  100. if (typeof module !== 'undefined' && module.exports)
  101. exports = module.exports = configurable;
  102. exports.configurable = configurable;
  103. } else
  104. this.configurable = configurable;
  105. }).call(this);