Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

83 rindas
2.1 KiB

  1. import {
  2. AdderService,
  3. AdderServiceImpl,
  4. ArgumentOutOfRangeError,
  5. InvalidArgumentTypeError,
  6. } from './adder.service';
  7. import { CommandHandler } from '../../packages/cli-wrapper';
  8. export interface AdderController {
  9. addNumbers: CommandHandler;
  10. subtractNumbers: CommandHandler;
  11. }
  12. export class AdderControllerImpl implements AdderController {
  13. constructor(
  14. private readonly adderService: AdderService = new AdderServiceImpl(),
  15. ) {
  16. // noop
  17. }
  18. readonly addNumbers: CommandHandler = (params) => {
  19. if (!params.interactive) {
  20. const checkArgs = params.args as Record<string, unknown>;
  21. if (typeof checkArgs.a === 'undefined') {
  22. params.logger.error('Missing required argument: a');
  23. return -1;
  24. }
  25. if (typeof checkArgs.b === 'undefined') {
  26. params.logger.error('Missing required argument: b');
  27. return -1;
  28. }
  29. }
  30. const { a, b } = params.args;
  31. try {
  32. const response = this.adderService.addNumbers({ a: Number(a), b: Number(b) });
  33. params.logger.info(response);
  34. } catch (errorRaw) {
  35. const error = errorRaw as Error;
  36. params.logger.error(error.message);
  37. if (error instanceof InvalidArgumentTypeError) {
  38. return -1;
  39. }
  40. if (error instanceof ArgumentOutOfRangeError) {
  41. return -2;
  42. }
  43. return -3;
  44. }
  45. return 0;
  46. }
  47. readonly subtractNumbers: CommandHandler = (params) => {
  48. if (!params.interactive) {
  49. const checkArgs = params.args as Record<string, unknown>;
  50. if (typeof checkArgs.a === 'undefined') {
  51. params.logger.error('Missing required argument: a');
  52. return -1;
  53. }
  54. if (typeof checkArgs.b === 'undefined') {
  55. params.logger.error('Missing required argument: b');
  56. return -1;
  57. }
  58. }
  59. const { a, b } = params.args;
  60. try {
  61. const response = this.adderService.addNumbers({ a: Number(a), b: -(Number(b)) });
  62. params.logger.info(response);
  63. } catch (errorRaw) {
  64. const error = errorRaw as Error;
  65. params.logger.error(error.message);
  66. if (error instanceof InvalidArgumentTypeError) {
  67. return -1;
  68. }
  69. if (error instanceof ArgumentOutOfRangeError) {
  70. return -2;
  71. }
  72. return -3;
  73. }
  74. return 0;
  75. }
  76. }