Skip to content

Commit

Permalink
feat(utils): add fromPairs helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dackmin committed Apr 12, 2021
1 parent 5139a36 commit c2189b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/junipero-utils/lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const classNames = (...args) => {

args.map(arg => {
if (!arg) {
return;
return false;
}

if (typeof arg === 'string' || typeof arg === 'number') {
Expand All @@ -18,8 +18,12 @@ export const classNames = (...args) => {
if (v) {
classes.push(k);
}

return false;
});
}

return false;
});

return classes.join(' ');
Expand Down Expand Up @@ -81,3 +85,10 @@ export const cloneDeep = obj =>
res[k] = cloneDeep(v);
return res;
}, {});

export const fromPairs = (pairs = []) =>
pairs.reduce((res, [k, v]) => {
res[k] = v;

return res;
}, {});
11 changes: 11 additions & 0 deletions packages/junipero-utils/lib/core/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
omitBy,
pick,
cloneDeep,
fromPairs,
} from './';

describe('core', () => {
Expand Down Expand Up @@ -299,4 +300,14 @@ describe('core', () => {
expect(error).toBeUndefined();
});
});

describe('fromPairs()', () => {
it('should allow to make an object from a table', () => {
const arr = [['foo', 'bar'], ['stuff', 'thing'], [0, true]];
const obj = fromPairs(arr);
expect(obj.foo).toBe('bar');
expect(obj.stuff).toBe('thing');
expect(obj[0]).toBe(true);
});
});
});

0 comments on commit c2189b2

Please sign in to comment.