-
-
Notifications
You must be signed in to change notification settings - Fork 44
Implement includes
operator
#342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…hin a store containing a string or an array
src/includes/index.ts
Outdated
if (typeof a === 'number') { | ||
throw new Error('first argument should be an unit of array or string'); | ||
} | ||
|
||
return a.includes(b as string); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a
must be either an array or a string, so I would write it like this:
if (typeof a === 'string') {
return a.includes(b as string);
}
throw new Error('First argument should be a unit of array or string');
I'm not a maintainer, just decided to leave a comment :)
export function includes <T extends string | number>( | ||
a: Store<Array<T>>, | ||
b: Store<T> | T, | ||
): Store<boolean> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only string | number
?
export function includes <T extends string>( | ||
a: Store<T>, | ||
b: Store<T> | T, | ||
): Store<boolean> | ||
export function includes <T extends string | number>( | ||
a: Store<Array<T>>, | ||
b: Store<T> | T, | ||
): Store<boolean> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if:
export function includes <T extends string>( | |
a: Store<T>, | |
b: Store<T> | T, | |
): Store<boolean> | |
export function includes <T extends string | number>( | |
a: Store<Array<T>>, | |
b: Store<T> | T, | |
): Store<boolean> | |
export function includes <T extends string>( | |
a: Store<T>, | |
b: Store<T> | T, | |
): Store<boolean> | |
export function includes <T>( | |
a: Store<Array<T>>, | |
b: Store<T> | T, | |
): Store<boolean> | |
export function includes <T>( | |
a: Store<{ ref: Set<T> }>, | |
b: Store<T> | T, | |
): Store<boolean> |
allow to compare with any list of items?
a: Store<T | Array<T>>, | ||
b: Store<T> | T, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a: Store<T | Array<T>>, | |
b: Store<T> | T, | |
haystack: Store<T | Array<T>>, | |
needle: Store<T> | T, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a, b
is not clearly explains what in what we checking:
includes($email, "@");
includes("@", $email);
it will work by types in both ways, but IDE will suggest a:
, b:
names which is not self-descriptive, I think
const $array = createStore([1, 2, 3]); | ||
const $isInclude = includes($array, 2); | ||
|
||
console.assert($isInclude.getState() === true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is better do not recommend using .getState()
in documentation.
console.assert($isInclude.getState() === true); | |
$isInclude // => true |
Description
includes
is implemented according to the issueThe operator checking if a store (containing either a string or an array) includes a specified value.
Checklist for a new method
src
directory inparam-case
src/method-name/index.ts
in ESModules export incamelCase
named exportsrc/method-name/method-name.test.ts
src/method-name/method-name.fork.test.ts
test-typings/method-name.ts
// @ts-expect-error
to mark expected type errorimport { expectType } from 'tsd'
to check expected return typesrc/method-name/readme.md
Patronum/MethodName
Motivation
,Formulae
,Arguments
andReturn
sections for each overloadExample
section for each overloadsrc/method-name/readme.md
title
. Make sure it uses camelCase syntax just like the method itselfslug
. Use param-case to write it. In most cases it will be just liketitle
desription
with one short sentence describing what method useful forgroup
. To categorize method on/operators
page. Full list of available groups, you can see in documentation/src/content/config.ts