Commit b18f0092 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Prefer lodash pickBy

Removes the omitEmptyProperties method in favour
of lodash pickBy
parent 7cfef3af
import Vue from 'vue'; import Vue from 'vue';
import { parseBoolean, omitEmptyProperties } from './lib/utils/common_utils'; import { pickBy } from 'lodash';
import { parseBoolean } from './lib/utils/common_utils';
import ConfirmDanger from './vue_shared/components/confirm_danger/confirm_danger.vue'; import ConfirmDanger from './vue_shared/components/confirm_danger/confirm_danger.vue';
export default () => { export default () => {
...@@ -22,12 +23,15 @@ export default () => { ...@@ -22,12 +23,15 @@ export default () => {
return new Vue({ return new Vue({
el, el,
provide: omitEmptyProperties({ provide: pickBy(
htmlConfirmationMessage, {
confirmDangerMessage, htmlConfirmationMessage,
additionalInformation, confirmDangerMessage,
confirmButtonText, additionalInformation,
}), confirmButtonText,
},
(v) => Boolean(v),
),
render: (createElement) => render: (createElement) =>
createElement(ConfirmDanger, { createElement(ConfirmDanger, {
props: { props: {
......
...@@ -746,25 +746,3 @@ export const isLoggedIn = () => Boolean(window.gon?.current_user_id); ...@@ -746,25 +746,3 @@ export const isLoggedIn = () => Boolean(window.gon?.current_user_id);
*/ */
export const convertArrayOfObjectsToCamelCase = (array) => export const convertArrayOfObjectsToCamelCase = (array) =>
array.map((o) => convertObjectPropsToCamelCase(o)); array.map((o) => convertObjectPropsToCamelCase(o));
/**
* This method takes an object and omits any properties
* that are null, undefined, false or empty. The returned
* object only includes properties that have a `truthy` value.
*
* @param {Object} obj - Target object to be checked
* @returns {Object} Resulting object with only truthy properties
*/
export const omitEmptyProperties = (obj = {}) => {
const entries = Object.entries(obj);
if (!entries.length) {
return obj;
}
return entries.reduce((acc, [key, value]) => {
if (!value) {
return acc;
}
return { ...acc, [key]: value };
}, {});
};
...@@ -1051,30 +1051,4 @@ describe('common_utils', () => { ...@@ -1051,30 +1051,4 @@ describe('common_utils', () => {
expect(result).toEqual([{ hello: '' }, { helloWorld: '' }]); expect(result).toEqual([{ hello: '' }, { helloWorld: '' }]);
}); });
}); });
describe('omitEmptyProperties', () => {
let res;
const empty = { foo: null, bar: undefined, baz: '', qux: false };
const values = { a: 'a', b: 1, c: { d: 'd' }, e: ['f'] };
beforeEach(() => {
res = Object.keys(
commonUtils.omitEmptyProperties({
...empty,
...values,
}),
);
});
it('returns an object with properties that have a value', () => {
const valueKeys = Object.keys(values);
expect(res).toEqual(valueKeys);
});
it('ignores properties that are empty, nullable or falsy', () => {
Object.keys(empty).forEach((key) => {
expect(res).not.toContain(key);
});
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment