all files / lib/ index.js

100% Statements 31/31
95.83% Branches 23/24
100% Functions 3/3
100% Lines 23/23
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39     184× 184× 184× 184×         184× 183× 88× 88× 88×   88×   6779× 5059×   1720× 427×             184×    
import isNil from "lodash/isNil";
import isArray from "lodash/isArray";
import isFunction from "lodash/isFunction";
import isObject from "lodash/isObject";
import mergeWith from "lodash/mergeWith";
 
import Fakerator from "./fakerator";
 
module.exports = function(localeID = "default") {
	let locale;
	try {
		locale = require("./locales/" + localeID + "/index");
	} catch (e) {
		// ignored
	}
	
	if (locale) {
		if (localeID != "default") {
			let fallbackID = locale._meta.fallback || "default";
			let fbLocale = require("./locales/" + fallbackID + "/index");
			if (EfbLocale) {
				// Merge locale and fallback
				locale = mergeWith(locale, fbLocale, (objValue) => {
					// DON'T MERGE ARRAYS
					if (isArray(objValue) || isFunction(objValue))
						return objValue;
					
					if (!isNil(objValue) && !isObject(objValue))
						return objValue;
				});
			}
		}
	} else {
		locale = require("./locales/default/index");
	}
 
	return new Fakerator(locale);
};