Comments (35)
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.
Others for consideration:
-
.arrowFunction()
-
.numeric()
- returns true for numbers and numeric strings (e.g. '4.3') -
.arraylike()
- true for things likearguments
and results ofdocument.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 forarguments
-
.positive()
-
.negative()
-
.safeInteger()
- Here is some reading -
.nil()
- true foris.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.
@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.
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.
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.
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.
@glhrmv I was thinking:
is.withinRange = (input, [lower, upper]) => input >= lower && input <= upper;
from is.
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.
As for .domElement, that'll require following this procedure for testing, right?
Yup. Letβs do it in a separate test file.
from is.
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.
@glhrmv You can see how it's done here: https://github.com/sindresorhus/linkify-urls/blob/4db31ab79d62dc0adf6b3c086a6a68f955343918/test.js#L2-L7
from is.
With #4:
-
.generator()
-
.generatorFunction()
from is.
Hey @sindresorhus, Hacktoberfest is upon us! π
How about getting some more eyes on this issue/repo with the relevant tag?
from is.
what about include it in AVA t.is ?
from is.
@fega wouldn't that create some weird circular dependency?
from is.
@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.
@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.
- Observable
- Promise
- Subject (related to Observable)
from is.
@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.
I mean that if multiple values are passed is should test all values
No, that's should be a separate method.
from is.
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.
@sindresorhus Subjects are extended from Observables. They are basically Observables you can call next
on, and invoke observers
from is.
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.
@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.
is.falsy > value => !value
?
could be used in other methods as well, f.e in #16
from is.
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.
.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.
.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 anA/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.
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.
.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.
@brandon93s Awesome braindump! :)
from is.
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.
.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.
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.
Most of this should be implemented now. Let's open new issues for additional requests.
from is.
Related Issues (20)
- Property 'function' does not exist on type 'typeof is'. HOT 1
- Proposal: is.nonEmptyStringAndNotWhitespace(value) HOT 10
- [v5] Better handling of `NaN`
- test code perf HOT 1
- Can't access default export properties when `moduleResolution: 'NodeNext'` HOT 3
- Add is.renderInReact or is.notRenderInReact HOT 1
- Question: What is the reason Node.js below version 14.16 is not supported? HOT 2
- add type inference to "nonEmptyArray"
- Confused between class_ docs and implementation HOT 1
- How to shorten: `"prop" in obj && obj.prop`
- Improve type narrowing for `nonEmptyArray` HOT 1
- is.numericString type guarding returns `x is string` and messes up typescript HOT 4
- Parcel JS build issue - No Main HOT 1
- v5.4.0 "Cannot find module '@sindresorhus/is' or its corresponding type declarations" HOT 4
- `plainObject` is incompatible with `structuredClone` HOT 2
- Some type guards under assert are not working HOT 1
- directInstanceOf(instance, class) throws error if instance is null or undefined HOT 1
- Struggling to configure jest to compile `is` HOT 1
- is.enumCase method is not a type guard HOT 2
- Typing errors with the isEmptyStringOrWhitespace type guard HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from is.