id-factory.ts 443 B

123456789101112131415
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. /** Builds id function returning ids within [firstId, maxId) */
  7. export function idFactory(firstId = 0, maxId = Number.MAX_SAFE_INTEGER) {
  8. let _nextId = firstId;
  9. return () => {
  10. const ret = _nextId;
  11. _nextId = (_nextId + 1) % maxId;
  12. return ret;
  13. };
  14. }