-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
prange.reverse.js
66 lines (58 loc) · 1.63 KB
/
prange.reverse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict'
const reversePairs = require('./lib/reverse-pairs')
const reverseNonPairs = require('./lib/reverse-non-pairs')
function sortOut(combos) {
const offsuit = new Set()
const suited = new Set()
const pairs = new Set()
for (let i = 0; i < combos.length; i++) {
const [ rank1, rank2, suit ] = combos[i].trim()
const desc = rank1 + rank2
if (rank1 === rank2) {
pairs.add(desc)
continue
}
if (suit == null || suit === 'o') {
offsuit.add(desc)
continue
}
if (suit === 's') {
suited.add(desc)
continue
}
throw new Error('Invalid suit "' + suit + '" of "' + combos[i] + '"!')
}
return { offsuit, suited, pairs }
}
function unsuitWhenPossible(os, su) {
const consolidated = []
for (const n of os) {
const suitedVersion = n.replace(/o/g, 's')
if (su.has(suitedVersion)) {
consolidated.push(n.replace(/o/g, ''))
su.delete(suitedVersion)
} else {
consolidated.push(n)
}
}
return consolidated.concat(Array.from(su))
}
/**
* Converts a poker hand range to short notation.
* It's the opposite of `prange`.
*
* @name prange.reverse
* @function
* @param {Array.<String>} combos hand combos to be converted to short notation
* @param {String} the short notation for the range
*/
function reverse(combos) {
const { offsuit, suited, pairs } = sortOut(combos)
const ps = reversePairs(pairs)
const os = reverseNonPairs(offsuit, 'o')
const su = reverseNonPairs(suited, 's')
const nonpairs = unsuitWhenPossible(new Set(os), new Set(su))
return ps.concat(nonpairs).join(', ')
}
module.exports = reverse
module.exports.sortOut = sortOut