Sha256: 659a198ac520124b597d070737e6d44822d9133e9a815e4782631901a9493d49

Contents?: true

Size: 1.49 KB

Versions: 54

Compression:

Stored size: 1.49 KB

Contents

export default class Square {
    input: string

    constructor(input: string) {
        this.input = input
    }

    normalizePlaintext(): string {
        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()
        let i
        let j
        // tslint:disable-next-line: no-any
        const columns: any[] = []
        let currentSegment
        let 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

54 entries across 54 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/crypto-square/crypto-square.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/crypto-square/crypto-square.example.ts