Skip to content

Commit

Permalink
update rendering algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sensone committed Feb 13, 2017
1 parent 0e564d5 commit dce3fb2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 57 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
Special text converter for easy converting text to something like this:

```
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🎱🎱🎱🙈🙈🙈🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🙈🙈
🙈🎱🎱🎱🎱🙈🙈🎱🎱🎱🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🙈🙈🙈🙈
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
🙈🙈🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🎱🎱🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🎱🎱🙈🙈
🙈🙈🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈
🙈🙈🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🎱🎱🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈
🙈🙈🙈🙈🙈🙈🙈🎱🎱🎱🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🙈🎱🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈
🙈🙈🙈🙈🙈🙈🙈🙈🎱🎱🙈🙈🙈🎱🎱🎱🎱🙈🙈🎱🙈🙈🎱🙈🙈🎱🎱🎱🎱🙈🙈🎱🎱🎱🙈🙈
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🎱🎱🎱🎱🙈🎱🙈🙈🙈🙈🎱🙈🙈🙈🙈🙈🎱🎱🙈🙈🙈🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🎱🙈🙈🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🙈
🙈🎱🎱🎱🎱🙈🎱🎱🎱🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🎱🙈🙈🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🎱🙈🎱🙈🙈🙈🙈
🙈🎱🙈🙈🎱🙈🎱🎱🎱🎱🙈🎱🎱🎱🎱🙈🎱🎱🎱🎱🙈🙈🎱🎱🙈🙈🎱🙈🙈🙈🙈
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
🙈🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🙈🎱🎱🙈🙈🎱🎱🎱🙈🙈🎱🙈🙈🙈🙈🎱🎱🎱🙈🙈
🙈🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🎱🙈🙈🎱🙈🎱🙈🙈🎱🙈🎱🙈🙈🙈🙈🎱🙈🙈🎱🙈
🙈🙈🙈🙈🙈🙈🎱🙈🙈🎱🙈🎱🙈🙈🎱🙈🎱🎱🎱🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🎱🙈
🙈🙈🙈🙈🙈🙈🎱🎱🎱🎱🙈🎱🙈🙈🎱🙈🎱🙈🎱🙈🙈🎱🙈🙈🙈🙈🎱🙈🙈🎱🙈
🙈🙈🙈🙈🙈🙈🙈🎱🎱🙈🙈🙈🎱🎱🙈🙈🎱🙈🙈🎱🙈🎱🎱🎱🎱🙈🎱🎱🎱🙈🙈
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈
```

## Installing
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "text2smiles",
"version": "1.0.3",
"version": "1.1.0",
"description": "Converting text to smiles",
"keywords": [
"emoji",
Expand All @@ -15,7 +15,8 @@
"transpile-es2015": "babel src -d lib",
"test": "./node_modules/mocha/bin/mocha --compilers js:babel-core/register ./src/tests",
"test-all": "npm run lint && npm run test",
"preinstall": "npm run transpile-es2015"
"preinstall": "npm run transpile-es2015",
"deploy": "npm run test-all && npm publish"
},
"author": "Evgeni Korsunov<5345043@gmail.com>",
"dependencies": {
Expand Down
16 changes: 9 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { ABC, LETTER_WIDTH, LETTER_HEIGHT } from './serift';

const STRING_LENGTH = 8;

export default function convertText2Smiles(text = '', symb1 = '🤘', symb2 = '💀', strLength = STRING_LENGTH) {
export default function convertText2Smiles(text = '', bgSymbol = '🤘', textSymbol = '💀', strLength = STRING_LENGTH) {
if (!text.length) return text;

function getLetter(symb, lineIndex) {
let letter;
const POSITION = lineIndex * LETTER_WIDTH;

switch (symb) {
case "'":
case '"':
letter = ABC.quote;
break;
default:
Expand All @@ -20,30 +22,30 @@ export default function convertText2Smiles(text = '', symb1 = '🤘', symb2 = '
letter = ABC[' '];
}

return letter.slice((lineIndex * LETTER_WIDTH), (lineIndex * LETTER_WIDTH) + LETTER_WIDTH);
return letter.slice(POSITION, POSITION + (LETTER_WIDTH - 1));
}

function generateLine(str, result = '') {
function generateLine(str, result = '', secondLine = false) {
let currentResult = result;
const currentString = str.slice(0, strLength);
const surplusString = str.slice(strLength);

for (let lineIndex = 0; lineIndex < LETTER_HEIGHT; lineIndex += 1) {
for (let lineIndex = secondLine ? 1 : 0; lineIndex < LETTER_HEIGHT; lineIndex += 1) {
for (let letterIndex = 0; letterIndex < currentString.length; letterIndex += 1) {
const letter = currentString[letterIndex];

currentResult += getLetter(letter, lineIndex);
}

currentResult += '\n';
currentResult += `${bgSymbol}\n`;
}

if (surplusString.length > 0) {
return generateLine(surplusString, currentResult);
return generateLine(surplusString, currentResult, true);
}

return currentResult;
}

return generateLine(text.toLowerCase()).replace(/\./g, symb1).replace(/\*/g, symb2);
return generateLine(text.toLowerCase()).replace(/\./g, bgSymbol).replace(/\*/g, textSymbol);
}
6 changes: 3 additions & 3 deletions src/serift.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ const N = `

const O = `
......
.****.
..**..
.*..*.
.*..*.
.*..*.
.****.
..**..
......
`;

Expand Down Expand Up @@ -242,7 +242,7 @@ const Y = `
......
.*..*.
.*..*.
.****.
..***.
....*.
.****.
......
Expand Down
72 changes: 41 additions & 31 deletions src/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Converting text to smiles:', () => {
describe('Testing with 4 arguments, text with symbols: X and Y', () => {
const LETTERS_IN_ROW = 1;
const TEXT = 'ok';
const ROWS_COUNT = ((TEXT.length / LETTERS_IN_ROW) * LETTER_HEIGHT) + 1;
const ROWS_COUNT = ((TEXT.length / LETTERS_IN_ROW) * LETTER_HEIGHT);

beforeEach(() => {
result = convertText2Smiles('ok', 'X', 'Y', LETTERS_IN_ROW);
Expand All @@ -81,8 +81,21 @@ describe('Converting text to smiles:', () => {
});

it('result should be equal template', () => {
let template = 'XXXXXX\nXYYYYX\nXYXXYX\nXYXXYX\nXYXXYX\nXYYYYX\nXXXXXX\n';
template += 'XXXXXX\nXYXXYX\nXYXYXX\nXYYXXX\nXYXYXX\nXYXXYX\nXXXXXX\n';
let template = '';

template += 'XXXXXX\n';
template += 'XXYYXX\n';
template += 'XYXXYX\n';
template += 'XYXXYX\n';
template += 'XYXXYX\n';
template += 'XXYYXX\n';
template += 'XXXXXX\n';
template += 'XYXXYX\n';
template += 'XYXYXX\n';
template += 'XYYXXX\n';
template += 'XYXYXX\n';
template += 'XYXXYX\n';
template += 'XXXXXX\n';

assert.equal(result, template);
});
Expand All @@ -92,34 +105,31 @@ describe('Converting text to smiles:', () => {
it('result should be equal template', () => {
let template = '';

template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,@@,,,,@,,,,@,,,,,@,,,,\n';
template += ',,@@,,,,@,,,,@,,,,,@,,,,\n';
template += ',,@@,,,,,,,,,@,,,,,@,,,,\n';
template += ',,@@,,,,,,,,,@,,,,,@,,,,\n';
template += ',,@@,,,,,,,,,@@@@,,@@@@,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,@@@,,,@@@@,,,,,,,\n';
template += ',,,,,,,@,,@,,@,,,,,,,,,,\n';
template += ',,,,,,,@@@,,,@@@,,,,,,,,\n';
template += ',,,,,,,@,,@,,@,,,,,,,,,,\n';
template += ',,,,,,,@@@,,,@@@@,,,,,,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',@@@,,,,@@,,,,@@@,,@,,@,\n';
template += ',@,,@,,@,,@,,@,,,,,@,@,,\n';
template += ',@@@,,,@@@@,,@,,,,,@@,,,\n';
template += ',@,,@,,@,,@,,@,,,,,@,@,,\n';
template += ',@@@,,,@,,@,,,@@@,,@,,@,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,,@@,,,,@@,,,,@@,,\n';
template += ',,,,,,,,@@,,,,@@,,,,@@,,\n';
template += ',,,,,,,,@@,,,,@@,,,,@@,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,,@@,,,,@@,,,,@@,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,\n';
template += ',,@@,,,@,,,@,,,,@,,,,\n';
template += ',,@@,,,@,,,@,,,,@,,,,\n';
template += ',,@@,,,,,,,@,,,,@,,,,\n';
template += ',,@@,,,,,,,@,,,,@,,,,\n';
template += ',,@@,,,,,,,@@@@,@@@@,\n';
template += ',,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,@@@,,@@@@,,,,,,\n';
template += ',,,,,,@,,@,@,,,,,,,,,\n';
template += ',,,,,,@@@,,@@@,,,,,,,\n';
template += ',,,,,,@,,@,@,,,,,,,,,\n';
template += ',,,,,,@@@,,@@@@,,,,,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,\n';
template += ',@@@,,,@@,,,@@@,@,,@,\n';
template += ',@,,@,@,,@,@,,,,@,@,,\n';
template += ',@@@,,@@@@,@,,,,@@,,,\n';
template += ',@,,@,@,,@,@,,,,@,@,,\n';
template += ',@@@,,@,,@,,@@@,@,,@,\n';
template += ',,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,@@,,,@@,,,@@,,\n';
template += ',,,,,,,@@,,,@@,,,@@,,\n';
template += ',,,,,,,@@,,,@@,,,@@,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,\n';
template += ',,,,,,,@@,,,@@,,,@@,,\n';
template += ',,,,,,,,,,,,,,,,,,,,,\n';

result = convertText2Smiles('I\'LL BE BACK^!!!', ',', '@', 4);

Expand Down

0 comments on commit dce3fb2

Please sign in to comment.