Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created November 17, 2014 11:32
Show Gist options
  • Save radiosilence/788911b9f3dc76afd66e to your computer and use it in GitHub Desktop.
Save radiosilence/788911b9f3dc76afd66e to your computer and use it in GitHub Desktop.
Handy function to turning a nested object into a list of string based constants.
"use strict";
module.exports = {
constantify: function constantify(app, obj, prevK) {
for (var k in obj) {
var nextK = prevK ? prevK + '.' + k : app + '.' + k;
if (obj[k] === null) obj[k] = nextK;
else constantify(app, obj[k], nextK);
}
return obj;
}
};
@radiosilence
Copy link
Author

Usage:

// MyAppConstants.js

module.exports = constantify('MyApp', {
  ActionTypes: {
    MyModel: {
      UPDATE: null
    }
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment