From b6b2b93c2d9708e472c7c1c1d2903430b85b93b5 Mon Sep 17 00:00:00 2001 From: Jon Roig Date: Mon, 12 Aug 2024 21:26:25 -0700 Subject: [PATCH] cleanuo --- lib/usBabyNames.mjs | 31 ++++++++++++++++--------------- package.json | 5 +++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/usBabyNames.mjs b/lib/usBabyNames.mjs index e1dccb2..1e0a4d3 100644 --- a/lib/usBabyNames.mjs +++ b/lib/usBabyNames.mjs @@ -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 @@ -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) => { @@ -119,6 +121,11 @@ const get = async (params) => { }); }; +const emptyYearObj = { + births: 0, + rank: 0 +}; + const getNameRankAndBirthsByYear = async (name, params) => { return new Promise((fulfill, reject) => { @@ -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... @@ -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); }); }); diff --git a/package.json b/package.json index 1c42957..0ee7c24 100644 --- a/package.json +++ b/package.json @@ -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?'" }, @@ -14,6 +14,7 @@ "directories": { "example": "examples" }, + "type": "module", "keywords": [ "names", "baby",