FileExportScriptInjector { static String buildNamespaceObject(final String namespace) { final int pkgSepIndex = namespace.lastIndexOf(NS_SEPARATOR); String raw = ""; if (pkgSepIndex > 0) { final String nsPkg = namespace.substring(0, pkgSepIndex); final String[] nsPkgObject = {"", ""}; Arrays.asList(nsPkg.split("\\.")) .forEach(p -> { nsPkgObject[0] += p; nsPkgObject[1] += nsPkgObject[0] + " = " + nsPkgObject[0] + JS_OBJ_SUFFIX + "\n"; nsPkgObject[0] += NS_SEPARATOR; }); raw = nsPkgObject[1]; } raw += namespace; return raw; } FileExportScriptInjector();   FileExportScriptInjector(final Consumer<String> scriptInjector); void inject(); static final String NS_SEPARATOR; static final String NS; static final String JS_OBJ_SUFFIX; }
@Test public void testNamespaces() { assertEquals("window = window || {};\n" + NS + "JsFileSaver", buildNamespaceObject(NS + JsFileSaver.class.getSimpleName())); assertEquals("window = window || {};\n" + NS + "JsPdf", buildNamespaceObject(NS + JsPdf.class.getSimpleName())); assertEquals("nonamespace", buildNamespaceObject("nonamespace")); }