Skip to content

Commit

Permalink
1910-2023 state data
Browse files Browse the repository at this point in the history
  • Loading branch information
jonroig committed Aug 31, 2024
1 parent bd4a672 commit 66b0962
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 37 deletions.
4 changes: 3 additions & 1 deletion examples/directDbAccess.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const usBabyNamesModule = await import('../lib/usBabyNames.mjs');

const directDbAccess = async () => {
const usBabyNamesModule = await import('../lib/usBabyNames.mjs');

const nameDataDb = usBabyNamesModule.nameDataDb;
const nameDetailsDb = usBabyNamesModule.nameDetailsDb;
const nameDetailsSql = "SELECT * from usNameDetails where name = 'jonathan' and sex = 'm' ";
Expand Down
8 changes: 8 additions & 0 deletions examples/getAllByName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { getAllByName } = await import('../lib/usBabyNames.mjs');

const getAll = async (theName) => {
const nationalNameData = await getAllByName(theName);
console.log('nationalNameData', nationalNameData);
};

getAll('jonathan');
3 changes: 2 additions & 1 deletion examples/getByBirthsRange.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const usBabyNamesModule = await import('../lib/usBabyNames.mjs');

const getByBirthsRange = async () => {
const usBabyNamesModule = await import('../lib/usBabyNames.mjs');
const theNames = await usBabyNamesModule.get({
birthsRange: {start: 0, end: 10},
year: 1975
Expand Down
5 changes: 3 additions & 2 deletions examples/getById.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getById } = await import('../lib/usBabyNames.mjs');

const getByIdExample = async () => {
const usBabyNames = await import('../lib/usBabyNames.mjs');
const theName = await usBabyNames.getById(790921);
const theName = await getById(790921);
console.log('theName', theName);
};

Expand Down
7 changes: 4 additions & 3 deletions examples/getByName.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const getByName = async () => {
const { getByName } = await import('../lib/usBabyNames.mjs');
const { getByName } = await import('../lib/usBabyNames.mjs');

const getByNamed = async () => {
const theNames = await getByName('debbie');
console.log('theNames', theNames);
};

getByName();
getByNamed();
3 changes: 2 additions & 1 deletion examples/getByRankRange.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const usBabyNames = await import('../lib/usBabyNames.mjs');

async function getByRankRange() {
const usBabyNames = await import('../lib/usBabyNames.mjs');
const theNames = await usBabyNames.get({rankRange: {start: 0, end: 10}});
console.log(theNames);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/getByYear.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const usBabyNames = await import('../lib/usBabyNames.mjs');

async function getByYear() {
const usBabyNames = await import('../lib/usBabyNames.mjs');
const theNames = await usBabyNames.getByYear(1975);
console.log(theNames);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/getByYearNameSex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const usBabyNames = await import('../lib/usBabyNames.mjs');

async function getByYearNameSex() {
const usBabyNames = await import('../lib/usBabyNames.mjs');
const theNames = await usBabyNames.get({name: 'jonathan', year: 1975, sex: 'm'});
console.log(theNames);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/getDetailed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const usBabyNames = await import('../lib/usBabyNames.mjs');

const getDetailed = async () => {
const usBabyNames = await import('../lib/usBabyNames.mjs');
const theDetails = await usBabyNames.getDetailed('jonathan', 'm');
console.log('theDetails', theDetails);
};
Expand Down
3 changes: 2 additions & 1 deletion examples/getNameRankAndBirths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const usBabyNames = await import('../lib/usBabyNames.mjs');

async function getNameRankAndBirths() {
const usBabyNames = await import('../lib/usBabyNames.mjs');
const theNames = await usBabyNames.getNameRankAndBirthsByYear('kanye', {sex: 'm', getEmptyYears: true});
console.log(theNames);
}
Expand Down
8 changes: 8 additions & 0 deletions examples/getStateDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { getStateNameData } = await import('../lib/usBabyNames.mjs');

const getStateData = async () => {
const theNames = await getStateNameData('kamala', 'f');
console.log('theNames', theNames);
};

getStateData();
144 changes: 120 additions & 24 deletions lib/usBabyNames.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/**
usBabyNames.js
... written by Jon Roig (@runnr_az) over a weekend in early 2016
then kinda evolved from there
**/

import sqlite3 from 'sqlite3';
import path from 'path';
import { fileURLToPath } from 'url';
import { join } from 'path';

const startYear = 1880;
const endYear = 2023;

Expand All @@ -15,6 +17,101 @@ const __dirname = path.dirname(__filename); // get the name of the directory

const nameDataDb = new sqlite3.Database( join(__dirname, '..', 'sqlite', 'us-name-data.sqlite'));
const nameDetailsDb = new sqlite3.Database( join(__dirname, '..', 'sqlite', 'us-name-details.sqlite'));
const stateNameDb = new sqlite3.Database( join(__dirname, '..', 'sqlite', 'state-name-data.sqlite'));

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

const getAllByName = async (nameInput) => {
const outputObj = {
byYear: {}
};

// build an array of empty years
for (let x = startYear; x <= endYear; x++) {
outputObj.byYear[x] = {
usa: {
m: emptyYearObj,
f: emptyYearObj
},
};
};

const stateNameObjs = await getStateNameData(nameInput);
const nationalNameObjs = await get({ name: nameInput});
outputObj.detailM = await getDetailed(nameInput, 'm');
outputObj.detailF = await getDetailed(nameInput, 'f');

if (outputObj.detailM?.id) {
delete outputObj.detailM.id;
try {
const decodedNameData = JSON.parse(outputObj.detailM.nameData);
outputObj.detailM.nameData = decodedNameData;
} catch (e) {
const decodedNameData = {};
}
}

if (outputObj.detailF?.id) {
delete outputObj.detailF.id;
try {
const decodedNameData = JSON.parse(outputObj.detailF.nameData);
outputObj.detailF.nameData = decodedNameData;
} catch (e) {
const decodedNameData = {};
}
}

for (let index in nationalNameObjs) {
const nameObj = nationalNameObjs[index];
outputObj.byYear[nameObj.year]['usa'][nameObj.sex] = {
births: nameObj.births,
rank: nameObj.rank
};
}

for (let index in stateNameObjs) {
const nameObj = stateNameObjs[index];
outputObj.byYear[nameObj.year][nameObj.state] = {
m: emptyYearObj,
f: emptyYearObj
};

outputObj.byYear[nameObj.year][nameObj.state][nameObj.sex] = {
births: nameObj.births,
rank: nameObj.rank
};
}
return outputObj;
};



const getStateNameData = async (nameInput, sexInput = null) => {
return new Promise((fulfill, reject) => {
const outputArray = [];
const queryParams = [nameInput];

stateNameDb.serialize(() => {
// the basic sql query
let sql = 'SELECT * FROM stateNameData WHERE name = (?) ';
if (sexInput) {
sql += ' and sex = (?)';
queryParams.push(sexInput);
}
stateNameDb.all(sql, queryParams, (err, rows) => {
if (err) {
console.log('err', err);
reject (err);
}

fulfill(rows);
});
});
});
};


const getDetailed = async (nameInput, sexInput) => {
Expand Down Expand Up @@ -61,53 +158,53 @@ const get = async (params) => {
nameDataDb.serialize(() => {
// the basic sql query
let sql = '';
if (params.unique) {
if (params?.unique) {
sql = 'SELECT DISTINCT name FROM usNameData WHERE "b" = "b" ';
} else {
sql = 'SELECT * FROM usNameData WHERE "b" = "b" ';
}

// build the filters
if (params.id) {
if (params?.id) {
sql += ' AND id = (?) ';
queryParams.push(params.id );
}
if (params.name) {
if (params?.name) {
sql += ' AND name = (?) ';
queryParams.push(params.name.toLowerCase() );
}
if (params.year) {
if (params?.year) {
sql += ' AND year = (?) ';
queryParams.push(params.year );
}
if (params.yearRange) {
if (params?.yearRange) {
sql += ' AND year >= (?) AND year <= (?) ';
queryParams.push(params.yearRange.start );
queryParams.push(params.yearRange.end );
}
if (params.sex) {
if (params?.sex) {
sql += ' AND sex = (?) ';
queryParams.push(params.sex.toUpperCase() );
queryParams.push(params.sex.toLowerCase() );
}
if (params.rank) {
if (params?.rank) {
sql += ' AND rank = (?) ';
queryParams.push(params.rank );
}
if (params.rankRange) {
if (params?.rankRange) {
sql += ' AND rank >= (?) AND rank <= (?) ';
queryParams.push(params.rankRange.start );
queryParams.push(params.rankRange.end );
}
if (params.births) {
if (params?.births) {
sql += ' AND births = (?) ';
queryParams.push(params.births );
}
if (params.birthsRange) {
if (params?.birthsRange) {
sql += ' AND births >= (?) AND births <= (?) ';
queryParams.push(params.birthsRange.start );
queryParams.push(params.birthsRange.end );
}
if (params.unique) {
if (params?.unique) {
sql += ' ORDER BY name';
}

Expand All @@ -124,11 +221,6 @@ const get = async (params) => {
});
};

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


const getNameRankAndBirthsByYear = async (name, params) => {
return new Promise((fulfill, reject) => {
Expand All @@ -138,12 +230,12 @@ const getNameRankAndBirthsByYear = async (name, params) => {
nameDataDb.serialize(() => {
// the basic sql query

let sql = 'SELECT year, births, rank FROM usNameData WHERE name = (?)';
let sql = 'SELECT year, births, rank, sex FROM usNameData WHERE name = (?)';
queryParams.push(name.toLowerCase() );

if (params.sex) {
if (params?.sex) {
sql += ' AND sex = (?) ';
queryParams.push(params.sex.toUpperCase() );
queryParams.push(params.sex.toLowerCase() );
}

// run the query, return the results...
Expand All @@ -152,7 +244,7 @@ const getNameRankAndBirthsByYear = async (name, params) => {
reject(err);
}

if (params.getEmptyYears) {
if (params?.getEmptyYears) {
for (let x = startYear; x <= endYear; x++) {
outputObj[x] = emptyYearObj;
}
Expand All @@ -163,7 +255,8 @@ const getNameRankAndBirthsByYear = async (name, params) => {
const nameObj = rows[index];
outputObj[nameObj.year] = {
births: nameObj.births,
rank: nameObj.rank
rank: nameObj.rank,
sex: nameObj.sex
};
}
fulfill(outputObj);
Expand All @@ -180,6 +273,9 @@ export {
get,
getNameRankAndBirthsByYear,
getDetailed,
getStateNameData,
getAllByName,
nameDataDb,
nameDetailsDb
};
nameDetailsDb,
stateNameDb
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "usbabynames",
"version": "2.0.5",
"version": "2.1.0",
"description": "Returns data about baby names used in the United States 1880-2021",
"main": "lib/usBabyNames.mjs",
"scripts": {
Expand Down

0 comments on commit 66b0962

Please sign in to comment.