Skip to content

Commit e73665e

Browse files
refactor: small changes
1 parent 37addfb commit e73665e

File tree

8 files changed

+49
-43
lines changed

8 files changed

+49
-43
lines changed

src/dtmf.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ const includesKey = i => ([key]) => i.includes(key)
3535

3636
const getLookup = mode => mode === 'include' ? LOOKUP : getSumOrDiffLookup(mode)
3737

38-
const getSumOrDiffLookup = mode => Object.entries(LOOKUP)
39-
.map(([colFrequency, rowObject]) =>
40-
Object.entries(rowObject).reduce((acc, cellObject) => mergeObjects(acc, getLookupObject(mode, colFrequency, cellObject)), {}))
41-
.reduce(mergeObjects, {})
38+
const getSumOrDiffLookup = mode => {
39+
const getLookupObjectForMode = getLookupObject(mode)
40+
return Object.entries(LOOKUP)
41+
.map(([colFrequency, rowObject]) => Object.entries(rowObject)
42+
.reduce((acc, cellObject) => mergeObjects(acc, getLookupObjectForMode(colFrequency, cellObject)), {}))
43+
.reduce(mergeObjects, {})
44+
}
4245

43-
const getLookupObject = (mode, colFrequency, [rowFrequency, character]) => {
46+
const getLookupObject = mode => (colFrequency, [rowFrequency, character]) => {
4447
[rowFrequency, colFrequency] = toNumber(rowFrequency, colFrequency)
4548

46-
const lookupKey = mode === 'sum' ? rowFrequency + colFrequency : rowFrequency - colFrequency
49+
const lookupKey = mode === MODES.sum ? rowFrequency + colFrequency : rowFrequency - colFrequency
4750
return { [lookupKey]: character }
4851
}
4952

@@ -52,19 +55,21 @@ const encode = (input, options = {}) => {
5255

5356
checkMode(options)
5457

58+
const encodeResultWithOptions = encodeResult(options)
59+
5560
return [...input].map(i => {
5661
const result = Object.entries(LOOKUP).map(([key, freqOb]) => {
5762
const foundFreq = Object.entries(freqOb).find(([, v]) => v === i)
5863

59-
return foundFreq ? encodeResult([key, foundFreq[0]], options) : false
64+
return foundFreq ? encodeResultWithOptions([key, foundFreq[0]]) : false
6065
}).find(Boolean)
6166

6267
return !result ? throwOrSilent(options, 'Invalid input') : result
6368
}).join(options.separator)
6469
}
6570

66-
const encodeResult = (frequencies, { mode, invertedOutput, connector }) => {
67-
// If output should be inverted, reverse array
71+
const encodeResult = ({ mode, invertedOutput, connector }) => frequencies => {
72+
// If output should be inverted, reverse array (mutates array)
6873
if (invertedOutput) {
6974
frequencies.reverse()
7075
}
@@ -81,12 +86,12 @@ const encodeResult = (frequencies, { mode, invertedOutput, connector }) => {
8186
}
8287

8388
const checkMode = ({ mode }) => {
84-
if (!TYPES.includes(mode)) {
89+
if (!Object.values(MODES).includes(mode)) {
8590
throw new Error('Unknown mode')
8691
}
8792
}
8893

89-
const TYPES = ['include', 'sum', 'diff']
94+
const MODES = { include: 'include', sum: 'sum', diff: 'diff' }
9095

9196
const DEFAULT_OPTIONS = {
9297
mode: 'include',

src/fractionatedMorse.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ export const decode = (input, options = {}) => {
55
options = { ...DEFAULT_OPTIONS, ...options }
66
const morseOptions = { ...DEFAULT_MORSE_OPTION, ...{ failOnUnknownCharacter: options.failOnUnknownCharacter } }
77

8-
let morseCode = [...input].map(c => {
9-
const decodedCharacterIndex = options.keyAlphabet.indexOf(c)
10-
if (decodedCharacterIndex !== -1) {
11-
return ENCODED_ALPHABET[decodedCharacterIndex]
12-
}
13-
14-
return throwOrSilent(options, `Undecodable character ${c}`)
15-
})
8+
let morseCode = [...input]
9+
.map(c => {
10+
const decodedCharacterIndex = options.keyAlphabet.indexOf(c)
11+
if (decodedCharacterIndex !== -1) {
12+
return ENCODED_ALPHABET[decodedCharacterIndex]
13+
}
14+
15+
return throwOrSilent(options, `Undecodable character ${c}`)
16+
})
1617
.join('')
1718

1819
// Remove padding if needed
@@ -38,7 +39,8 @@ export const encode = (input, options = {}) => {
3839
}
3940

4041
return throwOrSilent(options, `Unencodable character ${c}`)
41-
}).join('')
42+
})
43+
.join('')
4244
}
4345

4446
const morseCodeFromInput = (input, morseOptions) => {

src/helpers/rotateAndMultiply.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const isAllowed = (characterType, allowedTypes) => {
1818
}
1919

2020
const modifyCharacter = (c, options) => {
21-
const asciiOfset = ROTATE_AND_MULTIPLY_ASCII[options.type]
21+
const asciiOffset = ROTATE_AND_MULTIPLY_ASCII[options.type]
2222
const parsedCharacter = parseInt(c, 36) - (options.type === ROTATE_AND_MULTIPLY_TYPES.NUMBER ? 0 : 10)
2323
const mod = options.type === ROTATE_AND_MULTIPLY_TYPES.NUMBER ? 10 : 26
2424
const multiplicationKey = options.decode ? modInverse(options.keys[0], mod) : options.keys[0]
@@ -32,7 +32,7 @@ const modifyCharacter = (c, options) => {
3232
transformedCode += mod
3333
}
3434

35-
return String.fromCharCode(asciiOfset + (transformedCode % mod))
35+
return String.fromCharCode(asciiOffset + (transformedCode % mod))
3636
}
3737

3838
const DEFAULT_ROTATE_AND_MULTIPLY_OPTIONS = {
@@ -43,9 +43,10 @@ const DEFAULT_ROTATE_AND_MULTIPLY_OPTIONS = {
4343
decode: false
4444
}
4545

46-
const getType = c => (c >= 'a' && c <= 'z') ? ROTATE_AND_MULTIPLY_TYPES.LOWERCASE : (c >= 'A' && c <= 'Z')
47-
? ROTATE_AND_MULTIPLY_TYPES.UPPERCASE : (c >= '0' && c <= '9')
48-
? ROTATE_AND_MULTIPLY_TYPES.NUMBER : ROTATE_AND_MULTIPLY_TYPES.OTHER
46+
const getType = c => (c >= 'a' && c <= 'z')
47+
? ROTATE_AND_MULTIPLY_TYPES.LOWERCASE : (c >= 'A' && c <= 'Z')
48+
? ROTATE_AND_MULTIPLY_TYPES.UPPERCASE : (c >= '0' && c <= '9')
49+
? ROTATE_AND_MULTIPLY_TYPES.NUMBER : ROTATE_AND_MULTIPLY_TYPES.OTHER
4950

5051
export const ROTATE_AND_MULTIPLY_TYPES = {
5152
UPPERCASE: 1,

src/manchester.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const decode = (input, options = {}) => {
55

66
if (input.match(/[^01]/g)) {
77
throwOrSilent(options, 'Invalid Input')
8-
input = [...input].filter(c => '01'.includes(c)).join('')
8+
input = [...input].filter(c => /[01]/.test(c)).join('')
99
}
1010

1111
if (input.length % 2) {

src/multiTap.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,30 @@ export const decode = (input, options = {}) => {
2828
if (options.failOnUnknownCharacter) {
2929
throw Error(`Undecodable characters`)
3030
}
31-
32-
input.replace(invalidInputRegex)
3331
}
3432

3533
if (!input.length) {
3634
return ''
3735
}
3836

39-
const capturedInput = options.exponentForm ? input.match(exponentFormRegex) : input.match(normalFormRegex)
37+
const capturedInput = input.match(options.exponentForm ? exponentFormRegex : normalFormRegex)
4038

41-
return capturedInput.map(expr => {
42-
expr = expr.replace(/ /g, '')
39+
return capturedInput.map(expression => {
40+
expression = expression.replace(/ /g, '')
4341
/*
4442
* Retrieve the letter from the lookup object.
4543
* In exponent form use the number as key and get the letter on the exponent index
4644
* In "normal form" use the number as key as well, but determine the latter based on the length of the expression
4745
* Subtract one because array indices start at 0
4846
*/
49-
const cellIdentifier = (options.exponentForm ? expr[2] : expr.length) - 1
50-
return alphabet[expr[0]][cellIdentifier]
47+
const cellIdentifier = (options.exponentForm ? expression[2] : expression.length) - 1
48+
return alphabet[expression[0]][cellIdentifier]
5149
}).join('')
5250
}
5351

54-
const alphabetWithSpaceKey = customMapping =>
55-
customMapping && typeof customMapping === 'object'
56-
? mergeObjects(ALPHABET, customMapping)
57-
: ALPHABET
52+
const alphabetWithSpaceKey = customMapping => customMapping && typeof customMapping === 'object'
53+
? mergeObjects(ALPHABET, customMapping)
54+
: ALPHABET
5855

5956
const DEFAULT_OPTIONS = {
6057
customMapping: {

src/pollux.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-unused-vars */
21
import morse from './morse'
32
import { randomInRange } from './helpers'
43

src/polybius.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ const decode = (input, options = {}) => {
4444
const grid = prepareGridString(options)
4545
let squareSize = options.withNumbers ? 6 : 5
4646

47-
return input.split(' ')
47+
return input
48+
.split(' ')
4849
.map(sequence => {
4950
if (!sequence.match(/\d{2}/)) {
5051
return errorExpression(sequence, options)
@@ -65,9 +66,10 @@ const DEFAULT_OPTIONS = {
6566
omitUnknownCharacter: true
6667
}
6768

68-
const prepareGridString = options =>
69-
[...options.key.toUpperCase(), ...getAlphabet(options)]
70-
.reduce((acc, letter) => !acc.includes(letter) ? acc.concat(letter) : acc)
69+
const prepareGridString = options => [
70+
...options.key.toUpperCase(),
71+
...getAlphabet(options)
72+
].reduce((acc, letter) => !acc.includes(letter) ? acc.concat(letter) : acc)
7173

7274
const getAlphabet = options => {
7375
const [substitute, toReplace] = [...options.equalLetters]

test/multiTap.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ describe('decoding', () => {
8585
})
8686

8787
test('with invalid characters and silent fail', () => {
88-
expect(multiTap.decode('$2', silentFailOptions)).toBe('A')
88+
expect(multiTap.decode('$€2%^&', silentFailOptions)).toBe('A')
8989
})
9090
})

0 commit comments

Comments
 (0)