I have a single file that has utils methods. Each is exported individually. I am in a situation where one util requires another. I currently define the functions used before they're used. But I came across ES6's cyclic dependency and using that removes the need to meticulously organize the util functions. Is there any reason I should not use that?
Simplified eg: Currently:
export const safeParsing = (str) => {
try { return JSON.parse(str); }
catch (e) { return {}; }
};
export const parseToken = (t) => safeParsing(t);
Using cyclic dep:
import * as self from 'src/jsUtils';
export const parseToken = (t) => self.safeParsing(t);
export const safeParsing = (str) => {...}
Edit: Using cyclic imports also enables me to spyOn (Jest) inner functions. Eg:
test('parseToken uses safe parsing', () => { ... spyOn safeParsing ... });
Relevant refs:
ES6 modules: Export single class of static methods OR multiple individual methods
https://stackoverflow.com/a/40242291/958598