Skip to content

Commit 568ffed

Browse files
chore: polishing
1 parent 312e401 commit 568ffed

File tree

9 files changed

+58
-32
lines changed

9 files changed

+58
-32
lines changed

src/base64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBrowser } from './helpers/index'
1+
import { isBrowser } from './helpers'
22

33
// Courtesy: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
44

src/dtmf.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { throwOrSilent } from './helpers/index'
1+
import { mergeObjects, throwOrSilent, toNumber } from './helpers'
22

33
const decode = (input, options = {}) => {
44
options = { ...DEFAULT_OPTIONS, ...options }
@@ -32,9 +32,19 @@ const decode = (input, options = {}) => {
3232

3333
const includesKey = i => ([key]) => i.includes(key)
3434

35-
const getLookup = mode => mode === 'include' ? LOOKUP : Object.entries(LOOKUP).map(([k, rowObj]) => {
36-
return Object.entries(rowObj).reduce((acc, [rowK, v]) => ({ ...acc, ...{ [mode === 'sum' ? Number(rowK) + Number(k) : Number(rowK) - Number(k)]: v } }), {})
37-
}).reduce((acc, obj) => ({ ...acc, ...obj }), {})
35+
const getLookup = mode => mode === 'include' ? LOOKUP : getSumOrDiffLookup(mode)
36+
37+
const getSumOrDiffLookup = mode => Object.entries(LOOKUP)
38+
.map(([colFrequency, rowObject]) =>
39+
Object.entries(rowObject).reduce((acc, cellObject) => mergeObjects(acc, getLookupObject(mode, colFrequency, cellObject)), {}))
40+
.reduce(mergeObjects, {})
41+
42+
const getLookupObject = (mode, colFrequency, [rowFrequency, character]) => {
43+
[rowFrequency, colFrequency] = toNumber(rowFrequency, colFrequency)
44+
45+
const lookupKey = mode === 'sum' ? rowFrequency + colFrequency : rowFrequency - colFrequency
46+
return { [lookupKey]: character }
47+
}
3848

3949
const encode = (input, options = {}) => {
4050
options = { ...DEFAULT_OPTIONS, ...options }
@@ -44,10 +54,8 @@ const encode = (input, options = {}) => {
4454
return [...input].map(i => {
4555
const result = Object.entries(LOOKUP).map(([key, freqOb]) => {
4656
const foundFreq = Object.entries(freqOb).find(([k, v]) => v === i)
47-
if (foundFreq) {
48-
return encodeResult([key, foundFreq[0]], options)
49-
}
50-
return false
57+
58+
return foundFreq ? encodeResult([key, foundFreq[0]], options) : false
5159
}).find(o => o)
5260

5361
return !result ? throwOrSilent(options, 'Invalid input') : result
@@ -60,7 +68,7 @@ const encodeResult = (frequencies, { mode, invertedOutput, connector }) => {
6068
frequencies.reverse()
6169
}
6270

63-
frequencies = frequencies.map(f => Number(f))
71+
frequencies = toNumber(...frequencies)
6472

6573
const resultObjects = {
6674
include: `${frequencies[0]}${connector}${frequencies[1]}`,

src/fractionatedMorse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import morse from './morse'
2-
import { throwOrSilent } from './helpers/index'
2+
import { throwOrSilent } from './helpers'
33

44
export const decode = (input, options = {}) => {
55
options = { ...DEFAULT_OPTIONS, ...options }

src/helpers/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ export const modInverse = (number, mod) => {
4343
// Now find the mod inverse number or return false. Mod inverse: (i * number = 1) % mod
4444
return (rangeArray.find(i => i * number % mod === 1) % mod) || false
4545
}
46+
47+
export const toNumber = (...args) => args.map(a => Number(a))
48+
49+
export const mergeObjects = (a, b) => ({ ...a, ...b })

src/manchester.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { throwOrSilent } from './helpers/index'
1+
import { throwOrSilent } from './helpers'
22

33
const decode = (input, options = {}) => {
44
options = { ...DEFAULT_OPTIONS, ...options }

src/morse.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
import { throwOrSilent } from './helpers/index'
1+
import { throwOrSilent } from './helpers'
22

33
export const decode = (input, options = {}) => {
44
options = { ...DEFAULT_OPTIONS, ...options }
5-
return input.split(options.separator).map(c => {
6-
const decodedCharacter = Object.entries(ALPHABET).find(([k, v]) => v === c)
5+
6+
return input.split(options.separator).map(character => {
7+
const decodedCharacter = Object.entries(ALPHABET).find(([_, morse]) => morse === character)
78

89
if (decodedCharacter) {
910
return decodedCharacter[0]
1011
}
11-
throwOrSilent(options, `Undecodable character ${c}`)
12+
throwOrSilent(options, `Undecodable character ${character}`)
1213

13-
return options.omitUnknownCharacter ? '' : c
14+
return options.omitUnknownCharacter ? '' : character
1415
}).join('')
1516
}
1617

1718
export const encode = (input, options = {}) => {
1819
options = { ...DEFAULT_OPTIONS, ...options }
1920

20-
return [...input.toUpperCase()].map(c => {
21-
const encodedCharacter = Object.entries(ALPHABET).find(([k]) => k === c)
21+
return [...input.toUpperCase()].map(character => {
22+
const encodedCharacter = Object.entries(ALPHABET).find(([clearCharacter]) => clearCharacter === character)
2223

2324
if (encodedCharacter) {
2425
return encodedCharacter[1]
2526
}
26-
throwOrSilent(options, `Unencodable character ${c}`)
2727

28-
return options.omitUnknownCharacter ? '' : c
28+
throwOrSilent(options, `Unencodable character ${character}`)
29+
30+
return options.omitUnknownCharacter ? '' : character
2931
}).join(options.separator)
3032
}
3133

src/multiTap.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { throwOrSilent } from './helpers/index'
1+
import { mergeObjects, throwOrSilent } from './helpers'
22

33
export const encode = (input, options = {}) => {
44
options = { ...DEFAULT_OPTIONS, ...options }
@@ -17,31 +17,44 @@ export const encode = (input, options = {}) => {
1717

1818
export const decode = (input, options = {}) => {
1919
options = { ...DEFAULT_OPTIONS, ...options }
20-
const alphabet = alphabetWithSpaceKey(options.customMapping)
2120

21+
const alphabet = alphabetWithSpaceKey(options.customMapping)
2222
const invalidInputRegex = /[^\d^*# ]/g
23+
const exponentFormRegex = /\d\^\d ?/g
24+
const normalFormRegex = /(([79])\2{0,4}|([234568])\3{0,2}|([01*#])\4{0,2}) ?/g
2325

2426
// Validate input
2527
if (input.match(invalidInputRegex)) {
2628
if (options.failOnUnknownCharacter) {
2729
throw Error(`Undecodable characters`)
28-
} else {
29-
input.replace(invalidInputRegex)
3030
}
31+
32+
input.replace(invalidInputRegex)
3133
}
3234

3335
if (!input.length) {
3436
return ''
3537
}
3638

37-
const capturedInput = options.exponentForm ? input.match(/\d\^\d ?/g) : input.match(/(([79])\2{0,4}|([234568])\3{0,2}|([01*#])\4{0,2}) ?/g)
39+
const capturedInput = options.exponentForm ? input.match(exponentFormRegex) : input.match(normalFormRegex)
40+
3841
return capturedInput.map(expr => {
3942
expr = expr.replace(/ /g, '')
40-
return options.exponentForm ? alphabet[expr[0]][expr[2] - 1] : alphabet[expr[0]][expr.length - 1]
43+
/*
44+
* Retrieve the letter from the lookup object.
45+
* In exponent form use the number as key and get the letter on the exponent index
46+
* In "normal form" use the number as key as well, but determine the latter based on the length of the expression
47+
* Subtract one because array indices start at 0
48+
*/
49+
const cellIdentifier = (options.exponentForm ? expr[2] : expr.length) - 1
50+
return alphabet[expr[0]][cellIdentifier]
4151
}).join('')
4252
}
4353

44-
const alphabetWithSpaceKey = customMapping => typeof customMapping === 'object' ? { ...ALPHABET, ...customMapping } : ALPHABET
54+
const alphabetWithSpaceKey = customMapping =>
55+
customMapping && typeof customMapping === 'object'
56+
? mergeObjects(ALPHABET, customMapping)
57+
: ALPHABET
4558

4659
const DEFAULT_OPTIONS = {
4760
customMapping: {

src/pollux.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-unused-vars */
22
import morse from './morse'
3-
import { randomInRange } from './helpers/index'
3+
import { randomInRange } from './helpers'
44

55
export const decode = (input, keys = {}, morseOptions = {}) => {
66
sanitizeKeys(keys)

src/polybius.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ const DEFAULT_OPTIONS = {
6666
}
6767

6868
const prepareGridString = options =>
69-
[...options.key.toUpperCase(), ...getAlphabet(options)].reduce((acc, letter) => {
70-
return !acc.includes(letter) ? acc.concat(letter) : acc
71-
})
69+
[...options.key.toUpperCase(), ...getAlphabet(options)]
70+
.reduce((acc, letter) => !acc.includes(letter) ? acc.concat(letter) : acc)
7271

7372
const getAlphabet = options => {
7473
const [substitute, toReplace] = [...options.equalLetters]

0 commit comments

Comments
 (0)