boo1ean / casual

Fake data generator for javascript
3.03k stars 152 forks source link

casual locales are broken #99

Open josher19 opened 4 years ago

josher19 commented 4 years ago

@boo1ean Right now casual locales are broken ... importing one pollutes parts of the global namespace in casual which then causes strange results, like Arabic showing up in German addresses when using casual.ar_SY and casual.de_DE

Example failing test: https://gist.github.com/josher19/c4a0450d1eede980342a9e4010c72845

Perhaps Related: #8 and #77

josher19 commented 4 years ago

PS: There is an ugly work-around, but it is not very efficient.

You can delete the cache and re-require casual for each language/locale:

function refresh(moduleName = 'casual') {
    delete require.cache[require.resolve(moduleName)];
    Object.keys(require.cache)
              .filter(pathName => pathName && pathName.indexOf(moduleName) !== -1)
              .forEach(pathName => delete require.cache[pathName]);
    return require(moduleName);
}

const getLang = lang => refresh()[lang];

Example usage showing the problem:

const casual = require('casual');
casual.seed(41)
casual.ar_SY.city // Arabic, as expected, but followed by
casual.de_DE.city // now Arabic instead of German?

getLang('ar_SY').city; 
getLang('de_DE').city; // German, not Arabic