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

Maybe.fromEmpty? #213

Closed
customcommander opened this issue Jun 3, 2019 · 3 comments
Closed

Maybe.fromEmpty? #213

customcommander opened this issue Jun 3, 2019 · 3 comments
Milestone

Comments

@customcommander
Copy link
Contributor

I was wondering if it would make sense to have this new static method?

Maybe.fromEmpty([]);
//=> None
Maybe.fromEmpty({});
//=> None
Maybe.fromEmpty('');
//=> None
Maybe.fromEmpty(null);
//=> None
Maybe.fromEmpty(undefined);
//=> None

It could (or perhaps should) delegate to fantasy-land/empty to work out if a given value is its type's empty value.

Use case:

I have a css rules object which contains css rules to apply to a string if there's any. Otherwise the string is returned as is. Problem is that the object may actually be "empty" i.e. it is an object but has no properties.

const styles = (css, text) =>
  Maybe
    .fromEmpty(css) // e.g. {'text-decoration': 'underline', 'color': 'red'}
    .map(obj => obj.entries()) // [['text-decoration', 'underline'], ['color', 'red']]
    .map(arr => arr.map(pair => pair.join(':'))) // ['text-decoration:underline', 'color:red']
    .map(arr => arr.join(';')) // 'text-decoration:underline;color:red'
    .fold(text)(str => `<span style="${str}">${text}</span>`);

styles({'font-style': 'italic'}, 'foo');
//=> '<span style="font-style:italic">foo</span>'

styles({}, 'foo');
//=> 'foo'

Any thoughts?

@ulfryk
Copy link
Member

ulfryk commented Jun 3, 2019

👍 great idea @customcommander
I had one usecase just today :)

Can you implement it?

@ulfryk ulfryk added this to the 0.9.1 milestone Jun 3, 2019
@customcommander
Copy link
Contributor Author

I would love it. I'll give it a shot :)

customcommander added a commit to customcommander/monet.js that referenced this issue Jun 3, 2019
customcommander added a commit to customcommander/monet.js that referenced this issue Jun 3, 2019
@ulfryk ulfryk closed this as completed in 5344698 Jun 3, 2019
@ulfryk
Copy link
Member

ulfryk commented Jun 3, 2019

snapshot released as [email protected]

ulfryk pushed a commit that referenced this issue Jan 21, 2020
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

2 participants