Detect if _ is lodash or underscore

What is the “authoritative” way to determine if _ loaded using lodash or underscore?

I use lodash for a project in an environment where underscore can sometimes be downloaded as well.

Currently, I have come up with the following:

 /** * lodash defines a variable PLACEHOLDER = '__lodash_placeholder__' * so check if that is defined / contains the string "lodash" */ if ( typeof( _.PLACEHOLDER ) == 'undefined' || _.PLACEHOLDER.indexOf( 'lodash' ) < 0 ) { // _ is underscore, do what I need to access lodash } 

Important update: The above code does NOT work!

Is there an "authoritative" way to determine if _ lodash or underscore is?

Notes:
This is a specific query to find a way to determine if lodash or underscore is loaded in _ variable:
1. I am out of control whether the underline is loaded or not. (lodash is inside my control and will always be loaded).
2. You cannot rely on the boot order of lodash / underscore.
3. The underline version that is loading is likely to change (this is part of the CMS environment that can be updated).
4. Lodash 4.17.x has over 300 functions. My code uses many functions in lodash.
5. Lodash contains many features that emphasize. 6. Some of the functions that exist in both libraries have different implementations.

+7
javascript lodash
source share
2 answers

The code sent in the question does not work, because PLACEHOLDER is a private variable that is renamed during minimization.

Therefore, I applied the concept of “function detection” as indicated in the comments. Note that this method may break if future versions of the underscore are collapsed in all of these functions or if lodash denounces any of these functions:

 var isLodash = false; // If _ is defined and the function _.forEach exists then we know underscore OR lodash are in place if ( 'undefined' != typeof(_) && 'function' == typeof(_.forEach) ) { // A small sample of some of the functions that exist in lodash but not underscore var funcs = [ 'get', 'set', 'at', 'cloneDeep' ]; // Simplest if assume exists to start isLodash = true; funcs.forEach( function ( func ) { // If just one of the functions do not exist, then not lodash isLodash = ('function' != typeof(_[ func ])) ? false : isLodash; } ); } if ( isLodash ) { // We know that lodash is loaded in the _ variable console.log( 'Is lodash: ' + isLodash ); } else { // We know that lodash is NOT loaded } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.3/lodash.js"></script> 
+1
source share

Following similar lines, as @bhantol already noted, there is a Document Migration with a list of differences between lodash and underscores that were not compatible with. Can't they be used? For example,

 if ( typeof( _.invoke ) !== 'undefined' ){ // it lodash } 

But yes, reinforcing the comments of @ felix-kling and @tadman and others, it might be more reliable to limit the issue at the level (for example, a specific method), rather than the entire library.

+3
source share

All Articles