Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. In typescript, we can create interface function which defined the parameters
  2. and return type. The snippet is below.
  3.  
  4. ```
  5. interface SearchFunc() {
  6. (...keywords?: string[]): PageRank
  7. }
  8. ```
  9.  
  10. The _SearhFunc_ interface defines that a search function should accept bunch of
  11. keywords and return the result in PageRank format.
  12.  
  13. Now we can define a search engine, that accept search query, and search function
  14. should be injected during engine construction.
  15.  
  16. ```
  17. class SearchEngine() {
  18. storage: PageStorage;
  19. searchFunc: SearchFunc;
  20.  
  21. constructor(func: SearchFunc) {
  22. this.searchFunc = func;
  23. }
  24.  
  25. query(keywords: string[]): PageRank {
  26. // ... internal process
  27. return searchFunc(keywords);
  28. }
  29. }
  30. ```
  31.  
  32. Let's take another problem, say we want to make income tax calculator that have
  33. two main functions:
  34.  
  35. 1. tax calculcation based on income
  36. 2. tax exemption based on which band
  37.  
  38. As we are unit test follower, we should define breaking unit test (in jest)
  39. first and then implement it with name _pph21_.
  40.  
  41. ```
  42. describe('tax calculation function', () => {
  43.  
  44. const {calc} = require('./pph21');
  45.  
  46. it('should calculate the tax based on bracket regulation', () => {
  47.  
  48. const monthlyIncome = 3500;
  49. const tax = 55;
  50.  
  51. expect(
  52. calc(12 * monthlyIncome)
  53. ).toBe(tax);
  54. })
  55. // .. other scenarios
  56. })
  57.  
  58. // implementation
  59.  
  60. interface CalcFunc {
  61. (taxableIncome: number): number;
  62. }
  63.  
  64. const pph21: CalcFunc = function (income): number {
  65. let tax = 0;
  66. // ... internal process
  67. return tax;
  68. }
  69. ```
  70.  
  71. Next we may see how the _ExemptFunc_ written in red-green approach.
  72.  
  73. ```
  74. describe('income exemption function', () => {
  75.  
  76. const {exempt} = require('./pph21');
  77.  
  78. it('should return exemption number based on band', () => {
  79. const band = 'K0';
  80. const exemption = 50;
  81.  
  82. expect(
  83. exempt(band)
  84. ).toBe(exemption);
  85. })
  86. // ... other scenarios
  87. })
  88.  
  89. // implementation
  90.  
  91. interface ExemptFunc {
  92. (band: string): number;
  93. }
  94.  
  95. const exemptPPH21: ExemptFunc = function(band): number {
  96. let exemption = 0;
  97. // ... internal process
  98. return exemption;
  99. }
  100. ```
  101.  
  102. Nice, to conclude the whole gist I would like to write the _TaxEngine_ with
  103. injected _CalcFunc_ and _ExemptFunc_.
  104.  
  105. ```
  106. class TaxEngine {
  107. calc: CalcFunc;
  108. exempt: ExemptFunc;
  109.  
  110. constructor(calc: CalcFunc, exempt: ExemptFunc) {
  111. this.calc = calc;
  112. this.exempt = exempt;
  113. }
  114. }
  115.  
  116. const {pph21, exemptPPH21} = require('./pph21');
  117. const pph21Engine = new TaxEngine(pph21, exemptPPH21);
  118. ```
  119.  
  120. Interested for more? please do contact me.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement