There are several number of rubik's cube related programs on npm, and many of them has their own functions to parse algorithms. Mostly their functions support only limited syntax of algorithm notation unfortunately.
This library intends to be a base for this kind of programs to support broad syntax of algorithm notation.
The syntax definition is written by and the parser is generated by PEG.js.
$ npm install cube-notation-normalizer
const normalize = ;const ugly = "(rU R' U`) (r' FRF' ) ";// default, human-readable format;// => "r U R' U' r' F R F'"// program-friendly format;// => "RMMMURRRUUURRRMFRFFF"
With the latter options, result will contain only
S letters and not contain any whitespaces nor modifiers. This is highly well-formatted and will be very handy for programs. Especially,
.match(/(.)\1*/g) may be useful.
If you prefer, you also can choose
z instead of
Supported syntax and features
Face turns, slice turns and cube rotations
// supported face letters:;// => "R U F L D B M E S r u f l d b x y z"
Double layer turns
// 'w' notation is normalized to lowercase;// => "r u"
Whitespaces and comments
;// => "R U R' U'"
// variety of "prime"s;// => "R' U' R' U' R' U' R'";// => "F' U' R'"
;// => "R' U2 F D F D"// '*' and '^' are optional;// => "R2 U2 F D F D"
Conjugates and commutators
;// => "F R F'";// => "R U R' U'";// => "F R U R' U' F'"
;// => "";// => "U2";// => "R2 L"
Algorithm notation string to normalize.
Object with following format. All properties are optional.
separator: ' 'useModifiers: trueuniformCenterMoves: false // false | 'rotation' | 'slice'invert: false
Separator string which will be inserted between each turns. (default
true, returned notation includes modifier letters
false, inverted turns and half turns are represented as repetition. (default
;// => "R R R U U"
'slice', the turns with center moves (
r, etc.) are converted and unified to rotation moves (
z) or slice moves (
;// => "L' x U D' y'";// => "U D' E' R M'"
Note: This center move conversion is valid for only 3x3x3 cube.
true, algorithm will be inverted. (default
;// => "U R U' R'"
This returns same result as
normalize("(" + alg + ")'"), but using this option will make messages of possible errors more clear, especially for error location in algorithm strings.
PEG.js error class thrown from the parser.