Sha256: c706366780721edc006be4da04b113cf71f9d98905be64fb956d7d354186743f

Contents?: true

Size: 1.26 KB

Versions: 211

Compression:

Stored size: 1.26 KB

Contents

export default class Square {
  constructor (input) {
    this.input = input;
  }

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

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

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

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

  ciphertext () {
    const textSegments = this.plaintextSegments(),
      columns = [];
    let i, j,
      currentSegment,
      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('');
  }

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

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/crypto-square/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/crypto-square/example.js