Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

142 lignes
2.6 KiB

  1. //eslint-disable-next-line no-extend-native
  2. Object.defineProperty(Array.prototype, 'firstIndex', {
  3. enumerable: false,
  4. value: function (callback, thisArg) {
  5. let T = thisArg;
  6. let O = Object(this);
  7. let len = O.length >>> 0;
  8. let k = 0;
  9. while (k < len) {
  10. let kValue;
  11. if (k in O) {
  12. kValue = O[k];
  13. if (callback.call(T, kValue, k, O))
  14. return k;
  15. }
  16. k++;
  17. }
  18. return -1;
  19. }
  20. });
  21. //eslint-disable-next-line no-extend-native
  22. Object.defineProperty(Array.prototype, 'spliceWhere', {
  23. enumerable: false,
  24. value: function (callback, thisArg) {
  25. let T = thisArg;
  26. let O = Object(this);
  27. let len = O.length >>> 0;
  28. let k = 0;
  29. while (k < len) {
  30. let kValue;
  31. if (k in O) {
  32. kValue = O[k];
  33. if (callback.call(T, kValue, k, O)) {
  34. O.splice(k, 1);
  35. k--;
  36. }
  37. }
  38. k++;
  39. }
  40. }
  41. });
  42. //eslint-disable-next-line no-extend-native
  43. Object.defineProperty(Array.prototype, 'spliceFirstWhere', {
  44. enumerable: false,
  45. value: function (callback, thisArg) {
  46. let T = thisArg;
  47. let O = Object(this);
  48. let len = O.length >>> 0;
  49. let k = 0;
  50. while (k < len) {
  51. let kValue;
  52. if (k in O) {
  53. kValue = O[k];
  54. if (callback.call(T, kValue, k, O)) {
  55. O.splice(k, 1);
  56. return kValue;
  57. }
  58. }
  59. k++;
  60. }
  61. }
  62. });
  63. //eslint-disable-next-line no-extend-native
  64. Object.defineProperty(Object.prototype, 'has', {
  65. enumerable: false,
  66. value: function (prop) {
  67. //eslint-disable-next-line no-undefined
  68. return (this.hasOwnProperty(prop) && this[prop] !== undefined && this[prop] !== null);
  69. }
  70. });
  71. module.exports = {
  72. get2dArray: function (w, h, def) {
  73. def = def || 0;
  74. let result = [];
  75. for (let i = 0; i < w; i++) {
  76. let inner = [];
  77. for (let j = 0; j < h; j++) {
  78. if (def === 'array')
  79. inner.push([]);
  80. else
  81. inner.push(def);
  82. }
  83. result.push(inner);
  84. }
  85. return result;
  86. },
  87. randomKey: function (o) {
  88. let keys = Object.keys(o);
  89. let keyIndex = ~~(Math.random() * keys.length);
  90. let key = keys[keyIndex];
  91. return key;
  92. },
  93. getDeepProperty: function (obj, path) {
  94. let o = obj;
  95. let pLen = path.length;
  96. for (let i = 0; i < pLen; i++) {
  97. o = o[path[i]];
  98. if (!o)
  99. return null;
  100. }
  101. return o;
  102. },
  103. getGuid: function () {
  104. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  105. let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  106. return v.toString(16);
  107. });
  108. },
  109. //Only use this method for official logging. Temporary logs should use console.log
  110. // so those instances can be reported by eslint
  111. log: function (msg) {
  112. //eslint-disable-next-line no-console
  113. console.log(msg);
  114. }
  115. };