Skip to content

Commit

Permalink
refactor: enforce code robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
airscripts committed Jul 30, 2023
1 parent 5ffcabc commit 2ebcc80
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions hello.anal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ† πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ† πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ† πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ† πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†
1 change: 1 addition & 0 deletions hello.anus
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const COMPILE = 'compile';
export const ANALLIFY = 'anallify';
export const STRINGIFY = 'stringify';
export const ANAL_CHARACTERS = 'πŸ‘πŸ†';
export const GRAMMAR = `${ANAL_CHARACTERS} `;
1 change: 1 addition & 0 deletions lib/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const ERROR = {
fileNotFound: 'File not found.',
missingArgument: 'Missing argument.',
notString: 'Only strings are accepted',
notAcceptedByGrammar: 'There are some characters not accepted by grammar.',
};

export const SUCCESS = {
Expand Down
4 changes: 4 additions & 0 deletions lib/regexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ANAL_CHARACTERS, GRAMMAR } from './constants.js';

export const matcher = new RegExp(`^[${GRAMMAR}]+$`, 'u');
export const sequence = new RegExp(`${ANAL_CHARACTERS}`, 'g');
34 changes: 23 additions & 11 deletions lib/std.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs';
import { checker } from './utils.js';
import { ERROR, SUCCESS } from './dictionary.js';
import { ANAL_CHARACTERS } from './constants.js';
import { matcher, sequence } from './regexp.js';

export function anallify(string) {
if (!checker(string)) {
Expand Down Expand Up @@ -34,38 +35,49 @@ export function stringify(anal) {
throw new Error(ERROR.missingArgument);
}

if (!matcher.test(anal)) {
throw new Error(ERROR.notAcceptedByGrammar);
}

let string = '';
const fragments = anal.split(' ');
const rule = new RegExp(`${ANAL_CHARACTERS}`, 'g');

for (let index = 0; index < fragments.length; index += 1) {
const fragment = fragments[index];
const character = String.fromCharCode(((fragment.match(rule) || []).length));
const character = String.fromCharCode(((fragment.match(sequence)).length));
string = string.concat(character);
}

return string;
}

export function run(file) {
let contents = null;

try {
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
return stringify(contents);
contents = fs.readFileSync(file, { encoding: 'utf-8' });
} catch (error) {
throw new Error(ERROR.fileNotFound);
}

return stringify(contents);
}

export function compile(file) {
let contents = null;
const fileOptions = { encoding: 'utf-8' };

try {
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
let filename = file.split('.');
filename = filename.filter((element, index) => index < filename.length - 1);
filename = filename.join('.');
fs.writeFileSync(`${filename}.anal`, anallify(contents), { encoding: 'utf-8' });
process.stdout.write(`${SUCCESS.compileSuccess}`);
return true;
contents = fs.readFileSync(file, fileOptions);
} catch (error) {
throw new Error(ERROR.fileNotFound);
}

let filename = file.split('.');
filename = filename.filter((element, index) => index < filename.length - 1);
filename = filename.join('.');

fs.writeFileSync(`${filename}.anal`, anallify(contents), fileOptions);
process.stdout.write(`${SUCCESS.compileSuccess}`);
return true;
}
1 change: 1 addition & 0 deletions tests/seeds.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const NUMERIC_INPUT = 1;
export const EMPTY_STRING_INPUT = '';
export const NOT_ACCEPTED_INPUT = 'πŸ‘πŸ† a';

export const STRINGIFY_CORRECT_OUTPUT = 'B';
export const STRINGIFY_WRONG_OUTPUT = 'πŸ‘πŸ†πŸ‘πŸ†';
Expand Down
7 changes: 7 additions & 0 deletions tests/std.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RUN_WRONG_OUTPUT,
ANAL_FILE_LOCATION,
EMPTY_STRING_INPUT,
NOT_ACCEPTED_INPUT,
RUN_CORRECT_OUTPUT,
ANALLIFY_WRONG_OUTPUT,
STRINGIFY_WRONG_OUTPUT,
Expand Down Expand Up @@ -60,6 +61,12 @@ describe('Stringify', () => {
Error(ERROR.missingArgument),
);
});

test('Throw error if there are grammar-refused characters', () => {
expect(() => stringify(NOT_ACCEPTED_INPUT)).toThrowError(
Error(ERROR.notAcceptedByGrammar),
);
});
});

describe('Run', () => {
Expand Down

0 comments on commit 2ebcc80

Please sign in to comment.