student student - 3 months ago 10
Node.js Question

How is it possible for a string to be typed as Number in npm package commandLineArgs

In the below code from MongoDB's course Week 3's Query Operators in the Node.js Driver chapter :



var MongoClient = require('mongodb').MongoClient,
commandLineArgs = require('command-line-args'),
assert = require('assert');


var options = commandLineOptions();

MongoClient.connect('mongodb://localhost:27017/crunchbase', function(err, db) {

assert.equal(err, null);
console.log("Successfully connected to MongoDB.");

var query = queryDocument(options);
var projection = {
"_id": 1,
"name": 1,
"founded_year": 1,
"number_of_employees": 1,
"crunchbase_url": 1
};

var cursor = db.collection('companies').find(query, projection);
var numMatches = 0;

cursor.forEach(
function(doc) {
numMatches = numMatches + 1;
console.log(doc);
},
function(err) {
assert.equal(err, null);
console.log("Our query was:" + JSON.stringify(query));
console.log("Matching documents: " + numMatches);
return db.close();
}
);

});

function queryDocument(options) {

console.log(options);

var query = {
"founded_year": {
"$gte": options.firstYear,
"$lte": options.lastYear
}
};

if ("employees" in options) {
query.number_of_employees = {
"$gte": options.employees
};
}

return query;

}

function commandLineOptions() {

var cli = commandLineArgs([{
name: "firstYear",
alias: "f",
type: Number
}, {
name: "lastYear",
alias: "l",
type: Number
}, {
name: "employees",
alias: "e",
type: Number
}]);

var options = cli.parse()
if (!(("firstYear" in options) && ("lastYear" in options))) {
console.log(cli.getUsage({
title: "Usage",
description: "The first two options below are required. The rest are optional."
}));
process.exit();
}

return options;

}





I'm requiring
command-line-args
package, which has a method
commandLineArgs
. All good and fine...

Now, I see that the
type
of the objects passed to this method is set to
Number
. We can clearly see that they're
String
s.

How is it possible?

Answer

From the command-line-args GitHub page:

The type value is a setter function (you receive the output from this), enabling you to be specific about the type and value received.

In other words, passing Number as type allows you to parse the arguments as numbers.