@slugbugblue/trax 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -6
- package/README.md +9 -175
- package/docs/engine.md +1 -1
- package/package.json +12 -23
- package/src/engine.js +128 -52
- package/src/types.d.ts +23 -131
- package/src/version.js +1 -1
- package/.dockerignore +0 -11
- package/Dockerfile +0 -14
- package/docs/analyst.md +0 -236
- package/docs/lru.md +0 -151
- package/docs/point.md +0 -148
- package/src/analyst.js +0 -858
- package/src/cli.js +0 -732
- package/src/cmds/analyze.js +0 -112
- package/src/cmds/delete.js +0 -71
- package/src/cmds/help.js +0 -81
- package/src/cmds/import-export.js +0 -394
- package/src/cmds/list.js +0 -177
- package/src/cmds/new.js +0 -136
- package/src/cmds/notes.js +0 -37
- package/src/cmds/play-try.js +0 -175
- package/src/cmds/puzzles.js +0 -188
- package/src/cmds/select.js +0 -47
- package/src/cmds/suggest.js +0 -55
- package/src/cmds/undo.js +0 -40
- package/src/cmds/view.js +0 -65
- package/src/lru.js +0 -153
- package/src/point.js +0 -173
- package/src/puzzles.js +0 -1236
- package/src/threats.js +0 -223
- package/src/tty.js +0 -317
- package/src/utils.js +0 -22
package/src/cmds/puzzles.js
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
/** @file Puzzles CLI
|
|
2
|
-
* @copyright 2022-2023
|
|
3
|
-
* @author Chad Transtrum <chad@transtrum.net>
|
|
4
|
-
* @license Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { Trax } from '@slugbugblue/trax'
|
|
8
|
-
import { levels, puzzles, sources } from '@slugbugblue/trax/puzzles'
|
|
9
|
-
|
|
10
|
-
let maxLevel = 0
|
|
11
|
-
const puzzleIds = []
|
|
12
|
-
for (const puzzle of puzzles) {
|
|
13
|
-
maxLevel = Math.max(maxLevel, puzzle.level)
|
|
14
|
-
puzzleIds.push(puzzle.id)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const validLevels = levels.slice(0, maxLevel + 1)
|
|
18
|
-
const completions = [...validLevels, ...puzzleIds].join('|')
|
|
19
|
-
|
|
20
|
-
const listAliases = ['display', 'list', 'ls', 'show', 'view']
|
|
21
|
-
const startAliases = ['new', 'start']
|
|
22
|
-
|
|
23
|
-
export const puzzlesCmd = {
|
|
24
|
-
name: 'puzzles',
|
|
25
|
-
args: '["list"|"start"] [level|puzzle]',
|
|
26
|
-
comp: 'display|list|ls|new|show|start|view ' + completions,
|
|
27
|
-
desc: 'view and play puzzles',
|
|
28
|
-
help: [
|
|
29
|
-
'Practice your skills with puzzles. Use "list" to see the puzzle',
|
|
30
|
-
'categories, or "start" to play a puzzle.',
|
|
31
|
-
],
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const an = (word = '') => {
|
|
35
|
-
const a = /^[aeiou]/.test(word) ? 'an' : 'a'
|
|
36
|
-
return a + ' ' + word
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const showPuzzle = (CLI, id, started = false) => {
|
|
40
|
-
let puzzle = puzzles.find((p) => p.id === id)
|
|
41
|
-
if (!puzzle) {
|
|
42
|
-
const possibles = puzzles.filter((p) => p.id.includes(id))
|
|
43
|
-
if (possibles.length === 1) puzzle = possibles[0]
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (!puzzle) return CLI.error(`Puzzle ${id} not found`)
|
|
47
|
-
|
|
48
|
-
const trax = new Trax(puzzle.game, puzzle.notation, 'cli')
|
|
49
|
-
const players = ['white', 'black']
|
|
50
|
-
CLI.display(trax, players)
|
|
51
|
-
let out = CLI.color.short(puzzle.id)
|
|
52
|
-
out += CLI.color(` is ${an(levels[puzzle.level])} puzzle`) + '\n'
|
|
53
|
-
if (puzzle.title) out += CLI.color(puzzle.title) + '\n'
|
|
54
|
-
if (puzzle.desc) out += CLI.color.help(puzzle.desc) + '\n'
|
|
55
|
-
out += CLI.color(players[puzzle.player - 1] + ' to win by move ' + puzzle.max)
|
|
56
|
-
const src = sources[puzzle.src]
|
|
57
|
-
|
|
58
|
-
if (src) {
|
|
59
|
-
let source = '\nPuzzle '
|
|
60
|
-
source += src.copyright
|
|
61
|
-
? 'copyright ' + src.copyright + ' by'
|
|
62
|
-
: 'courtesy of'
|
|
63
|
-
source += ' ' + src.name
|
|
64
|
-
const url = src.url ? ' ' + CLI.color.help(src.url) : ''
|
|
65
|
-
out += CLI.color.white(source) + url
|
|
66
|
-
if (src.license) {
|
|
67
|
-
out += '\n' + CLI.color.optional('License: ' + src.license)
|
|
68
|
-
if (src.licenseUrl) out += CLI.color.black(' ' + src.licenseUrl)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
CLI.out(out)
|
|
73
|
-
|
|
74
|
-
if (started) return
|
|
75
|
-
|
|
76
|
-
CLI.out(
|
|
77
|
-
CLI.color.short(
|
|
78
|
-
`Want to try this puzzle? Enter: "puzzles start ${puzzle.id}"`,
|
|
79
|
-
),
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const listPuzzles = (CLI) => {
|
|
84
|
-
let size = 1
|
|
85
|
-
const data = []
|
|
86
|
-
for (const name of validLevels) {
|
|
87
|
-
size = Math.max(size, name.length)
|
|
88
|
-
data.push({ name, count: 0, solved: 0, selected: false })
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (data[CLI.puzzleLevel]) data[CLI.puzzleLevel].selected = true
|
|
92
|
-
|
|
93
|
-
for (const puzzle of puzzles) {
|
|
94
|
-
data[puzzle.level].count += 1
|
|
95
|
-
if (CLI.puzzle(puzzle.id).solved > 0) data[puzzle.level].solved += 1
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
for (const category of data) {
|
|
99
|
-
let out = category.selected ? CLI.color('* ') : ' '
|
|
100
|
-
out += CLI.color.short(category.name.padEnd(size + 1))
|
|
101
|
-
if (category.solved === 0) {
|
|
102
|
-
out += CLI.color(CLI.plural(category.count, 'puzzle'))
|
|
103
|
-
} else if (category.solved === category.count) {
|
|
104
|
-
if (category.count > 3) out += CLI.color.success('all ')
|
|
105
|
-
out += CLI.color.success(CLI.plural(category.count, 'puzzle') + ' solved')
|
|
106
|
-
} else {
|
|
107
|
-
out += CLI.color.success(`${category.solved} solved`)
|
|
108
|
-
out += CLI.color(` of ${category.count} puzzles`)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
CLI.out(out)
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const listLevel = (CLI, levelName) => {
|
|
116
|
-
let size = 1
|
|
117
|
-
const level = levels.indexOf(levelName)
|
|
118
|
-
const data = []
|
|
119
|
-
for (const puzzle of puzzles) {
|
|
120
|
-
if (puzzle.level !== level) continue
|
|
121
|
-
size = Math.max(size, puzzle.id.length)
|
|
122
|
-
data.push({
|
|
123
|
-
id: puzzle.id,
|
|
124
|
-
title: puzzle.title,
|
|
125
|
-
solved: CLI.puzzle(puzzle.id).solved > 0,
|
|
126
|
-
})
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
for (const puzzle of data) {
|
|
130
|
-
let out = CLI.color.success(puzzle.solved ? '✓' : ' ')
|
|
131
|
-
out += CLI.color.short(' ' + puzzle.id)
|
|
132
|
-
if (puzzle.title) out += CLI.color(' ' + puzzle.title)
|
|
133
|
-
CLI.out(out)
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const startPuzzle = (CLI, puzzle) => {
|
|
138
|
-
const id = CLI.newPuzzle(puzzle)
|
|
139
|
-
CLI.out(CLI.color(`Started puzzle ${puzzle.id} as game #${id}`))
|
|
140
|
-
showPuzzle(CLI, puzzle.id, true)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const startNextPuzzle = (CLI, level) => {
|
|
144
|
-
if (level === undefined) level = CLI.puzzleLevel
|
|
145
|
-
const unsolved = puzzles.filter((p) => !CLI.puzzle(p.id).solved)
|
|
146
|
-
if (unsolved.length === 0) {
|
|
147
|
-
CLI.out(CLI.success('You have already completed all the puzzles.'))
|
|
148
|
-
return CLI.error('Specify a specific puzzle to try one again.')
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const next = unsolved.find((p) => p.level >= level) || unsolved[0]
|
|
152
|
-
startPuzzle(CLI, next)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
puzzlesCmd.fn = (CLI, action, id) => {
|
|
156
|
-
action = action?.toLowerCase() ?? 'list'
|
|
157
|
-
if (startAliases.some((alias) => alias.startsWith(action))) {
|
|
158
|
-
if (!id) return startNextPuzzle(CLI)
|
|
159
|
-
|
|
160
|
-
const puzzle = puzzles.find((p) => p.id === id)
|
|
161
|
-
if (puzzle) {
|
|
162
|
-
return startPuzzle(CLI, puzzle)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (validLevels.includes(id)) {
|
|
166
|
-
return startNextPuzzle(CLI, levels.indexOf(id))
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const possibles = puzzles.filter((p) => p.id.includes(id))
|
|
170
|
-
if (possibles.length === 1) {
|
|
171
|
-
return startPuzzle(CLI, possibles[0])
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return CLI.error('Could not find puzzle ' + id)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (action) {
|
|
178
|
-
const x = id || action
|
|
179
|
-
if (validLevels.includes(x)) return listLevel(CLI, x)
|
|
180
|
-
if (listAliases.some((alias) => alias.startsWith(x))) {
|
|
181
|
-
return listPuzzles(CLI)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return showPuzzle(CLI, x)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
listPuzzles(CLI)
|
|
188
|
-
}
|
package/src/cmds/select.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/* Copyright 2022 Chad Transtrum
|
|
2
|
-
*
|
|
3
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
// CLI select command
|
|
17
|
-
|
|
18
|
-
export const selectCmd = {
|
|
19
|
-
name: 'select',
|
|
20
|
-
alt: ['choose', '#'],
|
|
21
|
-
args: '#id [command]',
|
|
22
|
-
comp: '<id> <cmd>',
|
|
23
|
-
desc: 'select a different game',
|
|
24
|
-
rx: /^#\d+$/,
|
|
25
|
-
help: [
|
|
26
|
-
'Make another game the default game of future commands. If another',
|
|
27
|
-
'command is included, that command will be run immediately.',
|
|
28
|
-
],
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
selectCmd.fn = (CLI, id, ...cmds) => {
|
|
32
|
-
if (!id) {
|
|
33
|
-
CLI.error('Missing id.')
|
|
34
|
-
return CLI.do('help', 'select')
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (id.startsWith('#')) id = id.slice(1)
|
|
38
|
-
CLI.load(id)
|
|
39
|
-
if (String(CLI.GAME?.id) === id) {
|
|
40
|
-
CLI.out(CLI.color(`Game #${id} selected.`))
|
|
41
|
-
if (cmds.length > 0) {
|
|
42
|
-
CLI.doNext(cmds.join(' '))
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
CLI.error('Invalid id. Type "list" to see available games.')
|
|
46
|
-
}
|
|
47
|
-
}
|
package/src/cmds/suggest.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/* Copyright 2022-2023 Chad Transtrum
|
|
2
|
-
*
|
|
3
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
// CLI suggest command
|
|
17
|
-
|
|
18
|
-
import { suggest } from '@slugbugblue/trax/analyst'
|
|
19
|
-
import { timeString } from '@slugbugblue/trax/utils'
|
|
20
|
-
|
|
21
|
-
export const suggestCmd = {
|
|
22
|
-
name: 'suggest',
|
|
23
|
-
alt: ['bot'],
|
|
24
|
-
desc: 'suggest a move',
|
|
25
|
-
help: ['Suggest a move in the current game.'],
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
suggestCmd.fn = (CLI, debug) => {
|
|
29
|
-
if (!String(CLI.GAME?.id)) {
|
|
30
|
-
return CLI.error('No active game. Type "new" to start a game.')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const game = CLI.TRAX
|
|
34
|
-
const suggestion = suggest(game)
|
|
35
|
-
|
|
36
|
-
if (!suggestion.best) return
|
|
37
|
-
|
|
38
|
-
CLI.out(
|
|
39
|
-
CLI.bubble(
|
|
40
|
-
game.turn === 1 ? 'w' : 'b',
|
|
41
|
-
String(game.move + 1) + '. ' + suggestion.pick.move,
|
|
42
|
-
),
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
if ('debug'.startsWith(debug)) {
|
|
46
|
-
const m = []
|
|
47
|
-
for (const move of suggestion.options) {
|
|
48
|
-
m.push(move.move + ' ' + move.score)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
CLI.out(
|
|
52
|
-
'[' + m.join(', ') + '] ' + CLI.color.black(timeString(suggestion.ms)),
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
}
|
package/src/cmds/undo.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/* Copyright 2022 Chad Transtrum
|
|
2
|
-
*
|
|
3
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
// CLI undo command
|
|
17
|
-
|
|
18
|
-
export const undoCmd = {
|
|
19
|
-
name: 'undo',
|
|
20
|
-
args: '[n]',
|
|
21
|
-
comp: '<move>',
|
|
22
|
-
desc: 'undo the last move in a game',
|
|
23
|
-
help: [
|
|
24
|
-
'Backtrack one or more moves in a game. Type "undo" to take back the last',
|
|
25
|
-
'move, or "undo 2" to undo the last two moves.',
|
|
26
|
-
],
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
undoCmd.fn = (CLI, n) => {
|
|
30
|
-
n = Math.max(1, Math.floor(Number(n) || 1)) // Get an integer 1 or higher
|
|
31
|
-
|
|
32
|
-
const { moves } = CLI.TRAX
|
|
33
|
-
const undo = moves.slice(0, 0 - n)
|
|
34
|
-
if (moves.length === undo.length) {
|
|
35
|
-
CLI.error('Unable to undo.')
|
|
36
|
-
} else {
|
|
37
|
-
CLI.setGame(undo)
|
|
38
|
-
CLI.do('view')
|
|
39
|
-
}
|
|
40
|
-
}
|
package/src/cmds/view.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/* Copyright 2022-2023 Chad Transtrum
|
|
2
|
-
*
|
|
3
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
// CLI view command
|
|
17
|
-
|
|
18
|
-
export const viewCmd = {
|
|
19
|
-
name: 'view',
|
|
20
|
-
alt: ['show', 'display'],
|
|
21
|
-
args: '[move number]',
|
|
22
|
-
comp: '<move>',
|
|
23
|
-
desc: 'show the game board',
|
|
24
|
-
help: [
|
|
25
|
-
'Show the current game. Include a move number to see the board as it was at',
|
|
26
|
-
'that move.',
|
|
27
|
-
],
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
viewCmd.fn = (CLI, move, ...extra) => {
|
|
31
|
-
if (!CLI.GAME?.id) {
|
|
32
|
-
return CLI.error('No active game. Type "new" to start a game.')
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (move?.length > 1 && 'puzzles'.startsWith(move)) {
|
|
36
|
-
return CLI.do('puzzles', 'list', ...extra)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const trax = CLI.TRAX
|
|
40
|
-
|
|
41
|
-
let show = Number.POSITIVE_INFINITY
|
|
42
|
-
if (move && Number(move) >= 0 && Number(move) <= trax.moves.length) {
|
|
43
|
-
show = Number(move)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const game = CLI.GAME
|
|
47
|
-
const players = game.players || ['white', 'black']
|
|
48
|
-
const puzzle = game.puzzle || ''
|
|
49
|
-
const { note } = [...(game.notes || [{}])].pop()
|
|
50
|
-
|
|
51
|
-
CLI.out(
|
|
52
|
-
CLI.color('#' + CLI.ID + ' ') +
|
|
53
|
-
CLI.name(trax.rules) +
|
|
54
|
-
' ' +
|
|
55
|
-
CLI.bubble('w' + (trax.turn === 1 ? 'h' : ''), players[0]) +
|
|
56
|
-
CLI.color(' vs ') +
|
|
57
|
-
CLI.bubble('b' + (trax.turn === 2 ? 'h' : ''), players[1]),
|
|
58
|
-
)
|
|
59
|
-
CLI.display(trax, players, undefined, show)
|
|
60
|
-
if (puzzle && (!note || !note.includes(puzzle))) {
|
|
61
|
-
CLI.out(CLI.color('Puzzle ' + puzzle))
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (note) CLI.out(CLI.color.help(note))
|
|
65
|
-
}
|
package/src/lru.js
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/** Least Recently Used cache
|
|
2
|
-
* @copyright 2023
|
|
3
|
-
* @author Chad Transtrum <chad@transtrum.net>
|
|
4
|
-
* @license Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/** LRU cache: a class for remembering things. It uses a Map internally and
|
|
8
|
-
* exposes most of the same interface.
|
|
9
|
-
*/
|
|
10
|
-
export class LRU {
|
|
11
|
-
/** @type Map */
|
|
12
|
-
#cache
|
|
13
|
-
#capacity = 0
|
|
14
|
-
#hits = 0
|
|
15
|
-
#misses = 0
|
|
16
|
-
#expired = 0
|
|
17
|
-
|
|
18
|
-
/** Instantiate this class with the max number of cached items
|
|
19
|
-
* @arg {number} capacity - the maximum number of items to cache
|
|
20
|
-
*/
|
|
21
|
-
constructor(capacity = 1000) {
|
|
22
|
-
this.#cache = new Map()
|
|
23
|
-
if (typeof capacity === 'number' && capacity >= 1) {
|
|
24
|
-
this.#capacity = Math.floor(capacity)
|
|
25
|
-
} else {
|
|
26
|
-
throw new SyntaxError('capacity must be a positive integer')
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** The maximum capacity. */
|
|
31
|
-
get capacity() {
|
|
32
|
-
return this.#capacity
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** The current size. */
|
|
36
|
-
get size() {
|
|
37
|
-
return this.#cache.size
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Number of times we have had a cache hit. */
|
|
41
|
-
get hits() {
|
|
42
|
-
return this.#hits
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Number of times we have had a cache miss. */
|
|
46
|
-
get misses() {
|
|
47
|
-
return this.#misses
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Number of times we have expired a cache entry. */
|
|
51
|
-
get expired() {
|
|
52
|
-
return this.#expired
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Retrieve an item from the cache.
|
|
56
|
-
* @arg {any} key - the key of the cached item
|
|
57
|
-
* @returns {any} - the cached item or undefined
|
|
58
|
-
*/
|
|
59
|
-
get(key) {
|
|
60
|
-
if (this.#cache.has(key)) {
|
|
61
|
-
// Update the least recently used order
|
|
62
|
-
const value = this.#cache.get(key)
|
|
63
|
-
this.#cache.delete(key)
|
|
64
|
-
this.#cache.set(key, value)
|
|
65
|
-
this.#hits++
|
|
66
|
-
} else {
|
|
67
|
-
this.#misses++
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return this.#cache.get(key)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Retrieve an item from the cache without triggering any LRU handling.
|
|
74
|
-
* @arg {any} key - the key of the cached item to retrieve
|
|
75
|
-
* @returns {any} - the cached item or undefined
|
|
76
|
-
*/
|
|
77
|
-
peek(key) {
|
|
78
|
-
return this.#cache.get(key)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Store an item in the cache.
|
|
82
|
-
* @arg {any} key - the key of the cached item
|
|
83
|
-
* @arg {any} value - the value to store
|
|
84
|
-
*/
|
|
85
|
-
set(key, value) {
|
|
86
|
-
// To update an item, remove and re-add it to track LRU status
|
|
87
|
-
this.#cache.delete(key)
|
|
88
|
-
|
|
89
|
-
// If there are too many items, expire one before adding more
|
|
90
|
-
if (this.#cache.size >= this.#capacity) {
|
|
91
|
-
this.#expired++
|
|
92
|
-
const [expire] = this.#cache[Symbol.iterator]().next().value
|
|
93
|
-
this.#cache.delete(expire)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
this.#cache.set(key, value)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Delete an item from the cache.
|
|
100
|
-
* @arg {any} key - the key of the cached item
|
|
101
|
-
* @returns {boolean} true if the item was in the cache
|
|
102
|
-
*/
|
|
103
|
-
delete(key) {
|
|
104
|
-
return this.#cache.delete(key)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** Clear the cache and all its statistics. */
|
|
108
|
-
clear() {
|
|
109
|
-
this.#cache.clear()
|
|
110
|
-
this.#hits = 0
|
|
111
|
-
this.#misses = 0
|
|
112
|
-
this.#expired = 0
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Get an iterator to return [key, value] pairs from the cache. Does not
|
|
116
|
-
* update any LRU information. */
|
|
117
|
-
entries() {
|
|
118
|
-
return this.#cache.entries()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Determine if a key is already in the cache. Does not trigger hit/miss.
|
|
122
|
-
* @arg {any} key - the key to check
|
|
123
|
-
* @returns {boolean} true if the key is in the cache
|
|
124
|
-
*/
|
|
125
|
-
has(key) {
|
|
126
|
-
return this.#cache.has(key)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Get an iterator for the keys stored in cache, in order from least to most
|
|
130
|
-
* recent. Does not update any LRU information. */
|
|
131
|
-
keys() {
|
|
132
|
-
return this.#cache.keys()
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Get an iterator for the values stored in cache, in order from least to
|
|
136
|
-
* most recent. Does not update any LRU information. */
|
|
137
|
-
values() {
|
|
138
|
-
return this.#cache.values()
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Play nicely with the rest of the world
|
|
142
|
-
toString() {
|
|
143
|
-
return `LRU(${this.#cache.size} of ${this.#capacity})`
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
[Symbol.for('nodejs.util.inspect.custom')](_, options) {
|
|
147
|
-
const [y, s, n] = [options.stylize, 'special', 'number']
|
|
148
|
-
return `${y('LRU', s)}(${y(this.#cache.size, n)} of ${y(
|
|
149
|
-
this.#capacity,
|
|
150
|
-
n,
|
|
151
|
-
)})`
|
|
152
|
-
}
|
|
153
|
-
}
|