Skip to content

Commit

Permalink
cleanuo
Browse files Browse the repository at this point in the history
  • Loading branch information
jonroig committed Aug 13, 2024
1 parent 8b4f4c6 commit b6b2b93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
31 changes: 16 additions & 15 deletions lib/usBabyNames.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
usBabyNames.js
... written by Jon Roig (@runnr_az) over a weekend in early 2016
**/

import sqlite3 from 'sqlite3';
import path from 'path';
import { fileURLToPath } from 'url';
import { join } from 'path';
import { rejects } from 'assert';
const startYear = 1880;
const endYear = 2023;

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
Expand All @@ -16,7 +18,7 @@ const nameDetailsDb = new sqlite3.Database( join(__dirname, '..', 'sqlite', 'us-


const getDetailed = async (nameInput, sexInput) => {
return new Promise((fulfill, rejects) => {
return new Promise((fulfill, reject) => {
const sql = "SELECT * from usNameDetails where name = ? and sex = ?";
// run the query, return the results...
nameDetailsDb.all(sql, [nameInput, sexInput], (err, rows) => {
Expand Down Expand Up @@ -119,6 +121,11 @@ const get = async (params) => {
});
};

const emptyYearObj = {
births: 0,
rank: 0
};


const getNameRankAndBirthsByYear = async (name, params) => {
return new Promise((fulfill, reject) => {
Expand All @@ -139,7 +146,13 @@ const getNameRankAndBirthsByYear = async (name, params) => {
// run the query, return the results...
nameDataDb.all(sql, queryParams, (err, rows) => {
if (err) {
reject (err);
reject(err);
}

if (params.getEmptyYears) {
for (let x = startYear; x <= endYear; x++) {
outputObj[x] = emptyYearObj;
}
}

// get into the array format we want...
Expand All @@ -150,18 +163,6 @@ const getNameRankAndBirthsByYear = async (name, params) => {
rank: nameObj.rank
};
}

if (params.getEmptyYears) {
for (let x = 1880; x <= 2020; x++) {
if (!outputObj[x]) {
outputObj[x] = {
births: 0,
rank: 0
}
}
}
}

fulfill(outputObj);
});
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "usbabynames",
"version": "2.0.1",
"version": "2.0.2",
"description": "Returns data about baby names used in the United States 1880-2021",
"main": "lib/usBabyNames.js",
"main": "lib/usBabyNames.mjs",
"scripts": {
"test": "echo 'tests?'"
},
Expand All @@ -14,6 +14,7 @@
"directories": {
"example": "examples"
},
"type": "module",
"keywords": [
"names",
"baby",
Expand Down

0 comments on commit b6b2b93

Please sign in to comment.