How to use the isFunction function from underscore

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

underscore.isFunction is a function provided by the Underscore.js library that checks whether a given value is a function.

470
471
472
473
474
475
476
477
478
479
};

// loop through mixins object and attach their handler methods
// to res.locals['mixin-name'].
_.each(mixins, function (mixin, name) {
  const handler = _.isFunction(mixin.handler) ? mixin.handler : function () {
    return function (key) {
      this.options = this.options || {};
      this.options.fields = this.options.fields || {};
      key = hoganRender(key, this);
fork icon14
star icon13
watch icon12

501
502
503
504
505
506
507
508
509
    }
}, opts );
opts = _.extend( {
    readFields: function( req ) {
        var values = {};
        var fields = _.isFunction( opts.fields ) ? opts.fields( req ) : opts.fields;
        _.each( fields, function( fieldOpts, field ) {
            var source = fieldOpts.source || "postman";
            var filterName = fieldOpts.filter || opts.filters[ fieldOpts.type ] || field;
fork icon3
star icon9
watch icon1

+ 11 other calls in file

How does underscore.isFunction work?

underscore.isFunction is a function provided by the Underscore.js library that checks whether a given value is a function. When you call underscore.isFunction, you provide a single input parameter. The function checks whether the input parameter is a function by checking its internal [[Class]] property, which is a string that represents the object's class. If the input parameter is a function, underscore.isFunction returns true. Otherwise, it returns false. By checking whether a value is a function, underscore.isFunction can be used to ensure that functions are only called with other functions as arguments, or to filter arrays or collections to only contain functions. This can be useful in a variety of JavaScript contexts, such as event handling, functional programming, and callback-based APIs.

2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
};

// Invoke a method (with arguments) on every item in a collection.
_.invoke = function(obj, method) {
  var args = slice.call(arguments, 2);
  var isFunc = _.isFunction(method);
  return _.map(obj, function(value) {
    return (isFunc ? method : value[method]).apply(value, args);
  });
};
fork icon0
star icon2
watch icon0

+ 6 other calls in file

240
241
242
243
244
245
246
247
248
249
 * If an object is thenable, then return the object itself, otherwise wrap it into a promise
 * @param {any}
 * @deferred
 */
when: function (param) {
  if (param != null && _.isFunction(param.then)) {
    return param;
    // eslint-disable-next-line no-undefined
  } else if (param !== undefined) {
    return new Promise(function(resolve) {
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const _ = require("underscore");

const items = [
  { id: 1, name: "Item 1", handleClick: () => console.log("Clicked item 1!") },
  { id: 2, name: "Item 2", handleClick: null },
  { id: 3, name: "Item 3", handleClick: () => console.log("Clicked item 3!") },
];

const clickableItems = _.filter(items, (item) => {
  return _.isFunction(item.handleClick);
});

console.log(clickableItems);

In this example, underscore.isFunction is used to filter an array of objects called items to only include those that have a handleClick property that is a function. The _.filter() function is used to iterate over each object in the array, returning only those objects that pass the filter function. When _.isFunction(item.handleClick) is called on each object, it returns true for the first and third objects, which have a handleClick property that is a function. The second object has a handleClick property that is null, so it is filtered out. The resulting clickableItems array contains only the first and third objects, which have functions for their handleClick properties. Note that underscore.isFunction can also be used in other contexts, such as validating input arguments to functions or checking whether a variable is a function before invoking it.

75
76
77
78
79
80
81
82
83
84
  // avoid undefined
  value = context.global.get(subtokens[1]) != null ? context.global.get(subtokens[1]) : null
} else if (token.startsWith('tx.')) {
  // use global translator
  const tx = context.global.get('tx');
  value = _.isFunction(tx) ? tx(token.replace('tx.', ''), language) : token;
} else if (specials[token] != null) {
  value = specials[token];
} else {
  // excluded global, payload, need to access to the context
fork icon0
star icon1
watch icon1

1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
if (a == null || b == null) return a === b;
// Unwrap any wrapped objects.
if (a._chain) a = a._wrapped;
if (b._chain) b = b._wrapped;
// Invoke a custom `isEqual` method if one is provided.
if (a.isEqual && _.isFunction(a.isEqual)) return a.isEqual(b);
if (b.isEqual && _.isFunction(b.isEqual)) return b.isEqual(a);
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className != toString.call(b)) return false;
fork icon0
star icon0
watch icon1

+ 20 other calls in file

26
27
28
29
30
31
32
33
34
35
    this._drift = 0;
    this._syncInterval = 0;
}

_makeRequest(query, callback, route, security, method, attempt = 0) {
    assert(_.isUndefined(callback) || _.isFunction(callback), 'callback must be a function or undefined');
    assert(_.isObject(query), 'query must be an object');

    let queryString;
    const type = _.last(route.split('/')),
fork icon0
star icon0
watch icon1

3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a function, else `false`.
* @example
*
* _.isFunction(_);
* // => true
*
* _.isFunction(/abc/);
* // => false
fork icon0
star icon0
watch icon0

+ 13 other calls in file