Giter VIP home page Giter VIP logo

Comments (35)

sindresorhus avatar sindresorhus commented on September 12, 2024 2

Regarding, any and all.

Returns true if any of the input values returns true in the predicate:

any(predicate, values…)

Returns true if all of the input values returns true in the predicate:

all(predicate, values…)

Example:

any(is.string, {}, true, 'πŸ¦„');
//=> true

from is.

brandon93s avatar brandon93s commented on September 12, 2024 2

Others for consideration:

  • .arrowFunction()
  • .numeric() - returns true for numbers and numeric strings (e.g. '4.3')
  • .arraylike() - true for things like arguments and results of document.getElementsByTagName('p'). They have a .length property and index accessing but no array methods (pop, push, etc.).
  • .decimal()
  • .even()
  • .odd()
  • .greaterThan(4, 3) or .gt(4, 3) and equal to equivalent .gte(4, 3)
  • .lessThan(3, 4) or .lt(3, 4) and equal to equivalent .lte(3, 4)
  • .truthy() - is.truthy = x => !!x;
  • .falsy()
  • .emptyOrSpaces() - returns true for .empty() or whitespace strings (e.g. ' '). Alternatively, just adjust the behavior of .empty()
  • .arguments() - specifically checks for arguments
  • .positive()
  • .negative()
  • .safeInteger() - Here is some reading
  • .nil() - true for is.null(x) || is.undefined(x) - often checked together
  • .char() - true for an ASCII character?
  • .emoji() - true for emoji

Update: Added a few more... total brain dump. Glad to implement any of these pending need / usefulness.

from is.

gioragutt avatar gioragutt commented on September 12, 2024 1

@noamokman perheps we can implement that into is.any or is.all, where they would like:

is.all = (pred, ...values) => {
  if (!is.array(values) || is.empty(values)) {
    return false;
  }

  const hits = values.find(pred);
  return hits.length === values.length;
};

and is.any will check hits.length > 0.
or something of that sort.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024 1

is.falsy > value => !value ?

πŸ‘ And truthy. I like it because it makes the intent more clear. Often people misuse !foo when they actually don't want falsy.

from is.

yeskunall avatar yeskunall commented on September 12, 2024 1

is.emoji for when you don't want emojis in certain places. Legacy databases (MySQL with UTF-8 encoding) don't handle emojis well, so in that case, this addition might come in handy.

I've had to work on a project where emojis in the username were disallowed altogether. That's another use-case.

from is.

glhrmv avatar glhrmv commented on September 12, 2024

How are you thinking .withinRange should work? Is something like this OK?

is.withinRange = (x, arr) => (x > Math.min.apply(Math, arr)) && (x < Math.max.apply(Math, arr));

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

@glhrmv I was thinking:

is.withinRange = (input, [lower, upper]) => input >= lower && input <= upper;

from is.

glhrmv avatar glhrmv commented on September 12, 2024

Array destructuring is definitely more elegant, I forgot that was a thing.

As for .domElement, that'll require following this procedure for testing, right? If so, I can get on that as well.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

As for .domElement, that'll require following this procedure for testing, right?

Yup. Let’s do it in a separate test file.

from is.

glhrmv avatar glhrmv commented on September 12, 2024

I couldn't make browser-env (or even window) make document defined in the test file, for some reason. In any case, the idea I had of is.domElement(x) was to make sure x is an instanceof Element, but obviously Node doesn't support Element, either.

I'm giving this up to anyone who wants to deal with it, and I'm interested to see how this type of issue is solved.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

@glhrmv You can see how it's done here: https://github.com/sindresorhus/linkify-urls/blob/4db31ab79d62dc0adf6b3c086a6a68f955343918/test.js#L2-L7

from is.

yeskunall avatar yeskunall commented on September 12, 2024

With #4:

  • .generator()
  • .generatorFunction()

from is.

nrebhun avatar nrebhun commented on September 12, 2024

Hey @sindresorhus, Hacktoberfest is upon us! 😜

How about getting some more eyes on this issue/repo with the relevant tag?

from is.

fega avatar fega commented on September 12, 2024

what about include it in AVA t.is ?

from is.

gioragutt avatar gioragutt commented on September 12, 2024

@fega wouldn't that create some weird circular dependency?

from is.

noamokman avatar noamokman commented on September 12, 2024

@gioragutt nope, you would just install the latest version at the ava project

Unrelated, what about:

const a = 5;
const b = {};
const c = 'asd';
is.string(a,b,c); // false

from is.

gioragutt avatar gioragutt commented on September 12, 2024

@noamokman what is the case you're checking here exactly?

this returns false because the method only relates to arguments[0], which is a, so is.string(5) === false makes sense.

what do you expect this to return?

from is.

gioragutt avatar gioragutt commented on September 12, 2024

@sindresorhus

  • Observable
  • Promise
  • Subject (related to Observable)

from is.

noamokman avatar noamokman commented on September 12, 2024

@noamokman what is the case you're checking here exactly?

this returns false because the method only relates to arguments[0], which is a, so is.string(5) === false makes sense.

what do you expect this to return?

I mean that if multiple values are passed is should test all values

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

I mean that if multiple values are passed is should test all values

No, that's should be a separate method.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

@gioragutt

Observable

πŸ‘ See how to do it here: https://github.com/sindresorhus/is-observable/blob/master/index.js

Promise

Already included.

Subject (related to Observable)

What is this and why do you need to detect it? Can you link to some reading material?

from is.

gioragutt avatar gioragutt commented on September 12, 2024

@sindresorhus Subjects are extended from Observables. They are basically Observables you can call next on, and invoke observers

from is.

melvin0008 avatar melvin0008 commented on September 12, 2024

@sindresorhus @gioragutt

Could I get more insight into what is expected from .any and .all ?

is.any(value, [types] ) ?
is.any(5,[string, Number]) ?

Is it similar for .all?
Is this what you have in mind?

from is.

gioragutt avatar gioragutt commented on September 12, 2024

@melvin0008 I guess this is up for discussion, because as you stated, there are many ways to interpret any and all.

@sindresorhus your thoughts?

from is.

gioragutt avatar gioragutt commented on September 12, 2024

is.falsy > value => !value ?
could be used in other methods as well, f.e in #16

from is.

brandon93s avatar brandon93s commented on September 12, 2024

inRange could be enhanced to support:

Dates

is.inRange(new Date('March 1, 2017'), [new Date('January 1, 2017'), new Date('July 1, 2017')])
// => true

date.getTime() >= min && date.getTime() <= max

Questions:

  • What would the lower bound default to? Epoch?

Text - Alphabetical

is.inRange('Brandon', ['Andrew', 'Cole'])
// => true

name >= min && name <= max

Questions:

  • What would the lower bound default to?
  • Is there a use case for this?

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

.arrowFunction()

Use-case?

.numeric()

I don't like the looseness of this. We should not mix types like that. We could have numericString().

.arraylike()

πŸ‘

.decimal()

I considered this when doing .integer, but I couldn't come up with a use-case. It makes sense to have .integer because some methods might accept just whole numbers, but only accepting decimals doesn't make sense, a whole number like 3 is still a valid decimal.

.even()
.odd()

πŸ‘

.greaterThan(4, 3)

We already have min and max which overlaps with gte(), but it might be worth it still, for readability reasons. (@gioragutt Thoughts?) gte is not a good name though, even though it's verbose, I would go with, greaterThanOrEqual().

.truthy() - is.truthy = x => !!x;
.falsey()

πŸ‘ , but: falsey => falsy

.emptyOrSpaces()

πŸ‘

is.inRange(new Date('March 1, 2017'), [new Date('January 1, 2017'), new Date('July 1, 2017')])
// => true

I would make a new method for this: dateInRange([date], range)

date would be optional and default to new Date(). The range would not have an optional lower bound as it doesn't make sense here.

Text - Alphabetical
is.inRange('Brandon', ['Andrew', 'Cole'])

πŸ‘Ž I don't see the point in supporting a range for this.

from is.

gioragutt avatar gioragutt commented on September 12, 2024

@sindresorhus

.emptyOrSpaces()

I'd prefer .emptyOrWhitespaces, usually this is the use case

.lt/gt/lte/gte

I honestly think that doing this would reduce code readability.
I guess this can be useful when you want to provide a comparer to a method, though.
This can make it a bit better.
The question is - is the use case of using the methods over just using the operator worth adding 4 methods to the API.

If we wish to keep the API cleaner, this might not be a good idea to do those methods. But if you believe this wouldn't create much of a bloat, then I'm fine with it.

dateInRange([date], range)

Do you mean that range would be an upper bound only? I think that it's kinda counter intuitive to say that date is in range, when range is actually just a max-value.

And there are definitely use cases for checking against a lower bound.

Text - Alphabetical

I agree with you on that there's no point in a range (this is basically .includes),

But this does give 2 new ideas:

  • Alphabetic - check is string is purely alphabetic ([a-z]. [A-Z], and whitespaces)
  • Alphanumeric - checking if a string is alphanumeric ([a-z]. [A-Z], [0-9] and whitespaces. Although if we look for it, the spec for what's an A/N string might have something more than what I specified).

and this in turn makes sense to include numeric, or as you've suggested numericString.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

I'd prefer .emptyOrWhitespaces, usually this is the use case

Agreed, but should be .emptyOrWhitespace (without the s).

If we wish to keep the API cleaner, this might not be a good idea to do those methods. But if you believe this wouldn't create much of a bloat, then I'm fine with it.

I'm -0. Maybe if someone can convince me it has benefits of including it over just using the operators.

Do you mean that range would be an upper bound only? I think that it's kinda counter intuitive to say that date is in range, when range is actually just a max-value.

No, range would be an array with lower and upper bound.

Alphabetic - check is string is purely alphabetic ([a-z]. [A-Z], and whitespaces)

Use-case?

Alphanumeric - checking if a string is alphanumeric ([a-z]. [A-Z], [0-9] and whitespaces. Although if we look for it, the spec for what's an A/N string might have something more than what I specified).

Use-case?

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

.arguments() - specifically checks for arguments

πŸ‘Ž No one should be using the arguments object now that we have rest/spread.

.positive()
.negative()

I guess this is the same case as gt and gte. Convince us.

.safeInteger() - Here is some reading

πŸ‘ Since we already alias some other useful native methods.

.nil() - true for is.null(x) || is.undefined(x) - often checked together

This is already included as nullOrUndefined. I considered nil, but it's not clear enough.

.char() - true for an ASCII character?

Not clear enough name. And, use-case?

.emoji() - true for emoji

Use-case?

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

@brandon93s Awesome braindump! :)

from is.

brandon93s avatar brandon93s commented on September 12, 2024

Appreciate the feedback!

.arrowFunction() - Since you cannot bind or call an arrow function, one could imagine a use case where a library needs to detect that an arrow function has been provided and throw an error to the consumer. Probably unnecessary until encountered, though.

.gt .lt .positive .negative etc - I'm in agreement on these ones. The operators themselves are more concise than having methods. No need for them.

.decimal .char .emoji - No immediate use cases come to mind.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

.arrowFunction() - Since you cannot bind or call an arrow function, one could imagine a use case where a library needs to detect that an arrow function has been provided and throw an error to the consumer. Probably unnecessary until encountered, though.

That's a good use-case. I'm πŸ‘ on this now.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

Legacy databases (MySQL with UTF-8 encoding) don't handle emojis well, so in that case, this addition might come in handy.

We won't add anything for legacy reasons.

I've had to work on a project where emojis in the username were disallowed altogether. That's another use-case.

This is a good use-case though. I'm πŸ‘ on this.

from is.

sindresorhus avatar sindresorhus commented on September 12, 2024

Most of this should be implemented now. Let's open new issues for additional requests.

from is.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.