Skip to content

Commit

Permalink
refactor[2020-day-02]: prep for different password validation rules
Browse files Browse the repository at this point in the history
Rules provided were for old employers
  • Loading branch information
amclin committed Dec 7, 2020
1 parent be91e9e commit 03a33e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 11 additions & 9 deletions 2020/day-02/cleanupPasswords.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions 2020/day-02/cleanupPasswords.test.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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])
})
})
Expand All @@ -32,22 +32,22 @@ 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)
})
})
describe('isValidRecord()', () => {
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])
})
})
Expand Down

0 comments on commit 03a33e7

Please sign in to comment.