Source: site.view [edit]
Function name: lookupBabyNames
Arguments: name,year,gender
Description:
Page type: webl
Render function:  
Module: global

Page source:

var db = Wub_GetDB("namesDB");
var coll = Wub_GetCollection(db, "names");

var query = [. .];
if name != "" then
   query.name := name
end;
if year != "" then
   query.year := ToInt(year)
elsif name == "" then
   query.year := ToInt(2018)
end;
if gender != "" then
   query.gender := Str_ToLowerCase(gender)
end;
var dbobj = Wub_NewDBObject(query);

var res = Wub_QueryDB(coll, dbobj, 100); // 100 = max results
var res2 = [];

every elt in res do
   elt.name := Str_ToUpperCase(Select(elt.name, 0, 1)) + Select(elt.name, 1, Size(elt.name));
   DeleteField(elt, "_id");
   res2 = res2 + [ elt ]
end;
res2;




/*   CODE TO CREATE DB
var addToDB = fun(coll, year)

   var P = GetURL("http://adam.cheyer.com/data/yob" + ToString(year) + ".txt");

   var i = 1;
   var rows = [];
   var total = 0;

   every line in Str_Split(Text(P), "\n") do
      var cols = Str_Split(Str_Trim(line), ",");
      if Size(cols) == 3 then
         rows = rows + [ [. rank=i, name = Str_ToLowerCase(cols[0]), sex = cols[1], num = ToInt(cols[2]) .] ];
         total = total + ToInt(cols[2]);
         i = i + 1;
      end
   end;

   every row in rows do
  
      var dbrow = row;
      dbrow.percent := (row.num * 10000) div total;
      var dbobj = Wub_NewDBObject(dbrow);
      Wub_InsertDB(coll, dbobj);
   end;
end;
  
  
// Wub_DropDB("namesDB");
var db = Wub_GetDB("namesDB");
var coll = Wub_GetCollection(db, "names");

var year = 2000;
while year < 2019 do
  addToDB(coll, year);
  year = year + 1
end;  

*/