├── .gitignore ├── examples ├── basic.js └── no-module.js ├── package.json ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /examples/basic.js: -------------------------------------------------------------------------------- 1 | var qcew = require('../index.js'); // Note: Change this to `require('qcew')` when using the module in your own script. 2 | 3 | // Data output format can be `json`, `csv` or `rows`. 4 | // If no value is passed as the last argument, `json` will be returned. 5 | // `rows` will return an array of arrays representing the desired table. 6 | 7 | qcew.getAreaData('2013','1','26000', function(err, areaData){ 8 | if (!err){ 9 | console.log(areaData); 10 | } else { 11 | console.log(err); 12 | } 13 | }, 'json'); 14 | 15 | qcew.getIndustryData('2013','1','3361', function(err, autoManufacturing){ 16 | if (!err){ 17 | console.log(autoManufacturing); 18 | } else { 19 | console.log(err); 20 | } 21 | }, 'csv'); 22 | 23 | qcew.getSizeData('2013','6', function(err, sizeData){ 24 | if (!err){ 25 | console.log(sizeData); 26 | } else { 27 | console.log(err); 28 | } 29 | }, 'rows'); 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "qcew", 3 | "version": "1.0.3", 4 | "description": "Interface for querying Bureau of Labor Statistics Quarterly Census of Employment and Wages", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/mhkeller/qcew.git" 12 | }, 13 | "keywords": [ 14 | "BLS", 15 | "bureau of labor statistics", 16 | "labor", 17 | "wages", 18 | "wage data", 19 | "employment", 20 | "employment data", 21 | "qcew", 22 | "economics" 23 | ], 24 | "author": "Michael Keller", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/mhkeller/qcew/issues" 28 | }, 29 | "homepage": "https://github.com/mhkeller/qcew", 30 | "dependencies": { 31 | "d3-dsv": "^1.0.7", 32 | "request": "^2.55.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | QCEW 2 | === 3 | 4 | An NodeJs interface for [Bureau of Labor Statistics Quarterly Census of Employment and Wages](http://www.bls.gov/cew/home.htm) data. For other languages, see the [QCEW Open Data Access](http://www.bls.gov/cew/doc/access/data_access_examples.htm) page. This setup borrows from the Python and PHP/JavaScript examples for code samples and documentation. 5 | 6 | *Note: I am not affiliated in any way with BLS.* 7 | 8 | ## Installation 9 | 10 | ```` 11 | npm install qcew 12 | ```` 13 | 14 | ## Usage 15 | 16 | The module has three functions, which you would use as below. For documentation on what the data means, please see the [BLS QCEW website](http://www.bls.gov/cew/home.htm) and [overview](http://www.bls.gov/cew/cewover.htm), which will have the most up-to-date and in-depth information. 17 | 18 | All functions can take an optional `format` parameter as the last argument to determine the data format you'll receive. If nothing is set, the data will come back as Json. The options are: 19 | 20 | * `'json'` — The default and needs not be specified. Returns an array of objects. 21 | * `'csv'` — Returns the data as a csv string — how the API sends it. Useful for writing data to file. 22 | * `'rows'` — Returns an array of arrays representing each row in the data. The first array contains the header values. 23 | 24 | #### *getAreaData(year, quarter, area, callback[, format])* 25 | 26 | This function takes a year, quarter, and area argument and returns data containing the associated area data. Use 'a' for annual averages. 27 | 28 | For all area codes and titles see: 29 | 30 | #### *getIndustryData(year, quarter, industry_code, callback[, format])* 31 | 32 | This function takes a year, quarter, and industry code and returns data containing the associated industry data. Use 'a' for annual averages. Some industry codes contain hyphens. The CSV files use underscores instead of hyphens. So 31-33 becomes 31_33. 33 | 34 | For all industry codes and titles see: 35 | 36 | #### *getSizeData(year, establishment_size_class_code, callback[, format])* 37 | 38 | This function takes a year and establishment size class code and returns data containing the associated size data. Size data is only available for the first quarter of each year. 39 | 40 | For all establishment size classes and titles see: 41 | 42 | ## Example 43 | 44 | ````js 45 | var qcew = require('qcew'); 46 | 47 | // Data output format can be `json`, `csv` or `rows`. 48 | // If no value is passed as the last argument, `json` will be returned. 49 | // `rows` will return an array of arrays representing the desired table. 50 | 51 | qcew.getAreaData('2013','1','26000', function(err, areaData){ 52 | if (!err){ 53 | console.log(areaData); 54 | } else { 55 | console.log(err); 56 | } 57 | }); 58 | 59 | qcew.getIndustryData('2013','1','3361', function(err, autoManufacturing){ 60 | if (!err){ 61 | console.log(autoManufacturing); 62 | } else { 63 | console.log(err); 64 | } 65 | }, 'csv'); 66 | 67 | qcew.getSizeData('2013','6', function(err, sizeData){ 68 | if (!err){ 69 | console.log(sizeData); 70 | } else { 71 | console.log(err); 72 | } 73 | }, 'rows'); 74 | ```` 75 | 76 | ## License 77 | 78 | MIT -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var request = require('request') 2 | var dsv = require('d3-dsv') 3 | 4 | /* ******************************************************************************* 5 | qcewRequestToData : This function takes a url, a callback and an optional format argument 6 | and returns data. By default, it will return json if `format` is not set. 7 | If `'csv'` is passed as the format, it will return the data as it comes from the API, 8 | which is a csv. 9 | If `rows` is passed it will return an array of arrays, the first one being the header row. 10 | */ 11 | function qcewRequestToData(urlPath, callback, format){ 12 | var formatters = { 13 | json: function(body){ 14 | return dsv.csvParse(body); 15 | }, 16 | csv: function(body){ 17 | return body; 18 | }, 19 | rows: function(body){ 20 | var csvLines = body.split('\r\n'); 21 | var dataTable = []; 22 | for (var i = 0; i < csvLines.length; i += 1) { 23 | dataTable.push(csvLines[i].split(',')); 24 | } 25 | return dataTable; 26 | } 27 | }; 28 | format = format || 'json'; 29 | request(urlPath, function(err, response, body){ 30 | var data; 31 | if (!err && response.statusCode == 200){ 32 | data = formatters[format](body); 33 | callback(null, data); 34 | } else { 35 | callback(err); 36 | } 37 | }); 38 | } 39 | 40 | /* ******************************************************************************* 41 | qcewGetAreaData : This function takes a year, quarter, and area argument and 42 | returns data containing the associated area data. Use 'a' for annual 43 | averages. 44 | For all area codes and titles see: 45 | http://www.bls.gov/cew/doc/titles/area/area_titles.htm 46 | */ 47 | function qcewGetAreaData(year, qtr, area, callback, format){ 48 | var urlPath = 'http://www.bls.gov/cew/data/api/'+year+'/'+qtr+'/area/'+area+'.csv'; 49 | qcewRequestToData(urlPath, callback, format); 50 | } 51 | 52 | /* ******************************************************************************* 53 | qcewGetIndustryData : This function takes a year, quarter, and industry code 54 | and returns data containing the associated industry data. Use 'a' for 55 | annual averages. Some industry codes contain hyphens. The CSV files use 56 | underscores instead of hyphens. So 31-33 becomes 31_33. 57 | For all industry codes and titles see: 58 | http://www.bls.gov/cew/doc/titles/industry/industry_titles.htm 59 | */ 60 | function qcewGetIndustryData(year, qtr, industry, callback, format){ 61 | var urlPath = 'http://www.bls.gov/cew/data/api/'+year+'/'+qtr+'/industry/'+industry+'.csv'; 62 | qcewRequestToData(urlPath, callback, format); 63 | } 64 | 65 | /* ******************************************************************************* 66 | qcewGetSizeData : This function takes a year and establishment size class code 67 | and returns data containing the associated size data. Size data 68 | is only available for the first quarter of each year. 69 | For all establishment size classes and titles see: 70 | http://www.bls.gov/cew/doc/titles/size/size_titles.htm 71 | */ 72 | function qcewGetSizeData(year, size, callback, format){ 73 | var urlPath = 'http://www.bls.gov/cew/data/api/'+year+'/1/size/'+size+'.csv'; 74 | qcewRequestToData(urlPath, callback, format); 75 | } 76 | 77 | module.exports = { 78 | getAreaData: qcewGetAreaData, 79 | getIndustryData: qcewGetIndustryData, 80 | getSizeData: qcewGetSizeData 81 | }; 82 | -------------------------------------------------------------------------------- /examples/no-module.js: -------------------------------------------------------------------------------- 1 | // The other examples on the BLS website aren't set up with the library as a module 2 | // To keep things consistent, here's an example that shows how to use these functions as standalones. 3 | // Note: There is no real reason why you would ever want to use this set up over what is in `examples/basic.js`. 4 | // It is provided here as a purely demonstrational example if you're new to NodeJs. 5 | 6 | var request = require('request'); 7 | var dsv = require('dsv'); 8 | 9 | /* ******************************************************************************* 10 | qcewRequestToData : This function takes a url, a callback and an optional format argument 11 | and returns data. By default, it will return json if `format` is not set. 12 | If `'csv'` is passed as the format, it will return the data as it comes from the API, 13 | which is a csv. 14 | If `rows` is passed it will return an array of arrays, the first one being the header row. 15 | */ 16 | function qcewRequestToData(urlPath, cb, format){ 17 | var formatters = { 18 | json: function(body){ 19 | return dsv.csv.parse(body); 20 | }, 21 | csv: function(body){ 22 | return body; 23 | }, 24 | rows: function(body){ 25 | var csvLines = body.split('\r\n'); 26 | var dataTable = []; 27 | for (var i = 0; i < csvLines.length; i += 1) { 28 | dataTable.push(csvLines[i].split(',')); 29 | } 30 | return dataTable; 31 | } 32 | }; 33 | format = format || 'json'; 34 | request(urlPath, function(err, response, body){ 35 | var data; 36 | if (!err && response.statusCode == 200){ 37 | data = formatters[format](body); 38 | cb(null, data); 39 | } else { 40 | cb(err); 41 | } 42 | }); 43 | } 44 | 45 | /* ******************************************************************************* 46 | qcewGetAreaData : This function takes a year, quarter, and area argument and 47 | returns an array containing the associated area data. Use 'a' for annual 48 | averages. 49 | For all area codes and titles see: 50 | http://www.bls.gov/cew/doc/titles/area/area_titles.htm 51 | */ 52 | function qcewGetAreaData(year, qtr, area, cb, format){ 53 | var urlPath = 'http://www.bls.gov/cew/data/api/'+year+'/'+qtr+'/area/'+area+'.csv'; 54 | qcewRequestToData(urlPath, cb, format); 55 | } 56 | 57 | /* ******************************************************************************* 58 | qcewGetIndustryData : This function takes a year, quarter, and industry code 59 | and returns an array containing the associated industry data. Use 'a' for 60 | annual averages. Some industry codes contain hyphens. The CSV files use 61 | underscores instead of hyphens. So 31-33 becomes 31_33. 62 | For all industry codes and titles see: 63 | http://www.bls.gov/cew/doc/titles/industry/industry_titles.htm 64 | */ 65 | function qcewGetIndustryData(year, qtr, industry, cb, format){ 66 | var urlPath = 'http://www.bls.gov/cew/data/api/'+year+'/'+qtr+'/industry/'+industry+'.csv'; 67 | qcewRequestToData(urlPath, cb, format); 68 | } 69 | 70 | /* ******************************************************************************* 71 | qcewGetSizeData : This function takes a year and establishment size class code 72 | and returns an array containing the associated size data. Size data 73 | is only available for the first quarter of each year. 74 | For all establishment size classes and titles see: 75 | http://www.bls.gov/cew/doc/titles/size/size_titles.htm 76 | */ 77 | function qcewGetSizeData(year, size, cb, format){ 78 | var urlPath = 'http://www.bls.gov/cew/data/api/'+year+'/1/size/'+size+'.csv'; 79 | qcewRequestToData(urlPath, cb, format); 80 | } 81 | 82 | qcewGetAreaData('2013','1','26000', function(err, areaData){ 83 | if (!err){ 84 | console.log(areaData); 85 | } else { 86 | console.log(err); 87 | } 88 | }, 'json'); 89 | 90 | qcewGetIndustryData('2013','1','3361', function(err, autoManufacturing){ 91 | if (!err){ 92 | console.log(autoManufacturing); 93 | } else { 94 | console.log(err); 95 | } 96 | }, 'csv'); 97 | 98 | qcewGetSizeData('2013','6', function(err, sizeData){ 99 | if (!err){ 100 | console.log(sizeData); 101 | } else { 102 | console.log(err); 103 | } 104 | }, 'rows'); 105 | --------------------------------------------------------------------------------