Skip to content

Commit

Permalink
Merge pull request #4 from carvilsi/solve-repeated-names
Browse files Browse the repository at this point in the history
Solve repeated names
  • Loading branch information
carvilsi authored Mar 13, 2024
2 parents 6eea825 + b0f214c commit 52474ae
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
10 changes: 5 additions & 5 deletions benchmark/report.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
┌─────────┬──────────────────────────────────┬─────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────────────────────────┼─────────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'random face' │ '2,856,094' │ 350.1285012435906 │ '±4.26%' │ 285610
│ 1 │ 'print all the faces' │ '8,072' │ 123873.74487917611 │ '±0.33%' │ 808
│ 2 │ 'get one by name; more than one' │ '14,074' │ 71048.20182377641 │ '±0.77%' │ 1408
│ 3 │ 'get one by name; just one' │ '13,608' │ 73481.83329538068 │ '±1.32%' │ 1361
│ 4 │ 'get array of faces by name' │ '10,948' │ 91336.41477589194 │ '±2.29%' │ 1095
│ 0 │ 'random face' │ '2,773,328' │ 360.5775347400159 │ '±4.54%' │ 277333
│ 1 │ 'print all the faces' │ '6,676' │ 149773.55720754154 │ '±1.99%' │ 668
│ 2 │ 'get one by name; more than one' │ '11,974' │ 83509.6806984314 │ '±1.00%' │ 1198
│ 3 │ 'get one by name; just one' │ '12,495' │ 80026.75449848175 │ '±0.64%' │ 1250
│ 4 │ 'get array of faces by name' │ '11,519' │ 86807.44704583452 │ '±1.41%' │ 1152
└─────────┴──────────────────────────────────┴─────────────┴────────────────────┴──────────┴─────────┘
17 changes: 14 additions & 3 deletions src/faces.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ const canonical = {
kaomoji_faces: {
troubled: [ '(>_<)', '(>_<)>', '(>w<)' ],
baby: [ '(\';\')' ],
nervous_shy_sweat_drop: [
nervous_embarrassed_troubled_shy_sweat_drop: [
'(^^ゞ', '(^_^;)', '(-_-;)', '(~_~;)', '(・.・;)', '(・_・;)',
'(・・;)', '^^;', '^_^;', '(#^.^#)', '(^ ^;)'
],
flushed: [ '(⁄⁄•⁄ω⁄•⁄⁄)' ],
embarrassed_flushed: [ '(⁄⁄•⁄ω⁄•⁄⁄)' ],
smoking: [ '(^.^)y-.o○', '(-.-)y-°°°' ],
sleeping: [ '(-_-)zzz' ],
wink: [ '(^_-)', '(^_-)-☆' ],
Expand All @@ -129,7 +129,18 @@ const canonical = {
'(__)', '_(._.)_', '_(_^_)_', '<(_ _)>', '<m(__)m>', 'm(__)m', 'm(_ _)m'
],
middle_fingers: [ '(凸ಠ益ಠ)凸' ],
questioning: [ '\(°ロ\)', '(/ロ°)/' ]
questioning: [ '\(°ロ\)', '(/ロ°)/' ],
sad_crying: [
'(\'_\')', '(/_;)', '(T_T)', '(;_;)', '(;_;', '(;_:)', '(;O;)', '(:_;)',
'(ToT)', '(T▽T)', ';_;', ';-;', ';n;', ';;', 'Q.Q', 'T.T', 'TnT', 'QQ',
'Q_Q'
],
shame: [ '(ー_ー)!!', '(-.-)', '(-_-)', '(一一)', '(;一_一)' ],
tired: [ '(=_=)' ],
cat: [ '(=^・^=)', '(=^・・^=)', '=^_^=' ],
looking_down_sadness_boredom: [ '(..)', '(._.)' ],
giggling_with_hand_covering_mouth: [ '^m^' ],
confusion: [ '(・・?', '(?_?)' ]
}
};

Expand Down
24 changes: 9 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import faces from './faces.js';

let arrayOfFaces = [];
let textFaces = '';
let faceWithName = undefined;

// Gives a "random" number between 0 and length (inclusive)
function randomInt(length) {
Expand Down Expand Up @@ -62,27 +61,21 @@ function getRandomFace(obj) {
}
}

function collectFaceByName(obj, name, returnArray) {
function collectFaceByName(obj, name) {
for (const property in obj) {
if (Object.prototype.hasOwnProperty.call(obj, property)) {
if (!Array.isArray(obj)) {
const regexp = new RegExp(name, 'g');
if (property.match(regexp)?.length) {
if (Array.isArray(obj[property])) {
if (returnArray) {
arrayOfFaces = obj[property];
} else {
faceWithName = obj[property].length === 1 ?
obj[property][0] :
obj[property][randomInt(obj[property].length)];
}
arrayOfFaces.push(obj[property]);
} else {
collectFaceByName(obj[property], name, returnArray);
collectFaceByName(obj[property], name);
}
}
}
if (typeof obj[property] === 'object') {
collectFaceByName(obj[property], name, returnArray);
collectFaceByName(obj[property], name);
}
}
}
Expand All @@ -103,14 +96,15 @@ const facetxt = {
return getRandomFace(faces);
},
like(name) {
faceWithName = undefined;
arrayOfFaces = [];
collectFaceByName(faces, name);
return faceWithName;
const arr = arrayOfFaces.flat(1);
return arr.length === 1 ? arr[0] : arr[randomInt(arr.length)];
},
likes(name) {
arrayOfFaces = [];
collectFaceByName(faces, name, true);
return arrayOfFaces;
collectFaceByName(faces, name);
return arrayOfFaces.flat(1);
}
};

Expand Down
18 changes: 15 additions & 3 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
import test from 'ava';
import facetxt from '../src/index.js';

const TOTAL_FACES = 320;
const WHOLE_EMBARRASSED_FACES = [
':$', '://)',
'://3', '(^^ゞ',
'(^_^;)', '(-_-;)',
'(~_~;)', '(・.・;)',
'(・_・;)', '(・・;)',
'^^;', '^_^;',
'(#^.^#)', '(^ ^;)',
'(⁄⁄•⁄ω⁄•⁄⁄)'
];

test('should retrieve an array of faces and a random face', (t) => {
const arrayOfFaces = facetxt.all;

t.true(arrayOfFaces.length > 0, 'the array of faces is empty');
t.is(arrayOfFaces.length, 287);
t.is(arrayOfFaces.length, TOTAL_FACES);

const face = facetxt.rand;

Expand All @@ -24,7 +36,7 @@ test('should retrieve string with all faces fo pretty print', (t) => {
test('should retrieve a random face by name', (t) => {
const face = facetxt.like('embarrassed');

t.true([ ':$', '://)', '://3' ].indexOf(face) >= 0);
t.true(WHOLE_EMBARRASSED_FACES.indexOf(face) >= 0);
});

test('should not retrieve a random face by name because does not exists', (t) => {
Expand All @@ -36,7 +48,7 @@ test('should not retrieve a random face by name because does not exists', (t) =>
test('should retrieve the array related with name', (t) => {
const faces = facetxt.likes('embarrassed');

t.like(faces, [ ':$', '://)', '://3' ]);
t.like(faces, WHOLE_EMBARRASSED_FACES);
});

test('should not retrieve the array related with name, because does not exists', (t) => {
Expand Down

0 comments on commit 52474ae

Please sign in to comment.