Dan
Dan

Reputation: 381

Organizing a big helper file full of exports

Apologies if this question is too open-ended.

I have a big "helper" file filled with useful functions. They're all exported functions, so

exports.getFirstNameLastName = nameString => { ... }

This file is getting to be pretty big and has enough functions in it that I feel that it can be divided up into smaller, categorized files (e.g. parsingHelper.js, webHelper.js, etc).

I'm coming from a heavily object oriented software dev background. In something like C# I'd create a static class file for each of these categories (parsing, web, etc), then simply do the one import (using ParserHelpers;), then do ParserHelpers.GetFirstNameLastName(...); instead of importing each and every function I end up using.

My question is, is there a way to organize all my helper functions in a similar manner? I'm trying to reduce the number of individually exported/imported items and trying to split this big file into smaller files.

Would rather not use additional packages if I don't have to (using ES6).

Upvotes: 0

Views: 607

Answers (1)

Guilherme Toti
Guilherme Toti

Reputation: 574

Yes! There is a way to do something like that!

// file users.js

exports.users = {
    getFirstName: () => {},
  getLastName: () => {},
  // Add as many as you want
}


// file categories.js
exports.categories = {
    getCategoryById: () => {},
  getCategoryName: () => {}
}



// You can use them separately or you can create another file an union all them:

// file helpers.js

import users from './users'
import categories from './categories'

exports helpers = {
    users,
  categories
}


// This way you can do something like:

import helpers from './helpers.js'

helper.users.getFirstName()

Upvotes: 1

Related Questions