Skip to content
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

Add .appendToArray() method #124

Closed
wants to merge 4 commits into from

Conversation

subins2000
Copy link

Allows appending or creating an array item with .appendToArray :

store.appendToArray('foo', 'a') // foo will be ['a']
store.appendToArray('foo', 'b') // foo will be ['a', 'b']

test/index.ts Show resolved Hide resolved
@@ -223,6 +223,25 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
this.store = store;
}

/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types here should limit it to only allow using appendToArray on a key defined to have an array value or unknown.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm new to typescript, I tried to understand much from the existing code to make this one. Sorry, I'm not sure how to do what you mean 😅 Can you suggest one please ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being new to something is a great opportunity to learn more about it. Give it a shot at least. I can help if you really cannot do it.

Copy link
Author

@subins2000 subins2000 Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I know if the overload definition I set is correct, I tried doing

store.set('a', 1)
store.appendToArray('a', 2)

These lines didn't show any error in VSCode, it should right ?, that's how typescript should work right ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests would fail if it's correct, as TS would refuse to compile. You need to make it TS fail, and then you can use the @ts-expect-error comment to ignore the error just for the test.

Copy link
Author

@subins2000 subins2000 Aug 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried many, but can't figure it out. Is there any examples that I can look for ? I've been at this for hours now 😅. The best I could come up to do "limit it to only allow using appendToArray on a key defined to have an array value". Each line is a separate try and not a group :

appendToArray<Key extends keyof T, Index extends keyof any[]>(key: Key, value: T[Key][Index]): void
appendToArray<Key extends keyof T, Index extends keyof T[Key]>(key: Key, value: T[Key][Index]): void
appendToArray<Key extends keyof T, Index extends T[Key][keyof T[Key]]>(key: Key, value: T[Key][Index]): void

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus I'm not able to make it, would like to know the solution please

source/index.ts Outdated Show resolved Hide resolved
source/index.ts Outdated
const list = this.get(key, [] as T[Key]);

if (!Array.isArray(list)) {
throw new TypeError('Config param already set and is not array');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new TypeError('Config param already set and is not array');
throw new TypeError(`The key \`${key}\` is already set to a non-array value`);

source/index.ts Outdated Show resolved Hide resolved
source/index.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

It must be documented in the readme.

@subins2000 subins2000 changed the title Add .appendToArray() #109, https://github.com/sindresorhus/electron-s… Add .appendToArray() Aug 21, 2020
source/index.ts Show resolved Hide resolved
@@ -223,6 +223,25 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
this.store = store;
}

/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being new to something is a great opportunity to learn more about it. Give it a shot at least. I can help if you really cannot do it.

readme.md Outdated
@@ -338,6 +338,19 @@ const unsubscribe = conf.onDidAnyChange(key, callback);
unsubscribe();
```

#### .appendToArray(key, value)

Append an item to array.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a bit more verbose and also explain the behavior of how it works, like if the existing value is not array, or if there's no value.

readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title Add .appendToArray() Add .appendToArray() method Aug 21, 2020
@LanceGundersen
Copy link

This is exactly what I am looking for! Anything I can help to get this merged?

@sindresorhus
Copy link
Owner

Anything I can help to get this merged?

Yes, see: #124 (comment)

Base automatically changed from master to main January 23, 2021 08:51
@sindresorhus
Copy link
Owner

@subins2000 Bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants