sindresorhus / is

Type check values

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: is.nonEmptyStringAndNotWhitespace(value)

ivankatliarchuk opened this issue · comments

Currently there is .emptyStringOrWhitespace(value), it would be good to enhance library with nonEmptyStringAndNotWhitespace(value).

Returns true if is.nonEmptyString(value) or if it's a string that is all whitespace

Now

.filter((el) => is.nonEmptyString(el) && !is.emptyStringOrWhitespace(el))

With negated type

.filter(is.nonEmptyStringAndNotWhitespace)

Your examples do not do the same thing.

.filter((el) => is.nonEmptyString(el) && !is.emptyStringOrWhitespace(el))

This check prevents whitespace-only strings

.filter(s.nonEmptyStringOrWhitespace)

This allows whitespace-only strings. If you read the name.

I think what you want is is.nonEmptyStringAndNotWhitespace. I'm not sure that's worth it as you could just do:

.filter((el) => !is.emptyStringOrWhitespace(el))

Thanks for the reply -)
The proposed solution e.g. .filter((el) => !is.emptyStringOrWhitespace(el)) does not work.
Example test case https://codesandbox.io/s/npm-playground-forked-u1cypj?file=/src/index.js

const is = require("@sindresorhus/is");

const labels = ["labelA", "", null];
const addLabels = ["labelB", "     ", undefined, , ,];

function transpose() {
  return [...new Set([...labels, ...addLabels])].filter(
    (el) => !is.emptyStringOrWhitespace(el)
  );
}

console.log(transpose());

(4) ["labelA", null, "labelB", undefined]

const is = require('@sindresorhus/is');

const labels = ['labelA', '', null]
const addLabels =['labelB', '    ', undefined, , ,]

function transpose() {
  return [...new Set([...labels, ...addLabels])]
    .filter((el) => is.nonEmptyString(el) && !is.emptyStringOrWhitespace(el));
}

console.log(transpose())

["labelA", "labelB"]

Some screenshots as well
solution-one
solution-two

Absolutely right the proposal is for nonEmptyStringAndNotWhitespace. I changed the topic name.

You are checking non-empty twice. It could be:

.filter((el) => is.string(el) && !is.emptyStringOrWhitespace(el))

But yeah, I think is.nonEmptyStringAndNotWhitespace could be useful.

Fixed by #161