kellyselden/ember-awesome-macros

Add string.concat?

billdami opened this issue · 3 comments

Wondering if it would make sense/be useful to add a string.concat() macro, that would allow for the following:

someStr: 'blah',
anotherStr: 'blarg',
yetAnotherStr: 'argh',
foo: string.concat('someStr', 'anotherStr', 'yetAnotherStr', raw('-')) // => "blah-blarg-argh"

I have a custom macro using lazyComputed() from ember-macro-helpers that is close to this:

import lazyComputed from 'ember-macro-helpers/lazy-computed';
import { isNone } from '@ember/utils';

export default function(...values) {
    const separator = values.pop();
    return lazyComputed(...values, (get, ...props) => {
        return props.map(prop => get(prop)).filter(val => !isNone(val)).join(separator);
    });
}

But would be nice to see this as part of ember-awesome-macro's otherwise comprehensive suite of macros. :)

You are probably already aware of this, but pointing it out just in case. It is already possible to do exactly what you want without the need for a custom macro by leveraging composition.

someStr: 'blah',
anotherStr: 'blarg',
yetAnotherStr: 'argh',
foo: join(collect('someStr', 'anotherStr', 'yetAnotherStr'), raw('-')) // => "blah-blarg-argh"

@mriska's comment is what you want, or tag

someStr: 'blah',
anotherStr: 'blarg',
yetAnotherStr: 'argh',
foo: tag`${'someStr'}-${'anotherStr'}-${'yetAnotherStr'}` // => "blah-blarg-argh"

ah perfect, thank you!