UNPKG

rubik-cipher

Version:

Simple rubik cube cipher

121 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Cube = require('bedard-cube'); exports.chunkString = (str, length) => str.match(new RegExp('.{1,' + length + '}', 'g')); exports.minimalSizeByText = (text) => { let sizeNumber = 2; while (text.length > (6 * (sizeNumber * sizeNumber))) { sizeNumber++; } return sizeNumber; }; exports.canFitInCube = (text, size) => (text.length <= (6 * (size ** 2))); const createInverseTurn = (turnObj) => { const { rotation } = turnObj; const retArr = []; if (rotation === -1) { retArr.push({ ...turnObj, rotation: 1, }); } else if (rotation === 1) { retArr.push({ ...turnObj, rotation: -1, }); } else { // Rotation === 2 retArr.push({ ...turnObj, rotation: -1, }, { ...turnObj, rotation: -1, }); } return retArr; }; exports.getInverseCombination = (combination) => (combination.map(c => createInverseTurn(c)).flat().reverse()); const getIndex = (obj, size) => ({ ...obj, index: (obj.value * (size ** 2)) + obj.originalIndex, }); const getStickers = (cube) => { const stickers = []; cube.stickers((sticker) => { stickers.push({ ...sticker, index: 0, }); }); return stickers; }; const getIndexMap = (cube, size) => (getStickers(cube).map((sticker) => getIndex(sticker, size))); const getIndexOnly = (indexMap) => indexMap.map(index => index.index); exports.getAllIndexes = (cube, size) => getIndexOnly(getIndexMap(cube, size)); exports.isValidKey = (text, key) => { if (!(/^[^+](\+{0,1}[- a-zA-Z0-9]+)+[^+]$/.test(key))) { return false; } const keyParts = key.split('+'); if (keyParts.length !== 5) { return false; } const cubeSize = Number(keyParts[0]); const groupSize = Number(keyParts[1]); if (cubeSize < 2) { return false; } if (groupSize < 1) { return false; } if (!exports.canFitInCube(text, cubeSize)) { return false; } return true; }; exports.getDataFromKey = (text, key) => { if (exports.isValidKey(text, key)) { const keyParts = key.split('+'); return { cubeSize: Number(keyParts[0]), groupSize: Number(keyParts[1]), fillRest: (keyParts[2] === '1'), fillWithRandom: (keyParts[3] === '1'), }; } throw new Error('Key is not valid'); }; exports.encodeScramble = (turnObj) => { const { depth, target, wide, rotation } = turnObj; const isWide = wide ? '1' : '0'; let rot = ''; switch (rotation) { case 1: case 2: rot = rotation.toString(); break; case -1: rot = '3'; break; default: throw new Error('Invalid spin count'); } return `${target}${isWide}${depth.toString()}${rot}`; }; exports.decodeScramble = (turnStr) => { const splitString = turnStr.split(''); if (splitString.length >= 4) { const target = splitString.shift(); const wide = splitString.shift() === '1'; let rotation = Number(splitString.pop()); if (rotation === 3) { rotation = -1; } const depth = Number(splitString); return { target, wide, rotation, depth }; } throw new Error('Invalid scramble string format'); }; //# sourceMappingURL=rubikHelpers.js.map