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

Fluent API #345

Open
stefaanMLB opened this issue Aug 31, 2023 · 2 comments
Open

Fluent API #345

stefaanMLB opened this issue Aug 31, 2023 · 2 comments

Comments

@stefaanMLB
Copy link

Would be nice to have a fluent API, f. i.

import { mapEntries } from 'radash/fluent'

const ra = {
  name: 'Ra',
  power: 'sun',
  rank: 100,
  culture: 'egypt'
}

ra.mapEntries((key, value) => [key.toUpperCase(), `${value}`]) 
// => { NAME: 'Ra', POWER: 'sun', RANK: '100', CULTURE: 'egypt' }
@osdiab
Copy link

osdiab commented Sep 19, 2023

how would this work? ra is a plain object and doesn't have a mapEntries member.

@stefaanv
Copy link

Can be done with this code. Example for the unique function

declare global {
  interface Array<T> {
    unique(toKey?: SelectFn<T>): Array<T>
  }
}

export interface Array<T> {
  unique(toKey?: SelectFn<T>): Array<T>
}

Array.prototype.unique = function <T>(toKey?: SelectFn<T>) {
  return unique(this, toKey)
}

The unique function can then be used like this:

const u = fish.unique(f => f.name)

Some people prefer this fluent type of API. It also aligns better with the built-in array functions.

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

3 participants