From 141824d50097af8234c8097b6565cfec8987a033 Mon Sep 17 00:00:00 2001 From: chrysomallos <68998811+chrysomallos@users.noreply.github.com> Date: Thu, 21 Jan 2021 22:31:00 +0100 Subject: [PATCH] refactor some code lines, increase version * Base32: use object assign and map * DeckEncoder: use reduce instead of push --- package.json | 2 +- src/Base32.js | 9 ++------ src/DeckEncoder.js | 55 ++++++++++++++++++++-------------------------- 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 2d5f2bc..77e944c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "runeterra", - "version": "4.0.0", + "version": "4.0.1", "description": "Legends of Runeterra deck code encoder/decoder", "main": "src/index.js", "types": "index.d.ts", diff --git a/src/Base32.js b/src/Base32.js index d758191..f4e6713 100644 --- a/src/Base32.js +++ b/src/Base32.js @@ -26,9 +26,7 @@ class Base32 { } static decode (encoded) { - encoded = encoded.trim().replace(Base32.SEPARATOR, '') - encoded = encoded.replace(/[=]*$/, '') - encoded = encoded.toUpperCase() + encoded = encoded.trim().replace(Base32.SEPARATOR, '').replace(/[=]*$/, '').toUpperCase() if (encoded.length === 0) return [0] const encodedLength = encoded.length @@ -91,10 +89,7 @@ class Base32 { Base32.DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.split('') Base32.MASK = Base32.DIGITS.length - 1 Base32.SHIFT = Base32.numberOfTrailingZeros(Base32.DIGITS.length) -Base32.CHAR_MAP = Base32.DIGITS.reduce((m, d, i) => { - m[d.toString()] = i - return m -}, {}) +Base32.CHAR_MAP = Object.assign(...Base32.DIGITS.map((d, i) => ({ [d]: i }))) Base32.SEPARATOR = '-' module.exports = Base32 diff --git a/src/DeckEncoder.js b/src/DeckEncoder.js index aeceadf..972fa98 100644 --- a/src/DeckEncoder.js +++ b/src/DeckEncoder.js @@ -25,7 +25,8 @@ class DeckEncoder { throw new TypeError('The provided code requires a higher version of this library; please update.') } - for (let i = 3; i > 0; i--) { + // first encoded cards are grouped by 3, 2, 1 pieces + DeckEncoder.GROUPS.forEach(i => { const numGroupOfs = VarInt.pop(bytes) for (let j = 0; j < numGroupOfs; j++) { @@ -43,7 +44,7 @@ class DeckEncoder { result.push(Card.from(setString, factionString, cardString, i)) } } - } + }) while (bytes.length > 0) { const fourPlusCount = VarInt.pop(bytes) @@ -66,46 +67,37 @@ class DeckEncoder { throw new TypeError('The deck provided contains invalid card codes') } - const grouped3 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 3)) - const grouped2 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 2)) - const grouped1 = this.groupByFactionAndSetSorted(cards.filter(c => c.count === 1)) - const nOfs = cards.filter(c => c.count > 3) - + const grouped = DeckEncoder.GROUPS.map(i => this.groupByFactionAndSetSorted(cards.filter(c => c.count === i))) return Base32.encode([ DeckEncoder.FORMAT << 4 | cards.reduce((p, c) => Math.max(p, c.version), 0) & 0xF, - ...this.encodeGroup(grouped3), - ...this.encodeGroup(grouped2), - ...this.encodeGroup(grouped1), - ...this.encodeNofs(nOfs) + ...grouped.map(group => this.encodeGroup(group)).reduce((prev, curr) => [...prev, ...curr], []), + ...this.encodeNofs(cards.filter(c => c.count > DeckEncoder.GROUPS[0])) ]) } static encodeNofs (nOfs) { return nOfs .sort((a, b) => a.code.localeCompare(b.code)) - .reduce((result, card) => { - result.push(...VarInt.get(card.count)) - result.push(...VarInt.get(card.set)) - result.push(...VarInt.get(card.faction.id)) - result.push(...VarInt.get(card.id)) - return result - }, []) + .reduce((prev, card) => [ + ...prev, + ...VarInt.get(card.count), + ...VarInt.get(card.set), + ...VarInt.get(card.faction.id), + ...VarInt.get(card.id) + ], []) } static encodeGroup (group) { - return group.reduce((result, list) => { - result.push(...VarInt.get(list.length)) - - const first = list[0] - result.push(...VarInt.get(first.set)) - result.push(...VarInt.get(first.faction.id)) - - for (const card of list) { - result.push(...VarInt.get(card.id)) - } - - return result - }, VarInt.get(group.length)) + return group.reduce( + (prev, list) => [ + ...prev, + ...VarInt.get(list.length), + ...VarInt.get(list[0].set), + ...VarInt.get(list[0].faction.id), + ...list.map(card => VarInt.get(card.id)).reduce((prev, curr) => [...prev, ...curr], []) + ], + VarInt.get(group.length) + ) } static isValidDeck (cards) { @@ -145,5 +137,6 @@ class DeckEncoder { DeckEncoder.MAX_KNOWN_VERSION = 5 DeckEncoder.FORMAT = 1 DeckEncoder.INITIAL_VERSION = 1 +DeckEncoder.GROUPS = [3, 2, 1] module.exports = DeckEncoder