Skip to content

Commit

Permalink
refactor consolidate
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethbruskiewicz committed Aug 22, 2024
1 parent ab870ee commit ce13c83
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 57 deletions.
10 changes: 1 addition & 9 deletions lib/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DataHarmonizer } from '@/lib';
import { findLocalesForLangcodes } from '@/lib/utils/i18n';
import { Template, findSlotNamesForClass } from '@/lib/utils/templates';
import { wait } from '@/lib/utils/general';
import { invert, removeNumericKeys } from '@/lib/utils/objects';
import { invert, removeNumericKeys, consolidate } from '@/lib/utils/objects';
import { setup1M, buildSchemaTree } from '@/lib/utils/1m';
import { createDataHarmonizerContainer, createDataHarmonizerTab } from '@/web';

Expand Down Expand Up @@ -176,14 +176,6 @@ export class AppContext {
locales = this.getLocaleData(template);
}

// Consolidate function for reducing objects
function consolidate(iterable, reducer) {
if (iterable) {
return Object.entries(iterable).reduce(reducer, {});
}
return {};
}

const defaultLocale = {
langcode: 'default',
nativeName: 'Default',
Expand Down
37 changes: 6 additions & 31 deletions lib/utils/general.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { consolidate } from "@/lib/utils/objects";

export function wait(ms) {
return new Promise((r) => setTimeout(r, ms));
}
Expand Down Expand Up @@ -37,37 +39,10 @@ export const tap = (id) => {
return id;
};

export const nullValuesToString = (obj) =>
Object.entries(obj).reduce((acc, [key, val]) => {
return Object.assign(acc, {
[key]: val === null ? '' : val,
});
}, {});

/**
* Creates an idempotent handler for a given operation.
* @param {Function} operation - The operation to be performed, e.g., updateHotCell.
* @param {number} n - The maximum number of times the operation can be performed.
* @returns {Function} - A handler function that can be called with arguments for the operation.
*/
export const createIdempotentHandler = (operation) => (n) => {
let remainingCalls = n;

return (...args) => {
console.log(
'createIdempotentHandler thunk called: ' +
(n - remainingCalls) +
' times with ' +
args
);
operation(...args);
if (remainingCalls > 0) {
operation(...args);
remainingCalls--;
}
// No operation if remainingCalls is 0 or less
};
};
export const nullValuesToString = (obj) => consolidate(obj,
(acc, [key, val]) => Object.assign(acc, {
[key]: val === null ? '' : val,
}));

export function stripDiv(html) {
const div = document.createElement('div');
Expand Down
30 changes: 13 additions & 17 deletions lib/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import i18next from 'i18next';
import jqueryI18next from 'jquery-i18next';
// import tags from 'language-tags';

import { flattenObject } from '@/lib/utils/objects';
import { flattenObject, consolidate } from '@/lib/utils/objects';
import label_translations_file from '@/web/translations/translations.json';
import {
MULTIVALUED_DELIMITER,
Expand All @@ -18,20 +18,16 @@ export const interface_translation = transformStructFirstSpec(
// TODO: refactor `with*` to JSONPath
const withNamespace =
(namespace, addons = {}) =>
(obj) =>
Object.entries(obj).reduce(
(acc, [lang, translation]) => ({
...Object.assign(acc, {
[lang]: {
[namespace]: {
...translation,
...addons,
},
},
}),
}),
{}
);
(obj) => consolidate(obj, (acc, [lang, translation]) => ({
...Object.assign(acc, {
[lang]: {
[namespace]: {
...translation,
...addons,
},
},
}),
}));

const englishIsDefault = (translation_object) => {
// if there is an english resource translation, use it as the default translation
Expand Down Expand Up @@ -199,12 +195,12 @@ export function findLocalesForLangcodes(langcodes) {
}

export const translateObject = (obj, lng = i18next.language, delimiter = '_') =>
Object.entries(obj).reduce((acc, [key, val]) => {
consolidate(obj, (acc, [key, val]) => {
return Object.assign(acc, {
[i18next.t(key.replace(/ /g, delimiter), { lng })]:
val !== null ? i18next.t(val, { lng }) : null,
});
}, {});
});

export function getLanguageNameInLanguage(langCode) {
if (typeof Intl.DisplayNames !== 'undefined') {
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,12 @@ export function looseMatchInObject(keys) {

export const takeKeys = (keys) => (obj) =>
Object.fromEntries(Object.entries(obj).filter(([key]) => keys.includes(key)));


// Consolidate function for reducing objects
export function consolidate(iterable, reducer) {
if (iterable) {
return Object.entries(iterable).reduce(reducer, {});
}
return {};
}

0 comments on commit ce13c83

Please sign in to comment.