Skip to content

Commit

Permalink
refactor: move pairing code check into a extra file
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGolms committed Jan 30, 2022
1 parent e81273c commit 0e00f89
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/utils/number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const isNumber = (value: string) => {
return !isNaN(Number(value));
};

export const isPairingCode = (pairingCode: string) => {
return pairingCode.length === 8 && pairingCode.split('').every((char) => isNumber(char));
};
7 changes: 3 additions & 4 deletions src/yargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import yargs from 'yargs/yargs';
import { CATEGORIES } from './config/categories';
import { OUTPUT_FORMATS } from './config/output';
import { Category, OutputFormat } from './types';
import { isNumber } from './utils/number';
import { isPairingCode } from './utils/number';

export const argv = yargs(process.argv.slice(2))
// Attempt to detect the os locale
Expand Down Expand Up @@ -92,11 +92,10 @@ export const argv = yargs(process.argv.slice(2))
);
})
.check(({ pairingCode }) => {
if (pairingCode.length === 8 && pairingCode.split('').every((char) => isNumber(char))) {
if (isPairingCode(pairingCode)) {
return true;
} else {
throw new Error('Pairing Code must be specified with 8 digits');
}
throw new Error('Pairing Code must be specified with 8 digits');
})
.help()
.usage('Usage: homekit-code [commands] [options]')
Expand Down

0 comments on commit 0e00f89

Please sign in to comment.