Skip to content

Commit 141824d

Browse files
committed
refactor some code lines, increase version
* Base32: use object assign and map * DeckEncoder: use reduce instead of push
1 parent eb8fbdc commit 141824d

File tree

3 files changed

+27
-39
lines changed

3 files changed

+27
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "runeterra",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Legends of Runeterra deck code encoder/decoder",
55
"main": "src/index.js",
66
"types": "index.d.ts",

src/Base32.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class Base32 {
2626
}
2727

2828
static decode (encoded) {
29-
encoded = encoded.trim().replace(Base32.SEPARATOR, '')
30-
encoded = encoded.replace(/[=]*$/, '')
31-
encoded = encoded.toUpperCase()
29+
encoded = encoded.trim().replace(Base32.SEPARATOR, '').replace(/[=]*$/, '').toUpperCase()
3230

3331
if (encoded.length === 0) return [0]
3432
const encodedLength = encoded.length
@@ -91,10 +89,7 @@ class Base32 {
9189
Base32.DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.split('')
9290
Base32.MASK = Base32.DIGITS.length - 1
9391
Base32.SHIFT = Base32.numberOfTrailingZeros(Base32.DIGITS.length)
94-
Base32.CHAR_MAP = Base32.DIGITS.reduce((m, d, i) => {
95-
m[d.toString()] = i
96-
return m
97-
}, {})
92+
Base32.CHAR_MAP = Object.assign(...Base32.DIGITS.map((d, i) => ({ [d]: i })))
9893
Base32.SEPARATOR = '-'
9994

10095
module.exports = Base32

src/DeckEncoder.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class DeckEncoder {
2525
throw new TypeError('The provided code requires a higher version of this library; please update.')
2626
}
2727

28-
for (let i = 3; i > 0; i--) {
28+
// first encoded cards are grouped by 3, 2, 1 pieces
29+
DeckEncoder.GROUPS.forEach(i => {
2930
const numGroupOfs = VarInt.pop(bytes)
3031

3132
for (let j = 0; j < numGroupOfs; j++) {
@@ -43,7 +44,7 @@ class DeckEncoder {
4344
result.push(Card.from(setString, factionString, cardString, i))
4445
}
4546
}
46-
}
47+
})
4748

4849
while (bytes.length > 0) {
4950
const fourPlusCount = VarInt.pop(bytes)
@@ -66,46 +67,37 @@ class DeckEncoder {
6667
throw new TypeError('The deck provided contains invalid card codes')
6768
}
6869

69-
const grouped3 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 3))
70-
const grouped2 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 2))
71-
const grouped1 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 1))
72-
const nOfs = cards.filter(c => c.count > 3)
73-
70+
const grouped = DeckEncoder.GROUPS.map(i => this.groupByFactionAndSetSorted(cards.filter(c => c.count === i)))
7471
return Base32.encode([
7572
DeckEncoder.FORMAT << 4 | cards.reduce((p, c) => Math.max(p, c.version), 0) & 0xF,
76-
...this.encodeGroup(grouped3),
77-
...this.encodeGroup(grouped2),
78-
...this.encodeGroup(grouped1),
79-
...this.encodeNofs(nOfs)
73+
...grouped.map(group => this.encodeGroup(group)).reduce((prev, curr) => [...prev, ...curr], []),
74+
...this.encodeNofs(cards.filter(c => c.count > DeckEncoder.GROUPS[0]))
8075
])
8176
}
8277

8378
static encodeNofs (nOfs) {
8479
return nOfs
8580
.sort((a, b) => a.code.localeCompare(b.code))
86-
.reduce((result, card) => {
87-
result.push(...VarInt.get(card.count))
88-
result.push(...VarInt.get(card.set))
89-
result.push(...VarInt.get(card.faction.id))
90-
result.push(...VarInt.get(card.id))
91-
return result
92-
}, [])
81+
.reduce((prev, card) => [
82+
...prev,
83+
...VarInt.get(card.count),
84+
...VarInt.get(card.set),
85+
...VarInt.get(card.faction.id),
86+
...VarInt.get(card.id)
87+
], [])
9388
}
9489

9590
static encodeGroup (group) {
96-
return group.reduce((result, list) => {
97-
result.push(...VarInt.get(list.length))
98-
99-
const first = list[0]
100-
result.push(...VarInt.get(first.set))
101-
result.push(...VarInt.get(first.faction.id))
102-
103-
for (const card of list) {
104-
result.push(...VarInt.get(card.id))
105-
}
106-
107-
return result
108-
}, VarInt.get(group.length))
91+
return group.reduce(
92+
(prev, list) => [
93+
...prev,
94+
...VarInt.get(list.length),
95+
...VarInt.get(list[0].set),
96+
...VarInt.get(list[0].faction.id),
97+
...list.map(card => VarInt.get(card.id)).reduce((prev, curr) => [...prev, ...curr], [])
98+
],
99+
VarInt.get(group.length)
100+
)
109101
}
110102

111103
static isValidDeck (cards) {
@@ -145,5 +137,6 @@ class DeckEncoder {
145137
DeckEncoder.MAX_KNOWN_VERSION = 5
146138
DeckEncoder.FORMAT = 1
147139
DeckEncoder.INITIAL_VERSION = 1
140+
DeckEncoder.GROUPS = [3, 2, 1]
148141

149142
module.exports = DeckEncoder

0 commit comments

Comments
 (0)