Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. class Stack<T> {
  2. private _intenal: T[]
  3. private _pointer: number = -1
  4.  
  5. constructor(size: number = 0) {
  6. this._intenal = new Array(size)
  7. }
  8.  
  9. get top(): T | undefined {
  10. return this._intenal[this._pointer]
  11. }
  12.  
  13. get empty(): boolean {
  14. return this._pointer === -1
  15. }
  16.  
  17. push(value: T): void {
  18. this._pointer++
  19. if (this._intenal.length > this._pointer) {
  20. this._intenal[this._pointer] = value
  21. } else {
  22. this._intenal.push(value)
  23. }
  24. }
  25.  
  26. pop(): T | undefined {
  27. return this._intenal[this._pointer--]
  28. }
  29.  
  30. clear(): void {
  31. this._pointer = -1
  32. }
  33. }
  34.  
  35. type StackItem =
  36. | {
  37. type: 'ARRAY'
  38. position: number
  39. size: number
  40. array: Array<unknown>
  41. }
  42. | {
  43. type: 'MAP_KEY' | 'MAP_VALUE'
  44. remains: number
  45. key: string | null
  46. map: Record<string, unknown>
  47. }
  48.  
  49. class Deserializer {
  50. private stack: Stack<StackItem>
  51. private value: unknown
  52.  
  53. constructor(depth: number = 8) {
  54. this.stack = new Stack(depth)
  55. }
  56.  
  57. getResult(): unknown {
  58. if (!this.stack.empty) throw new Error('Cannot get result when processing')
  59. return this.value
  60. }
  61.  
  62. setValue(value: unknown) {
  63. this.value = value
  64. let item: StackItem | undefined
  65. while ((item = this.stack.top)) {
  66. switch (item.type) {
  67. case 'ARRAY':
  68. item.array[item.position++] = this.value
  69. if (item.position !== item.array.length) return
  70. this.value = item.array
  71. this.stack.pop()
  72. continue
  73. case 'MAP_KEY':
  74. if (typeof this.value !== 'string') throw new Error('The type of key must be string')
  75. item.key = this.value
  76. item.type = 'MAP_VALUE'
  77. return
  78. case 'MAP_VALUE':
  79. item.map[item.key!] = this.value
  80. if (--item.remains !== 0) {
  81. item.type = 'MAP_KEY'
  82. return
  83. } else {
  84. this.value = item.map
  85. this.stack.pop()
  86. continue
  87. }
  88. default:
  89. return
  90. }
  91. }
  92. }
  93.  
  94. enterArray(size: number) {
  95. this.stack.push({ type: 'ARRAY', position: 0, size: size | 0, array: new Array(size) })
  96. }
  97.  
  98. enterMap(size: number) {
  99. if (size === 0) {
  100. this.value = {}
  101. return
  102. }
  103. this.stack.push({ type: 'MAP_KEY', remains: size | 0, key: null, map: {} })
  104. }
  105. }
  106.  
  107. const d = new Deserializer()
  108.  
  109. d.enterMap(4)
  110. d.setValue('id')
  111. d.setValue(1)
  112. d.setValue('text')
  113. d.setValue('hogehoge')
  114. d.setValue('user')
  115. d.enterMap(2)
  116. d.setValue('id')
  117. d.setValue(1)
  118. d.setValue('name')
  119. d.setValue('admin')
  120. d.setValue('array')
  121. d.enterArray(3)
  122. d.setValue(1)
  123. d.setValue(2)
  124. d.setValue(3)
  125.  
  126. console.log(d.getResult())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement