@naturalcycles/js-lib 14.192.1 → 14.193.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,16 @@ export declare function _randomInt(minIncl: number, maxIncl: number): number;
6
6
  * which is not reflected in the output type)
7
7
  */
8
8
  export declare function _randomArrayItem<T>(array: T[]): T;
9
+ /**
10
+ * Convenience function to "throttle" some code - run it less often.
11
+ *
12
+ * @example
13
+ *
14
+ * if (_runLessOften(10)) {
15
+ * // this code will run only 10% of the time
16
+ * }
17
+ */
18
+ export declare function _runLessOften(percent: number): boolean;
9
19
  /**
10
20
  * _inRange(-10, 1, 5) // false
11
21
  * _inRange(1, 1, 5) // true
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._round = exports._toPrecision = exports._toFixed = exports._sortNumbers = exports._clamp = exports._inRange = exports._randomArrayItem = exports._randomInt = void 0;
3
+ exports._round = exports._toPrecision = exports._toFixed = exports._sortNumbers = exports._clamp = exports._inRange = exports._runLessOften = exports._randomArrayItem = exports._randomInt = void 0;
4
4
  function _randomInt(minIncl, maxIncl) {
5
5
  return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl);
6
6
  }
@@ -14,6 +14,19 @@ function _randomArrayItem(array) {
14
14
  return array[_randomInt(0, array.length - 1)];
15
15
  }
16
16
  exports._randomArrayItem = _randomArrayItem;
17
+ /**
18
+ * Convenience function to "throttle" some code - run it less often.
19
+ *
20
+ * @example
21
+ *
22
+ * if (_runLessOften(10)) {
23
+ * // this code will run only 10% of the time
24
+ * }
25
+ */
26
+ function _runLessOften(percent) {
27
+ return Math.random() * 100 < percent;
28
+ }
29
+ exports._runLessOften = _runLessOften;
17
30
  // todo: _.random to support floats
18
31
  /**
19
32
  * _inRange(-10, 1, 5) // false
@@ -9,6 +9,18 @@ export function _randomInt(minIncl, maxIncl) {
9
9
  export function _randomArrayItem(array) {
10
10
  return array[_randomInt(0, array.length - 1)];
11
11
  }
12
+ /**
13
+ * Convenience function to "throttle" some code - run it less often.
14
+ *
15
+ * @example
16
+ *
17
+ * if (_runLessOften(10)) {
18
+ * // this code will run only 10% of the time
19
+ * }
20
+ */
21
+ export function _runLessOften(percent) {
22
+ return Math.random() * 100 < percent;
23
+ }
12
24
  // todo: _.random to support floats
13
25
  /**
14
26
  * _inRange(-10, 1, 5) // false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.192.1",
3
+ "version": "14.193.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -13,6 +13,19 @@ export function _randomArrayItem<T>(array: T[]): T {
13
13
  return array[_randomInt(0, array.length - 1)]!
14
14
  }
15
15
 
16
+ /**
17
+ * Convenience function to "throttle" some code - run it less often.
18
+ *
19
+ * @example
20
+ *
21
+ * if (_runLessOften(10)) {
22
+ * // this code will run only 10% of the time
23
+ * }
24
+ */
25
+ export function _runLessOften(percent: number): boolean {
26
+ return Math.random() * 100 < percent
27
+ }
28
+
16
29
  // todo: _.random to support floats
17
30
 
18
31
  /**