Sha256: ac93bb2a0f827e2073bf84958bfe51c107d0235239c15a05188f2a061e8e16f4

Contents?: true

Size: 1.33 KB

Versions: 119

Compression:

Stored size: 1.33 KB

Contents

'use strict';

module.exports = function (input) {
  this.input = input;

  this.normalizePlaintext = function () {
    return input.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
  };

  this.size = function () {
    var realLength = Math.sqrt(this.normalizePlaintext().length);
    return Math.ceil(realLength);
  };

  this.plaintextSegments = function () {
    var plainText = this.normalizePlaintext();
    var chunkSize = this.size();

    var splitRegex = new RegExp('.{1,' + chunkSize + '}', 'g');
    return plainText.match(splitRegex);
  };

  this.ciphertext = function () {
    var textSegments = this.plaintextSegments();
    var i;
    var j;
    var columns = [];
    var currentSegment;
    var currentLetter;

    for (i = 0; i < this.size(); i++) {
      columns.push([]);
    }

    for (i = 0; i < textSegments.length; i++) {
      currentSegment = textSegments[i];

      for (j = 0; j < currentSegment.length; j++) {
        currentLetter = currentSegment[j];
        columns[j].push(currentLetter);
      }
    }

    for (i = 0; i < columns.length; i++) {
      columns[i] = columns[i].join('');
    }

    return columns.join('');
  };

  this.normalizeCiphertext = function () {
    var chunkSize = this.size();
    var splitRegex = new RegExp('.{1,' + chunkSize + '}', 'g');
    return this.ciphertext().match(splitRegex).join(' ');
  };
};

Version data entries

119 entries across 119 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.158 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.157 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.156 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.155 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.154 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.153 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.152 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.151 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.150 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.149 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.148 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.147 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.146 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.145 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.144 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.143 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.142 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.141 tracks/javascript/exercises/crypto-square/example.js
trackler-2.2.1.140 tracks/javascript/exercises/crypto-square/example.js