From 03a33e7d90a521befdc124f88e62032e5e06f7c0 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Sun, 6 Dec 2020 19:15:07 -0800 Subject: [PATCH] refactor[2020-day-02]: prep for different password validation rules Rules provided were for old employers --- 2020/day-02/cleanupPasswords.js | 20 +++++++++++--------- 2020/day-02/cleanupPasswords.test.js | 12 ++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/2020/day-02/cleanupPasswords.js b/2020/day-02/cleanupPasswords.js index 042bc94..7b4dde3 100644 --- a/2020/day-02/cleanupPasswords.js +++ b/2020/day-02/cleanupPasswords.js @@ -14,7 +14,7 @@ const splitRecord = (row) => { /** * Splits a password validation rule into its component parts */ -const splitRule = (rule) => { +const oldSplitRule = (rule) => { const splitRow = rule.split(/-| /) return { @@ -27,7 +27,7 @@ const splitRule = (rule) => { /** * Validates a password against the specified rule */ -const isValidPassword = (rule, password) => { +const oldIsValidPassword = (rule, password) => { // count how many times `rule.char` exists in `password` const count = ( password.match( @@ -40,15 +40,17 @@ const isValidPassword = (rule, password) => { return true } -const isValidRecord = (record) => { +const oldIsValidRecord = (record) => { const { rule, password } = splitRecord(record) - const { min, max, char } = splitRule(rule) - return isValidPassword({ min, max, char }, password) + const parsedRule = oldSplitRule(rule) + return oldIsValidPassword(parsedRule, password) } module.exports = { - splitRecord, - splitRule, - isValidRecord, - isValidPassword + old: { + splitRule: oldSplitRule, + isValidPassword: oldIsValidPassword, + isValidRecord: oldIsValidRecord + }, + splitRecord } diff --git a/2020/day-02/cleanupPasswords.test.js b/2020/day-02/cleanupPasswords.test.js index 01602fa..93f407b 100644 --- a/2020/day-02/cleanupPasswords.test.js +++ b/2020/day-02/cleanupPasswords.test.js @@ -1,6 +1,6 @@ /* eslint-env mocha */ const { expect } = require('chai') -const { splitRecord, splitRule, isValidPassword, isValidRecord } = require('./cleanupPasswords') +const { splitRecord, old } = require('./cleanupPasswords') const testData = [ '1-3 a: abcde', @@ -22,7 +22,7 @@ describe('--- Day 2: Password Philosophy ---', () => { it('splits a password formatting rule into component parts', () => { testData.forEach((row, idx) => { const { rule, password } = splitRecord(row) - const { min, max, char } = splitRule(rule) + const { min, max, char } = old.splitRule(rule) expect(`${min}-${max} ${char}: ${password}`).to.equal(testData[idx]) }) }) @@ -32,14 +32,14 @@ describe('--- Day 2: Password Philosophy ---', () => { const expectedResults = [true, false, true] testData.forEach((row, idx) => { const { rule, password } = splitRecord(row) - const { min, max, char } = splitRule(rule) - expect(isValidPassword({ min, max, char }, password)) + const { min, max, char } = old.splitRule(rule) + expect(old.isValidPassword({ min, max, char }, password)) .to.equal(expectedResults[idx]) }) }) it('won\'t allow more than the specified character count', () => { const badPass = 'abcabcabcabc' - expect(isValidPassword({ min: 2, max: 3, char: 'a' }, badPass)) + expect(old.isValidPassword({ min: 2, max: 3, char: 'a' }, badPass)) .to.equal(false) }) }) @@ -47,7 +47,7 @@ describe('--- Day 2: Password Philosophy ---', () => { it('checks if a specified record contains valid rule and password', () => { const expectedResults = [true, false, true] testData.forEach((row, idx) => { - expect(isValidRecord(row)) + expect(old.isValidRecord(row)) .to.equal(expectedResults[idx]) }) })