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

Derived keys #67

Open
sindresorhus opened this issue Apr 19, 2021 · 0 comments
Open

Derived keys #67

sindresorhus opened this issue Apr 19, 2021 · 0 comments

Comments

@sindresorhus
Copy link
Owner

I have encountered a need where I would like to create a new key where the value is derived from one or more existing keys.

For example, let's say you have a .name and .email key. You might want to simplify the checking and have a .hasFilledOutAllFields key. This key would be derived from the former keys.

The reason it has to be in Defaults and not just in a custom extension is that it needs to be reactive. So that, if either .name or .email changes, .hasFilledOutAllFields triggers an update too.

My initial thoughts are something like this:

extension Defaults.Keys {
	static let hasFilledOutAllFields = Key<Bool>(derivedFrom: .name, .email) { name, email in
		!name.isEmpty && !email.isEmpty
	}
}

Another use-case is needing the derived key to be settable too. Not sure how that would look like though.

And maybe it would be useful to be able to listen to external events too. For example, for the .hasFilledOutAllFields key, we might also need to ensure the keyboard is not shown or something.

Maybe there's a more general way we could handle this. I'm open to ideas.

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

No branches or pull requests

1 participant