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;
};
fork icon0
star icon2
watch icon0

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;
fork icon0
star icon1
watch icon1

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':
fork icon0
star icon1
watch icon1

+ 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({
fork icon0
star icon1
watch icon1

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));
  }
}
fork icon0
star icon1
watch icon1

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'))
fork icon0
star icon0
watch icon1

+ 3 other calls in file

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: '实例',
fork icon0
star icon0
watch icon1

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: "最大值",
fork icon0
star icon0
watch icon1

+ 3 other calls in file