parseShardPair.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.parseShardPair = void 0;
  6. /**
  7. * Copyright (c) Meta Platforms, Inc. and affiliates.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. const parseShardPair = pair => {
  13. const shardPair = pair
  14. .split('/')
  15. .filter(d => /^\d+$/.test(d))
  16. .map(d => parseInt(d, 10))
  17. .filter(shard => !Number.isNaN(shard));
  18. const [shardIndex, shardCount] = shardPair;
  19. if (shardPair.length !== 2) {
  20. throw new Error(
  21. 'The shard option requires a string in the format of <n>/<m>.'
  22. );
  23. }
  24. if (shardIndex === 0 || shardCount === 0) {
  25. throw new Error(
  26. 'The shard option requires 1-based values, received 0 or lower in the pair.'
  27. );
  28. }
  29. if (shardIndex > shardCount) {
  30. throw new Error(
  31. 'The shard option <n>/<m> requires <n> to be lower or equal than <m>.'
  32. );
  33. }
  34. return {
  35. shardCount,
  36. shardIndex
  37. };
  38. };
  39. exports.parseShardPair = parseShardPair;