How to use the isEmpty function from underscore

Find comprehensive JavaScript underscore.isEmpty code examples handpicked from public code repositorys.

underscore.isEmpty checks if a given value is an empty object, collection, map or set.

2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
// See [WebKit Bug 80797](https://bugs.webkit.org/show_bug.cgi?id=80797)
_.max = function(obj, iterator, context) {
  if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
    return Math.max.apply(Math, obj);
  }
  if (!iterator && _.isEmpty(obj)) return -Infinity;
  var result = {computed : -Infinity, value: -Infinity};
  each(obj, function(value, index, list) {
    var computed = iterator ? iterator.call(context, value, index, list) : value;
    computed > result.computed && (result = {value : value, computed : computed});
fork icon0
star icon2
watch icon0

0
1
2
3
4
5
6
7
8
9
10
const _ = require('underscore');
const utils = require('./helpers/utils');
const when = utils.when;
const moment = require('moment');


const isEmpty = value => _.isEmpty(value) && !_.isNumber(value);
const byString = function(o, s) {
  let str = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
  str = str.replace(/^\./, '');           // strip a leading dot
  var a = str.split('.');
fork icon0
star icon1
watch icon1

How does underscore.isEmpty work?

The _.isEmpty function of Underscore.js library is used to determine whether the given value is an empty object or not. If the given value is null or undefined, the function returns true. If the given value is an object with no enumerable properties or an empty array, the function returns true. Otherwise, it returns false.

89
90
91
92
93
94
95
96
97
this.branchNames = function() {
  return _.map(typeSchemas, function(typeSchema) { return makeFullyQualifiedTypeName(typeSchema, namespace); });
};

function validateArgs(typeSchemas) {
  if (!_.isArray(typeSchemas) || _.isEmpty(typeSchemas)) {
    throw new InvalidSchemaError('Union must have at least 1 branch');
  }
}
fork icon0
star icon1
watch icon1

584
585
586
587
588
589
590
591
592
593
let userId = data.userId;
// let onlineMember = getOnlineUsers();
// let checkUserOnline = _.forEach(onlineMember, function (result) {
//    return result
// }) 
//  let isUserOffline = _.isEmpty(checkUserOnline);

 let isUserOffline ;
 _.forEach(onlineUsersInSocket, function (result) {
 console.log(result)
fork icon0
star icon1
watch icon0

Ai Example

1
2
3
4
5
6
7
const _ = require("underscore");

const obj = {};
const arr = [];

console.log(_.isEmpty(obj)); // true
console.log(_.isEmpty(arr)); // true

In this example, we require the underscore library and create an empty object obj and an empty array arr. We then use the _.isEmpty function to check if both obj and arr are empty. The function returns true for both cases since they are indeed empty.

57
58
59
60
61
62
63
64
65
 * everywhere else
 */
var dontBuildMobileOnWindows = function (platforms, options) {
  options = options || {};

  if (! _.isEmpty(platforms) && process.platform === "win32") {
    // Default message to print when we can't Cordova on Windows
    var MESSAGE_NOTHING_ON_WINDOWS =
      "Currently, it is not possible to build mobile apps on a Windows system.";
fork icon0
star icon1
watch icon2

131
132
133
134
135
136
137
138
139
140
    length = 6;
} else {
    // nothing to do
}

if (!inputChars || _.isEmpty(inputChars)) {
    inputChars = '0123456789';
} else {
    // nothing to do
}
fork icon0
star icon0
watch icon1

+ 3 other calls in file

1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
var lon = req.body.lon;
var confirmed = true;
*/

// Validation
if (_.isEmpty(date) || !postcode || _.isEmpty(starttime) || starttime == '00:00' || _.isEmpty(endtime) || endtime == '00:00') {
    var docs = {
        // Test details
        date: date, // Date
        engineer: engineer,
fork icon0
star icon0
watch icon1

432
433
434
435
436
437
438
439
440
441
442
443
444


Accounts.prototype.contains = function(address){
    var accounts = LocalStore.get(this.options.varName);


    if(_.isUndefined(address)
       || _.isEmpty(address))
        return false;


    // Add '0x' prefix if not available
    address = formatAddress(address);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

62
63
64
65
66
67
68
69
70
71
        this.data.values = this.data.values.concat(flowDatas)
    }
},
afterFindOne: async function(){
    let id = this.id;
    if(id && _.isEmpty(this.data.values) && this.spaceId === 'template'){
        this.data.values = _.find(getFlows(), function(flow){
            return flow._id === id;
        })
    }
fork icon0
star icon0
watch icon1

3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is empty, else `false`.
* @example
*
* _.isEmpty(null);
* // => true
*
* _.isEmpty(true);
* // => true
fork icon0
star icon0
watch icon0

+ 9 other calls in file

245
246
247
248
249
250
251
252
253
254
255
256


         bindings[bindingName] = bindingPolicy;
      }
    }
  }
  return _.isEmpty(bindings) ? null : bindings;
};


/**
* Ensures that a url points to an SSL endpoint.
fork icon0
star icon0
watch icon0

77
78
79
80
81
82
83
84
85
86
    assignedVariables = assignedVariables.concat(
      file.computeAssignedVariables());
  });
  assignedVariables = _.uniq(assignedVariables);

  return _.isEmpty(assignedVariables) ? undefined : assignedVariables;
},

// Output is a list of objects with keys 'source', 'servePath', 'sourceMap',
// 'sourcePath'
fork icon0
star icon0
watch icon2

+ 5 other calls in file