Normalization
Architecture
How raw NSF API records become normalized NSFAward objects, and how the server shapes them into summaries, full records and printFields projections, normalizes dates, and converts outcome report HTML to text.
The NSF Award API sends every value as a string: amounts like "1498027", flags like "true" and "1", and "" for many absent values. It also misspells piMiddeInitial and packs each PI and Co-PI into one "Name email" string.1 The server never exposes these raw records. The NSF API client passes every record it receives through normalizeAward() in src/utils/normalize.ts before anything else sees it.23
The tools then shape normalized records for output: compact summaries by default, full records on request, or a printFields projection. This page also covers the two other conversions on the data path: date normalization for search inputs, and HTML-to-text conversion for Project Outcomes Reports. The fields themselves are listed in Award record.
normalizeAward
normalizeAward(raw: RawNSFAward): NSFAward converts one record. RawNSFAward is the record exactly as the live API sends it; NSFAward is the normalized shape.34
| Raw field | Raw value | Normalized |
|---|---|---|
id |
string | String(raw.id) |
estimatedTotalAmt, fundsObligatedAmt |
"1498027" |
Number; $, commas and whitespace are removed first. Blank or unparseable values are dropped |
activeAwd, histAwd |
"true" / "false" |
Boolean |
publicAccessMandate |
"1" / "0" |
Boolean |
primaryProgram |
string or array | Array of strings |
pi, coPDPI |
array of "Name email" strings |
Array of { name, email?, former } objects; blank entries are dropped |
piMiddeInitial (sic) |
string | Renamed to piMiddleInitial; dropped when empty |
| (none) | hasOutcomesReport: true when projectOutComesReport is a non-blank string |
Booleans accept true, 1, false and 0 (trimmed, case-insensitive) or a real boolean; any other value leaves the field out. Numbers accept a finite number or a numeric string.4
After these conversions, every field whose value is "" is deleted, because the API sends "" for absent values such as parentUeiNumber and poEmail. All other fields, including fields the API adds later, pass through unchanged.43
People
parsePerson(entry) parses one pi or coPDPI string of the form "First M Last (Former) email":43
- The entry is trimmed and split on whitespace.
- If there is more than one word and the last word contains
@, it becomesemail. - If the remaining name contains
(Former)(any case),formeristrueand the marker is removed with the extra spaces it leaves. - The result is
{ name, email, former }, or{ name, former }when there is no email.
{ "name": "Jane Q Doe", "email": "jane@example.edu", "former": true }
search_by_pi with includeCoPIs relies on these parsed names to check that each merged award really names the person (see search_by_pi).
Record shapes
shapeAward(award, detail, printFields?) in src/tools/shared.ts chooses the shape of each record in a search result:5
| Condition | Shape |
|---|---|
printFields is a non-empty string |
pickFields(award, parseFieldList(printFields)) |
otherwise, detail is full |
A shallow copy of the whole normalized record |
otherwise (detail defaults to summary) |
toSummary(award) |
printFields takes precedence over detail.5 The search tools’ detail and printFields inputs are described in Shared search parameters.
Summaries
toSummary(award) copies the SUMMARY_FIELDS that are present, always in this order, so summaries read the same for every award:4
id, title, pdPIName, coPDPI, awardeeName, awardeeCity, awardeeStateCode,
startDate, expDate, estimatedTotalAmt, fundsObligatedAmt, fundProgramName,
dirAbbr, divAbbr, transType, activeAwd, hasOutcomesReport
In a summary, coPDPI is a list of names only; a former Co-PI is written as "<name> (Former)". Emails stay in the full record.4 This is why the output schema allows each coPDPI entry to be either a string or a person object.6
printFields projections
parseFieldList(fields) splits the comma-separated string, trims each name and drops blanks. pickFields(record, fields) keeps the record’s own entries whose key is in that list. Unknown names are ignored, and the fields come out in the record’s order, not the requested order.4 A projection is taken from the full normalized record, so it can include any field (including jrnl and projectOutComesReport, which detail: "full" leaves out), and coPDPI and pi keep their person objects. The NSF API itself ignores its printFields parameter, so the projection is done here, after the full record arrives.1
A printFields value with no names in it after splitting, such as ",", still takes precedence over detail and yields empty records.54
Full records and lookups
detail: "full" returns every field of the normalized record, including abstractText, except projectOutComesReport and the PUBLICATION_FIELDS (jrnl, publicationResearch, publicationConference). shapeAward copies the record and deletes them, because with them one record can reach about 75 KB; without them a record is about 3-5 KB. printFields, get_award_details (with includePublications) and get_project_outcomes still reach those fields.56
The single-award paths trim records differently:
get_award_detailsalways removesprojectOutComesReport. It removesabstractTextwhenincludeAbstractisfalse(defaulttrue), and removes thePUBLICATION_FIELDS(jrnl,publicationResearch,publicationConference) unlessincludePublicationsistrue(defaultfalse).76- The
nsf://award/{id}resource always removesprojectOutComesReportand thePUBLICATION_FIELDS, and keeps the abstract.8
See get_award_details and nsf://award/{id}.
Page summary
buildSearchOutput() computes pageSummary from the normalized awards of the current page, before they are shaped: totalEstimatedAmt sums estimatedTotalAmt (missing counts as 0), byInstitution counts awards per awardeeName (falling back to awardee, then Unknown), and byProgram counts awards per fundProgramName (or Unknown). It covers this page only, not the whole result set.56 The rest of the envelope is described in Search results.
Dates
The API takes and returns dates as mm/dd/yyyy.1 Record dates (startDate, expDate, date and others) pass through normalization unchanged. Search inputs go through normalizeToNSFDate() in src/utils/date-utils.ts twice: the tool input schemas reject a date for which it returns undefined, and the client converts the accepted value before sending it.962
normalizeToNSFDate(input) accepts:9
| Input | Result |
|---|---|
mm/dd/yyyy or m/d/yyyy |
Zero-padded mm/dd/yyyy |
yyyy-mm-dd |
The same calendar day as mm/dd/yyyy |
yyyy-mm-ddThh:mm[:ss[.fff]] with no zone (T, t or a space before the time) |
The calendar day as written |
yyyy-mm-ddThh:mm... (or a space before the time) with Z or an offset such as -07:00 |
The UTC calendar day of that instant |
| A number (a timestamp) | Its UTC calendar day |
A Date object |
Its local calendar day |
Anything else, or an impossible date such as 2020-02-30 or 13/01/2020 |
undefined (never a rolled-over date) |
Calendar strings are formatted as written, so the result does not depend on the server’s time zone. A date-time with a zone gives the UTC day whether a T or a space separates the date and time, so 2024-01-15 23:00:00-07:00 and 2024-01-15T23:00:00-07:00 both give 01/16/2024.9
The other helpers in the file:96
| Function | Use |
|---|---|
isValidNSFDateFormat(s) |
true for a real calendar date in strict mm/dd/yyyy |
toNSFDateFormat(date) |
Formats a Date, timestamp or string; throws Invalid date: <value> when it cannot parse it |
parseNSFDate(s) |
Parses strict mm/dd/yyyy into a local Date; throws Invalid NSF date format: <s>. Expected mm/dd/yyyy |
validateDateRange(from, to) |
true when from <= to; used by the schemas’ checkRanges to reject reversed ranges |
getCurrentNSFDate(), getNSFDateDaysAgo(n), getNSFDateDaysAhead(n) |
Today, n days ago and n days ahead as mm/dd/yyyy (not used in src/ outside this file) |
HTML to text
Project Outcomes Reports arrive as HTML in the projectOutComesReport field. getProjectOutcomes converts them with htmlToText() from src/utils/html-utils.ts, and keeps the original HTML as outcomesReportHtml.2 The conversion is deliberately small; entity decoding uses decodeHTML from the entities package, a runtime dependency.10 In order, it:11
- Removes
<script>and<style>elements with their content. - Turns
<br>into a line break, and the closing tags</p>,</div>,</li>,</h1>to</h6>and</tr>into line breaks. - Turns each opening
<li>into-. - Strips every remaining tag, then drops a dangling tag fragment at the end (some reports are cut off mid-tag).
- Decodes every HTML5 named entity (such as
’,—,­and the case-sensitiveΔ/δ) and every numeric entity, decimal or hex. Each entity is decoded once, so&rsquo;becomes the text’. A numeric entity outside the Unicode range (above0x10FFFF) becomes U+FFFD, the replacement character, as in browsers. Unknown names such as&bogus;stay as written. Entities are decoded after the tags are stripped, so an encoded<b>survives as text.1112 - Turns non-breaking spaces (U+00A0, from
or literal) into plain spaces, and removes soft hyphens (U+00AD, from­or literal), which only break text matching.1112 - Removes carriage returns, spaces and tabs at line ends, and indentation at line starts.
- Empties lines that hold only an empty bullet (
-). The line break stays, so each such line becomes a blank line. - Collapses three or more line breaks into two, and trims the result.
The plain text is what get_project_outcomes and the nsf://award/{id}/outcomes resource return; see get_project_outcomes and nsf://award/{id}/outcomes.8
Testing normalization
Unit tests build their data from real, trimmed API responses in tests/fixtures/ run through normalizeAward. Do not hand-write numeric amounts: the live API sends strings.1 The output schemas expect numbers, so a record that skipped normalization fails output validation (see Request flow). See Testing.