How to use the isString function from underscore
Find comprehensive JavaScript underscore.isString code examples handpicked from public code repositorys.
The underscore.isString function is used to check whether a given value is a string or not.
2798 2799 2800 2801 2802 2803 2804 2805 2806
// Is a given array, string, or object empty? // An "empty" object has no enumerable own-properties. _.isEmpty = function(obj) { if (obj == null) return true; if (_.isArray(obj) || _.isString(obj)) return obj.length === 0; for (var key in obj) if (_.has(obj, key)) return false; return true; };
70 71 72 73 74 75 76 77 78 79
} // extract slots _(request.intent.slots) .each(function(slot) { intent.variables[slot.name] = slot.value; intent.slotConfirmationStatus[slot.name] = _.isString(slot.confirmationStatus) ? slot.confirmationStatus.toLowerCase() : 'none'; }); return intent;
How does underscore.isString work?
underscore.isString is a function provided by the Underscore.js library that determines whether a given value is a string or not. When called with an argument, it checks whether the argument's [[Class]] internal property is 'String', and returns a boolean value indicating the result of the check. If the argument is not a string, the function returns false.
59 60 61 62 63 64 65 66 67 68
}; break; case 'arrayOfString': validator = function(value) { return _.isArray(value) && _(value).all(function(obj) { return _.isString(obj); }); }; break; case 'arrayOfObject':
+ 5 other calls in file
30 31 32 33 34 35 36 37 38 39
}; // set current message in specials if (msg.payload != null && _.isString(msg.payload.content)) { specials.message = msg.payload.content; } else if (msg.previous != null && _.isString(msg.previous.content)) { specials.message = msg.previous.content; } const payload = _.isObject(msg.payload) ? msg.payload : {}; const options = Object.assign({
Ai Example
1 2 3 4 5 6
const _ = require("underscore"); console.log(_.isString("Hello, World!")); // true console.log(_.isString(123)); // false console.log(_.isString(false)); // false console.log(_.isString(null)); // false
In this example, we first import Underscore with require(). Then, we use _.isString() to check if each of the four values passed as arguments is a string or not. The method returns a boolean true if the value is a string and false otherwise.
106 107 108 109 110 111 112 113 114
function validateArgs(symbols) { if (!_.isArray(symbols)) { throw new InvalidSchemaError('Enum must have array of symbols, got ' + JSON.stringify(symbols)); } if (!_.all(symbols, function(symbol) { return _.isString(symbol); })) { throw new InvalidSchemaError('Enum symbols must be strings, got ' + JSON.stringify(symbols)); } }
216 217 218 219 220 221 222 223 224 225
// count valid accounts in browser storage _.each(this.get(), function(account, accountIndex){ if(_.isUndefined(account) || !_.isObject(account) || _.isString(account)) return; if(!_.has(account, 'encrypted') || !_.has(account, 'private'))
+ 3 other calls in file
GitHub: cloakscn/yapi-docker
7 8 9 10 11 12 13 14 15 16
return {}; } }; // 处理字符串换行 const handleWrap = str => { return _.isString(str) ? str.replace(/\n/gi, '<br/>') : str; }; const messageMap = { desc: '备注', default: '实例',
GitHub: evan-org/yapi
6 7 8 9 10 11 12 13 14 15
} catch (err) { return {}; } }; // 处理字符串换行 const handleWrap = (str) => _.isString(str) ? str.replace(/\n/gi, "<br/>") : str; const messageMap = { desc: "备注", default: "实例", maximum: "最大值",
+ 3 other calls in file
underscore.keys is the most popular function in underscore (11266 examples)