Learning Node.js Development
上QQ阅读APP看书,第一时间看更新

Using the _.isString utility

To use the _.isString utility, we'll add console.log in app.js to show the result to the screen and we'll use _.isString, passing in a couple of values. Let's pass in true first, then we can duplicate this line and we'll pass in a string such as Gary, as shown here:

console.log('Starting app.js');

const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');

console.log(_.isString(true));
console.log(_.isString('Gary'));

// console.log('Result:', notes.add(9, -2));

// var user = os.userInfo();
//
// fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);

We can run our project over in the Terminal using the same command we've used previously, node app.js, to run our file:

When we run the file, we get our two prompts that we've started both files, and we get false and then true. false comes because the Boolean is not a string, and true comes up because Gary is indeed a string, so it passes the test of _.isString. This is one of the many utility functions that comes bundled with lodash.

Now, lodash can do a lot more than simple type checking. It comes with a bunch of other utility methods we can take advantage of. Let's explore one more utility.