cibernox/ember-cpm

View on GitHub
addon/macros/concat.js

Summary

Maintainability
A
0 mins
Test Coverage
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';

/**
  Keeps n arrays concatenated.

  Example:
  ```js
  obj = Ember.Object.createWithMixins({
    itemsA: [],
    itemsB: [],
    itemsC: [],
    allItems: EmberCPM.Macros.concat('itemsA', 'itemsB', 'itemsC');
  });

  obj.get('itemsA').pushObjects(['a', 'b']);
  obj.get('allItems') //=> ['a', 'b']

  obj.get('itemsB').pushObjects(['c']);
  obj.get('allItems') //=> ['a', 'b', 'c']

  obj.get('itemsC').pushObjects(['d']);
  obj.get('allItems') //=> ['a', 'b', 'c', 'd']

  obj.get('itemsB').pushObjects(['e', 'f']);
  obj.get('allItems') //=> ['a', 'b', 'c', 'e', 'f', 'd']
  ```

  @method concat
  @for macros
  @param *arguments Dependent keys with the arrays to concat.
  @return {Array}
*/
export default function(...args) {
  const arrayReduce = (...args) => {
    return args.reduce((prev, value) => {
      return prev.concat(value || []);
    }, []);
  };

  return computed(...args.map((a) => normalizeArrayKey(a, [])), arrayReduce);
}