numbers.js 726 B

123456789101112131415161718192021
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export function clamp(value, min, max) {
  6. return Math.min(Math.max(value, min), max);
  7. }
  8. export class MovingAverage {
  9. constructor() {
  10. this._n = 1;
  11. this._val = 0;
  12. }
  13. update(value) {
  14. this._val = this._val + (value - this._val) / this._n;
  15. this._n += 1;
  16. return this;
  17. }
  18. get value() {
  19. return this._val;
  20. }
  21. }