Skip to content

Commit

Permalink
Merge pull request #17 from carvilsi/fix-repeated
Browse files Browse the repository at this point in the history
Fix repeated
  • Loading branch information
carvilsi authored May 8, 2024
2 parents b612d1b + 97ef752 commit 8301aa5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
14 changes: 7 additions & 7 deletions benchmark/report.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
┌─────────┬──────────────────────────────────────┬──────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────────────────────────────┼──────────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'random face' │ '1,132,887' │ 882.6998208121295 │ '±2.41%' │ 113289
│ 1 │ 'random face with description' │ '888,523' │ 1125.4625955230447 │ '±3.46%' │ 88853
│ 2 │ 'get array with all the faces' │ '6,361' │ 157201.19309262166 │ '±0.82%' │ 637
│ 3 │ 'get all the faces for pretty print' │ '11,622,904' │ 86.03701568705623 │ '±0.87%' │ 1162291
│ 4 │ 'get one by name; more than one' │ '13,077' │ 76467.84633027107 │ '±0.80%' │ 1308
│ 5 │ 'get one by name; just one' │ '13,686' │ 73065.91818846014 │ '±0.97%' │ 1369
│ 6 │ 'get array of faces by name' │ '12,887' │ 77593.82544608272 │ '±1.22%' │ 1289
│ 0 │ 'random face' │ '1,097,275' │ 911.3482429280396 │ '±2.38%' │ 109728
│ 1 │ 'random face with description' │ '825,915' │ 1210.7779809180963 │ '±3.54%' │ 82592
│ 2 │ 'get array with all the faces' │ '6,267' │ 159560.75438595744 │ '±1.16%' │ 627
│ 3 │ 'get all the faces for pretty print' │ '10,018,717' │ 99.81317492336673 │ '±0.74%' │ 1001873
│ 4 │ 'get one by name; more than one' │ '12,923' │ 77376.67749419436 │ '±1.22%' │ 1293
│ 5 │ 'get one by name; just one' │ '13,384' │ 74711.69977594598 │ '±1.31%' │ 1339
│ 6 │ 'get array of faces by name' │ '11,587' │ 86297.92062122496 │ '±2.16%' │ 1159
└─────────┴──────────────────────────────────────┴──────────────┴────────────────────┴──────────┴─────────┘
21 changes: 19 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,26 @@ function collectFacesToArray(obj) {
}
}

// for the faces (property) from object (obj) that
// exists at reducedFaces:
// - removes from the object (obj)
// - concatenates to the array on reducedFaces
function cleanRepeatedFaces(obj) {
for (const property in obj) {
if (Object.hasOwn(reducedFaces, property)) {
reducedFaces[property] = reducedFaces[property]
.concat(obj[property]);
delete obj[property];
}
}
return obj;
}

function reduceFacesObject(obj) {
for (const property in obj) {
if (!Array.isArray(obj) && Array.isArray(obj[property])) {
reducedFaces = { ...reducedFaces, ...obj };
const cleanedObj = cleanRepeatedFaces(obj);
reducedFaces = { ...reducedFaces, ...cleanedObj };
break;
}
if (typeof obj[property] === 'object') {
Expand Down Expand Up @@ -105,6 +121,7 @@ const facetxt = {
};

// get the reduced object with faces for random and like functions
reduceFacesObject(faces);
const facesObj = structuredClone(faces);
reduceFacesObject(facesObj);

export default facetxt;
13 changes: 12 additions & 1 deletion tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import test from 'ava';
import facetxt from '../src/index.js';

const TOTAL_FACES = 754;
const TOTAL_FACES = 753;
const WHOLE_EMBARRASSED_FACES = [
':$', '://)',
'://3', '(^^ゞ',
Expand All @@ -14,6 +14,11 @@ const WHOLE_EMBARRASSED_FACES = [
'(#^.^#)', '(^ ^;)',
'(⁄⁄•⁄ω⁄•⁄⁄)'
];
const WHOLE_ZOIDBERG_FACES = [
'(V) (;, ;) (V)',
'(V)(°, °)(V)',
'(V) (°,,,,°) (V)'
];

let arrayOfFaces = null;
let face = null;
Expand Down Expand Up @@ -71,6 +76,12 @@ test('should retrieve the array related with name', (t) => {
t.like(faces, WHOLE_EMBARRASSED_FACES);
});

test('should retrieve the array related with zoidberg', (t) => {
const faces = facetxt.likes('zoidberg');

t.like(faces, WHOLE_ZOIDBERG_FACES);
});

test('should not retrieve the array related with name, because does not exists', (t) => {
const faces = facetxt.likes('foz');

Expand Down

0 comments on commit 8301aa5

Please sign in to comment.