@bigbinary/neeto-commons-frontend 2.1.34 → 2.1.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/initializers.js CHANGED
@@ -5,7 +5,7 @@ import i18n from 'i18next';
5
5
  import { serializeKeysToSnakeCase, keysToCamelCase, matches, deepFreezeObject } from '@bigbinary/neeto-commons-frontend/pure';
6
6
  import { resetAuthTokens } from '@bigbinary/neeto-commons-frontend/utils';
7
7
  import { Toastr } from '@bigbinary/neetoui';
8
- import { evolve, omit, values, mergeDeepLeft } from 'ramda';
8
+ import { evolve, omit, values, isEmpty, mergeDeepLeft } from 'ramda';
9
9
  import { initReactI18next } from 'react-i18next';
10
10
  import Logger from 'js-logger';
11
11
 
@@ -55,6 +55,7 @@ var HEADERS_KEYS = {
55
55
  contentType: "Content-Type",
56
56
  accept: "Accept"
57
57
  };
58
+ var ANY_CASE_STR = "__ANY_CASE__";
58
59
 
59
60
  var shouldNot = function shouldNot(skip) {
60
61
  return _typeof$1(skip) === "object" || !skip;
@@ -2358,19 +2359,75 @@ var replaceNullValuesWithGetter = function replaceNullValuesWithGetter(inputObje
2358
2359
  }
2359
2360
  return result;
2360
2361
  };
2361
- var getSentenceCaseProcessor = function getSentenceCaseProcessor() {
2362
- return {
2363
- type: "postProcessor",
2364
- name: "sentenceCasePlaceholderAndTypography",
2365
- process: function process(value, keys) {
2366
- if (keys.some(function (str) {
2367
- return (str.startsWith("placeholder.") || str.startsWith("typography.")) && !str.includes("anyCase.");
2368
- })) {
2369
- return value.charAt(0).toLocaleUpperCase() + value.slice(1).toLocaleLowerCase();
2362
+
2363
+ /*
2364
+ For some reason dynamic values available via options param doesn't have formatter 'anyCase' applied.
2365
+ So here we are just re applying.
2366
+ */
2367
+ var wrapAnyCaseIfApplicable = function wrapAnyCaseIfApplicable(value, dynamic) {
2368
+ return Object.keys(dynamic).map(function (item) {
2369
+ var format = anyCaseFormatter(dynamic[item]);
2370
+ if (value.includes(format)) return format;
2371
+ return dynamic[item];
2372
+ });
2373
+ };
2374
+ var isAnyCaseStr = function isAnyCaseStr(str) {
2375
+ return str.startsWith(ANY_CASE_STR) && str.endsWith(ANY_CASE_STR);
2376
+ };
2377
+ var lowerCaseDynamicText = function lowerCaseDynamicText(value, dynamic) {
2378
+ /*
2379
+ Example:
2380
+ translation: {
2381
+ create: "Create {{entity1}}, {{entity2, anyCase}} and {{entity3}} and apply COLOR"
2382
+ }
2383
+ // ...
2384
+ t("create", {entity1: "Car", entity2: 'BuS', enity3: "Truck"});
2385
+ */
2386
+
2387
+ // example: ['Car', '__ANY_CASE__BuS__ANY_CASE__', 'Truck']
2388
+ var dynamicArr = wrapAnyCaseIfApplicable(value, dynamic);
2389
+ //example pattern: (Car)|(__ANY_CASE__BuS__ANY_CASE__)|(Bus)
2390
+ var regexPattern = dynamicArr.map(function (delimiter) {
2391
+ return "(".concat(delimiter, ")");
2392
+ }).join("|");
2393
+ /* Generated array
2394
+ [
2395
+ 'Create ', // static
2396
+ 'Car', // dynamic
2397
+ ', ', // static
2398
+ '__ANY_CASE__BuS__ANY_CASE__', // dynamic
2399
+ ' and ', // static
2400
+ 'Truck', // dynamic
2401
+ ' and apply COLOR' // static
2402
+ ]
2403
+ */
2404
+ var splitArr = value.split(new RegExp(regexPattern)).filter(Boolean);
2405
+ return splitArr.map(function (item, index) {
2406
+ if (dynamicArr.includes(item)) {
2407
+ if (isAnyCaseStr(item)) {
2408
+ return item.replaceAll(ANY_CASE_STR, "");
2409
+ } else if (index === 0) {
2410
+ // Don't lower case if it is first word.
2411
+ return item;
2370
2412
  }
2371
- return value;
2413
+ return item.toLocaleLowerCase();
2372
2414
  }
2373
- };
2415
+ return item;
2416
+ }).join("");
2417
+ };
2418
+ var sentenceCaseProcessor = {
2419
+ type: "postProcessor",
2420
+ name: "sentenceCaseProcessor",
2421
+ process: function process(value, _, options) {
2422
+ var dynamic = omit(["lng", "lngs", "ns"], options); // removes default args
2423
+ if (isEmpty(dynamic)) return value; // Do nothing if no dynamic values
2424
+
2425
+ return lowerCaseDynamicText(value, dynamic);
2426
+ }
2427
+ };
2428
+ var anyCaseFormatter = function anyCaseFormatter(value) {
2429
+ if (value) return ANY_CASE_STR + value + ANY_CASE_STR;
2430
+ return value;
2374
2431
  };
2375
2432
 
2376
2433
  var generic = {
@@ -2491,7 +2548,7 @@ var commonsEn = {
2491
2548
  // eslint-disable-next-line import/no-mutable-exports
2492
2549
  var taxonomies = {};
2493
2550
  var initializeI18n = function initializeI18n(hostTranslations) {
2494
- var _window$globalProps, _i18n$services$format;
2551
+ var _window$globalProps, _i18n$services$format, _i18n$services$format2;
2495
2552
  var packageTranslations = preval.require("./configs/scripts/getPkgTranslations.js");
2496
2553
  var commonsTranslations = {
2497
2554
  en: {
@@ -2499,10 +2556,16 @@ var initializeI18n = function initializeI18n(hostTranslations) {
2499
2556
  }
2500
2557
  };
2501
2558
  var resources = [hostTranslations, commonsTranslations, packageTranslations].reduce(mergeDeepLeft);
2502
- if ((_window$globalProps = window.globalProps) !== null && _window$globalProps !== void 0 && _window$globalProps.taxonomies) {
2503
- taxonomies = replaceNullValuesWithGetter(window.globalProps.taxonomies);
2504
- }
2505
- i18n.use(Browser).use(initReactI18next).use(getSentenceCaseProcessor()).init({
2559
+ var defaultTaxonomyKeys = Object.keys(resources.en.translation.taxonomyDefaultLabels || {});
2560
+ var defaultTaxonomies = Object.fromEntries(defaultTaxonomyKeys.map(function (key) {
2561
+ return [key, {
2562
+ singular: null,
2563
+ plural: null
2564
+ }];
2565
+ }));
2566
+ var hostTaxonomies = ((_window$globalProps = window.globalProps) === null || _window$globalProps === void 0 ? void 0 : _window$globalProps.taxonomies) || {};
2567
+ taxonomies = replaceNullValuesWithGetter(mergeDeepLeft(hostTaxonomies, defaultTaxonomies));
2568
+ i18n.use(Browser).use(initReactI18next).use(sentenceCaseProcessor).init({
2506
2569
  resources: resources,
2507
2570
  fallbackLng: "en",
2508
2571
  interpolation: {
@@ -2512,7 +2575,7 @@ var initializeI18n = function initializeI18n(hostTranslations) {
2512
2575
  escapeValue: false,
2513
2576
  skipOnVariables: false
2514
2577
  },
2515
- postProcess: ["sentenceCasePlaceholderAndTypography"],
2578
+ postProcess: ["sentenceCaseProcessor"],
2516
2579
  detection: {
2517
2580
  order: ["querystring", "cookie", "navigator", "path"],
2518
2581
  caches: ["cookie"],
@@ -2537,6 +2600,7 @@ var initializeI18n = function initializeI18n(hostTranslations) {
2537
2600
  return formatter.format(sanitizedItems);
2538
2601
  };
2539
2602
  });
2603
+ (_i18n$services$format2 = i18n.services.formatter) === null || _i18n$services$format2 === void 0 ? void 0 : _i18n$services$format2.add("anyCase", anyCaseFormatter);
2540
2604
  };
2541
2605
 
2542
2606
  function initializeLogger() {