Sha256: 4823f955eae64d79e171785cece8ab3c1b8e0ea4133f4d7c2baa3bcd096ede76

Contents?: true

Size: 1.32 KB

Versions: 255

Compression:

Stored size: 1.32 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, 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

255 entries across 255 versions & 1 rubygems

Version Path
trackler-2.1.0.40 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.39 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.38 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.37 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.36 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.34 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.33 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.32 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.31 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.30 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.29 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.28 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.27 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.26 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.25 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.24 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.23 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.22 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.21 tracks/javascript/exercises/crypto-square/example.js
trackler-2.1.0.20 tracks/javascript/exercises/crypto-square/example.js