Convert between different sudoku formats (lisudoku, fpuzzles, penpa).
See it in action on the lisudoku solver page through the import button.
yarn add sudoku-formats
import { decodeSudoku, encodeSudoku, FORMATS, SudokuDataFormat } from 'sudoku-formats'
// Parse grid string
decodeSudoku('1000003002000004').then(({ constraints }) => {
console.log(constraints)
})
// Parse lisudoku puzzle
const LISUDOKU_URL = 'https://lisudoku.xyz/solver?import=1000003002000004'
const { format, dataString, constraints, error } = await decodeSudoku(LISUDOKU_URL)
console.log(constraints) // { gridSize: 4, regions: [...], ... }
console.log(dataString) // '1000003002000004'
console.log(format) // 'lisudoku'
console.log(error) // undefined
console.log(encodeSudoku({ constraints: constraints, format: SudokuDataFormat.Lisudoku }))
// { dataString: '1000003002000004', url: 'https://lisudoku.xyz/solver?import=1000003002000004' }
// Parse f-puzzles puzzle
const FPUZZLES_URL = 'https://f-puzzles.com/?load=N4IgzglgXgpiBcBOANCA5gJwgEwQbT1ADcBDAGwFc54BGVNCImAOwQBcMqBfZYHv3vyGCRfALrJCwgTOlyJU0XKVKFsles1c1yrRu2S9R+Yd1nVp/Vdk7r58WK5A'
const { format, dataString, constraints, error } = await decodeSudoku(FPUZZLES_URL)
console.log(constraints) // { size: 9, grid: [...], ... }
console.log(dataString) // 'N4IgzglgXgpiBcBOAN...'
console.log(format) // 'fpuzzles'
console.log(error) // undefined
// Handling errors
const { error } = await decodeSudoku('123');
console.log(error) // 'Error while parsing inline data'
// Get all formats
console.log(FORMATS) // [{ format: 'lisudoku', urlPatterns: ... }, ...]
import { decodeSudoku, transformSudoku, SudokuDataFormat } from 'sudoku-formats'
const LISUDOKU_URL = 'https://lisudoku.xyz/solver?import=1000003002000004'
const { format, constraints } = await decodeSudoku(LISUDOKU_URL)
// lisudoku -> fpuzzles
const result = transformSudoku({
constraints,
fromFormat: format,
toFormat: SudokuDataFormat.Fpuzzles,
})
console.log(result.constraints) // { size: 4, ... }
console.log(result.url) // 'https://f-puzzles.com/?load=...'
console.log(result.error) // undefined
Format | URL patterns | Example |
---|---|---|
lisudoku |
https://lisudoku.xyz/e?import=:data https://lisudoku.xyz/solver?import=:data https://lisudoku.xyz/p/:id
|
example1 example2 |
fpuzzles | https://f-puzzles.com/?load=:data |
example1 |
penpa | ...://.../penpa-edit/#...&p=:data |
example1 example2 |
Do you want another format supported? Open an issue or a pull request.
Consider joining the discord server.