
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@bedard/twister
Advanced tools
[](https://github.com/scottbedard/twister/actions) [](https://codecov.io/gh/scottbedard/twister)
Twister is a library for modeling the state of twisty puzzles. To get started, check out the interactive playground.
The recommended way to install is through NPM.
npm install @bedard/twister
Alternatively, you can use the CDN. When using the CDN, the library will be exposed globally as Twister.
<script src="https://unpkg.com/@bedard/twister"></script>
Below is the recommended way to instantiate Twister models. Using destructured imports allows for unused puzzles to be tree-shaken from your application.
import { Cube } from '@bedard/twister'
const puzzle = new Cube({ size: 3 })
Once a puzzle has been instantiated, the following methods are available...
applySet the puzzle to a given state. State objects can be created using the output method.
puzzle.apply(state)
cloneCreate a new puzzle instance with the same state and options.
const puzzle = new Puzzle()
const clone = puzzle.clone()
executeUpdates puzzle state using a parsed turn object. In most situations, it's simpler to use the turn method and make use of turn notation.
const turn = puzzle.parse('R')
puzzle.execute(turn)
generateScrambleGenerates a scramble of a default or specified number of moves, but does not execute it.
const scramble = puzzle.generateScramble()
outputReturns a minified version of the puzzle's state. This method is useful for saving state as JSON, then restoring that state via the apply method.
const state = puzzle.output()
parseConvert a single piece of puzzle notation to a turn object. This method is generally used to interact with the execute method, but is also useful for testing if notation is valid.
const turn = puzzle.parse('R')
parseAlgorithmConvert a space-delimited string of turns into an array of turn objects.
const turns = puzzle.parseAlgorithm('R U R-')
resetReturn a puzzle to its solved state.
puzzle.reset()
scrambleScrambles a puzzle to a default or specified number of moves. This is similar to generateScramble, the only difference being that this method executes the resulting scramble. This method is also available via the CLI, more info here.
puzzle.scramble()
stickersGet stickers affected by a turn. If no turn notation is provided, all stickers will be returned.
const stickers = puzzle.stickers('R')
testTest if the puzzle is solved.
const solved = puzzle.test()
turnExecutes an algorithm. This method is also available via the CLI, more info here.
puzzle.turn('R U R-')
All stickers are stored as { value } objects. This allows for additional information, such as rendering data, to be attached to the stickers. To do this, simply instantiate the puzzle, and modify the objects that are part of puzzle.state.
import { Cube } from '@bedard/twister'
const puzzle = new Cube({ size: 3 })
puzzle.state.u[0].foo = 'bar'
For applications with advanced scrambling needs, a custom random function can be provided on instantiation. Below is an example using Rando.js to generate cryptographically strong scrambles. The random option must be a function that returns a floating point number between 0 and 1. By default, Math.random is used.
import { Cube } from '@bedard/twister'
import { rando } from '@nastyox/rando.js';
const puzzle = new Cube({
  random: rando,
  size: 3,
});
While this library does it's best to generate strong scrambles, it should never be used in WCA events. Always use the official TNoodle library for WCA purposes.
The following utilities are available from the command line. Constructor options can be provided via --options. Note that when providing JSON arguments, we use JSON5 syntax for a smoother user experience.
parseParse a single piece of turn notation.
$ twister parse cube R
parseAlgorithmParse multiple pieces of turn notation.
$ twister parseAlgorithm cube "R U R-"
scrambleScramble a puzzle.
# scramble a puzzle
$ twister scramble cube
# scramble a puzzle to a specific number of moves
$ twister scramble cube --turns=10
turnExecute an algorithm. This will be performed on a solved puzzle unless an initial state is provided.
# apply turns to a solved puzzle
$ twister turn cube "R U R-"
# apply turns from an initial state
$ twister turn cube "R U R-" --state="{...}"
This puzzle represents an N-layered face turning cube.
 
import { Cube } from '@bedard/twister';
const puzzle = new Cube({ size: 3 });
Cube state is represented as an array of sticker objects. Each face array starts from the top left sticker and read sequentially to the bottom right. To picture how these values map onto an actual cube, imagine unfolding a cube while looking at the F face. Notice that the B face has the same orientation as the L, F, and R faces.
   
Our notation is a superset of WCA notation. Any algorithm produced by a WCA scrambler is compatible with this library. There are however, a couple of extensions we've made to the WCA notation. The first of which is the optional use of a - to indicate counter-clockwise turns. The second is the ability to annotate "slice turns" with a single move. To do this, simply omit the wide segment of a turn. For example, a 3F in our notation system would be equal to 3Fw 2Fw' in WCA notation.
This puzzle represents an N-layered face turning dodecahedron.
 
import { Dodecaminx } from '@bedard/twister';
const puzzle = new Dodecaminx({ size: 3 });
State for a dodecaminx is stored as an array of corner matrices, middle arrays, and a center value. These arrays start from the primary corner of a face, and continue clockwise around that face. The corner matrices are similar to that of a cube face, starting with the corner furthest from the center and reading sequentially towards the center. Middle arrays start with the sticker furthest from the center. Note that for even-layered puzzles, the middle and center values are omitted.
   
Notation for this puzzle is similar to that of cubes. The main difference is that whole-puzzle rotations are denoted by lowercase letters. Here are a few examples to demonstrate various turns around the U face.
U = outer layer turned once clockwiseU2 = outer layer turned twice clockwiseU2- = outer layer turned twice counter-clockwise2U = second layer turned once clockwiseUw = second and outer layer turned once clockwiseu = entire puzzle rotated once clockwiseu2 = entire puzzle rotated twice clockwiseu2- = entire puzzle rotated twice counter-clockwiseCopyright (c) 2020-present, Scott Bedard
FAQs
[](https://github.com/scottbedard/twister/actions) [](https://codecov.io/gh/scottbedard/twister)
The npm package @bedard/twister receives a total of 231 weekly downloads. As such, @bedard/twister popularity was classified as not popular.
We found that @bedard/twister demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.