Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
russellsteadman committed May 7, 2017
0 parents commit 64046e7
Show file tree
Hide file tree
Showing 23 changed files with 988,503 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.history
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.history
converter
.gitignore
db
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Food Web

Food Web is an open source project that makes it simple to access the USDA's [Nutrient Standard Reference Database](https://www.ars.usda.gov/northeast-area/beltsville-md/beltsville-human-nutrition-research-center/nutrient-data-laboratory/docs/sr28-download-files/).

## Install

```shell
$ npm i -S foodweb
```

## Methods

TBA
21 changes: 21 additions & 0 deletions converter/component/ascii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var asciiHelper = function () {

this.convert = function (raw,allowBoolean) {
var newline = (/\r/g.test(raw)?"\r":"")+(/\n/g.test(raw)?"\n":"");
raw = raw.split(newline);
var addition = {};
for (var i in raw) {
raw[i] = raw[i].split('^');
for (var o in raw[i]) {
if (/^(-?[1-9]+\d*([.]\d+)?)$|^(-?0[.]\d*[1-9]+)$|^0$|^0.0$/.test(raw[i][o])) raw[i][o] = Number(raw[i][o]);
if (typeof raw[i][o] === 'string') raw[i][o] = raw[i][o].replace(/^~(.*?)~$/,'$1');
if (raw[i][o] === 'Y'&&allowBoolean) raw[i][o] = true;
if (raw[i][o] === 'N'&&allowBoolean) raw[i][o] = false;
}
}
return raw;
};

};

module.exports = new asciiHelper();
51 changes: 51 additions & 0 deletions converter/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var fs = require('fs');
var ascii = require('./component/ascii');

var foodDes = ascii.convert(fs.readFileSync('./../db/FOOD_DES.txt','ascii'), true);

var searchDb = [];

for (var i in foodDes) {
var a = foodDes[i];
foodDes[i] = {};
foodDes[i].id = a[0]; //NDB_NO
foodDes[i].group = a[1]; //FOOD GROUP
if (a[5]) foodDes[i].maker = a[5];
if (a[6]) foodDes[i].hasExtra = a[6];
if (a[8]) foodDes[i].refuse = a[8];
foodDes[i].about = {};
foodDes[i].about.long = a[2];
foodDes[i].about.short = a[3];
if (a[2]) foodDes[i].keywords = a[2].replace(/[^ \w]+/g,'').replace(/[\s]+/g,' ').toLowerCase().split(' ');
if (a[7]) foodDes[i].about.refuse = a[7];
foodDes[i].name = {};
if (a[9]) foodDes[i].name.sci = a[9];
if (a[4]) foodDes[i].name.alias = a[4];
foodDes[i].factor = {};
if (a[10]) foodDes[i].factor.n = a[10];
if (a[11]) foodDes[i].factor.pro = a[11];
if (a[12]) foodDes[i].factor.fat = a[12];
if (a[13]) foodDes[i].factor.cho = a[13];
searchDb.push({id: a[0], key: foodDes[i].keywords});
}

fs.writeFileSync('./../json/src/searchDb.json', JSON.stringify(searchDb));

var footNote = ascii.convert(fs.readFileSync('./../db/FOOTNOTE.txt','ascii'));

for (var i in foodDes) {
for (var o in footNote) {
foodDes[i].footnote = {};
if (foodDes[i].id === footNote[o][0]) {
if (footNote[o][1]) foodDes[i].footnote.number = footNote[o][1];
if (footNote[o][2]) foodDes[i].footnote.type = footNote[o][2];
if (footNote[o][3]) foodDes[i].footnote.nutrient = footNote[o][3];
if (footNote[o][4]) foodDes[i].footnote.text = footNote[o][4];
}
}
}

var foodGroup = ascii.convert(fs.readFileSync('./../db/FD_GROUP.txt','ascii'));
fs.writeFileSync('./../json/src/foodGroup.json', JSON.stringify(foodGroup));

fs.writeFileSync('./../json/src/foodDb.json', JSON.stringify(foodDes));
682 changes: 682 additions & 0 deletions db/DATA_SRC.txt

Large diffs are not rendered by default.

Loading

0 comments on commit 64046e7

Please sign in to comment.