Housing lightweight javascript utility functions for use in ember-cli


Suppose in an ember-cli app I want to reuse a simple utility function in multiple places. For example a function for packing an array of strings into a csv string:

var pack = function(arr) {
  return arr.filter(this, function() {return value != null} ).join(",");

Don’t dwell on the code too much, the point is I’m sure it’s lightweight, stateless and not Ember specific. But I do want to use it from multiple ember-cli modules.

What’s the cleanest strategy for housing this kind of simple utility function in an ember-cli project? Create a lib folder in my project and house it somewhere in there and import it into the ember part with namespacing maybe?

Apologies for the niavety, I’m relatively inexperienced with node style development. Also I’ve refrained from asking on Stack Overflow as the answer would depend on opinion.


Put it in utils or make it an addon.


I’ve put utility functions in something like app/utils/array.js:

export function pack(arr) { ... }

export default { 
  pack: pack 

Then use it like this:

import { pack } from '../utils/array';
pack( abc );


import arrayUtils from '../utils/array';
arrayUtils.pack( abc );

Like varblob said, you can put these in an addon if you want to use them across apps.