9

[Use-case: portable Lo-Dash code] I've been looking for an elegant Lo-Dash empty-string validator in our nodeJS projects for some time now, because _.isEmpty() only handles Arrays and Objects. So I always ended up doing it with the ugly old-fashioned likes of:

if (typeof(myString) !== "undefined" && myString.trim() !== "") {
  // handle the string
}

... Until I stumbled upon _.toString(), which returns '' for null and undefined. So I created a mixin in our extendedLodash.js helper that uses only Lo-Dash methods. However I'm uncertain of caveats; any comment on what I might be overseeing is welcome:

'use strict';

const _ = require('lodash');

/**
 * Extension of _.isEmpty(), which will return `true` for 
 *   "", 0, [], {}, false, undefined and null
 * @param stringOrObjectOrNull
 * @returns {boolean}
 */
function empty(stringOrObjectOrNull) {
  return _(stringOrObjectOrNull).toString().trim() === ""
      || _.isEmpty(stringOrObjectOrNull);
}

_.mixin({
  empty
});

module.exports = _;

Now we can use this:

const _ = require('../lib/helpers/extendedLodash'); 
if (!_.empty(myStringOrObject)) {
  // handle the string ( ...or object ;-| )
}

And test:

  console.log('is empty `[]`: ' + _.empty([]) + " -> " + "`" + _.toString([]) + "`");
  console.log('is empty `{}`: ' + _.empty({}) + " -> " + "`" + _.toString({}) + "`");
  console.log('is empty `""`: ' + _.empty("") + " -> " + "`" + _.toString("") + "`");
  console.log('is empty `"    "`: ' + _.empty("    ") + " -> " + "`" + _.toString("    ") + "`");
  console.log('is empty `0`: ' + _.empty(0) + " -> " + "`" + _.toString(0) + "`");
  console.log('is empty `false`: ' + _.empty(false) + " -> " + "`" + _.toString(false) + "`");
  console.log('is empty `null`: ' + _.empty(null) + " -> " + "`" + _.toString(null) + "`");
  console.log('is empty `undefined`: ' + _.empty(undefined) + " -> " + "`" + _.toString(undefined) + "`");

Note: Going over this code again, I realize this behaves just as fuzzy as PHP's empty() method ... I'm not sure that is appropriate; I just want to avoid necessity of catching undefined where variables might not be declared (...)

sneeuwitje
  • 131
  • 1
  • 1
  • 6
  • 4
    As stated in the dupe link, `if (myString !== undefined && myString !== null && myString !== "") { }` is equivalent to `if(myString) { }` because `null`, `undefined` and `""` are all falsy values. – ibrahim mahrir Apr 10 '18 at 20:46
  • 1
    Maybe make sure you trim() when doing this: _.toString(stringOrObjectOrNull) === "" – Stradosphere Apr 10 '18 at 20:56
  • The scope of this issue for me was not just javascript, but more lodash, which has broader application; e.g. typescript. Maybe this is more about writing portable code with lodash then ... any suggestions to alter this post in that direction @ibrahimmahrir ? Or do you still consider it duplicate? – sneeuwitje Apr 10 '18 at 21:02
  • thanks @Stradosphere, I just added that trim() – sneeuwitje Apr 10 '18 at 21:24
  • @ibrahimmahrir : JS tag removed to emphasize this topic is all-about Lodash, not JS. I'd appreciate your arguments against removing the duplicate flag you placed. – sneeuwitje Apr 10 '18 at 21:39
  • @sneeuwitje Do you want me to vote to reopen it? – ibrahim mahrir Apr 10 '18 at 22:52
  • I'm not sure @ibrahimmahrir ... I just fumbled a plnkr (https://plnkr.co/edit/PStmy2) that shows ```!== undefined``` will not work in regular JS ... so how is that? I think the duplicate tag is not appriate here indeed, but you may leave it ... thanks for the get-back. – sneeuwitje Apr 10 '18 at 23:26
  • 1
    @sneeuwitje I've voted to reopen it. You may also want to add a **Note** to your question to mention the closing and describe the nature of your need (like you did in [this comment](https://stackoverflow.com/questions/49762477/validating-a-non-empty-string-in-lodash?noredirect=1#comment86541163_49762477)). Good luck! :) – ibrahim mahrir Apr 10 '18 at 23:35

0 Answers0