casual icon indicating copy to clipboard operation
casual copied to clipboard

casual locales are broken

Open josher19 opened this issue 4 years ago • 1 comments

@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 avatar Sep 15 '20 23:09 josher19

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

josher19 avatar Sep 16 '20 00:09 josher19