@homeappcorporate/utils
v1.0.3
Published
Shared utils between internal projects
Downloads
2
Readme
Utils ·
Shared utils between internal projects
Installing
$ yarn add @homeappcorporate/utils
or
$ npm install @homeappcorporate/utils --save
API
createFormatter(config)
import { createFormatter } from '@homeapp/utils';
const formatter = createFormatter([
{
from: 'id',
to: '_id',
handler: (item, data, from, to) => String(item),
}, {
from: 'foo',
to: ['_foo', '_bar'],
}, {
from: ['bar', 'baz'],
to: '_biz',
},
])
formatter({
id: 1,
foo: 'hello',
bar: { baz: 'world' }
});
// Output:
{
_id: '1',
_foo: { _bar: 'hello' },
_biz: 'world',
}
groupById(data, generateId)
import { groupById } from '@homeapp/utils';
groupById([{ id: '1' }, { id: '2' }])
// Output:
{
byId: { '1': { id: '1' }, '2': { id: '2' } },
ids: ['1', '2'],
}
createResponseToState(dataToState, generateId)
import { createResponseToState } from '@homeapp/utils';
const dataToState = createFormatter([
{ from: 'id', to: 'id', handler: String },
{ from: 'name', to: 'name' },
]);
const responseToState = createResponseToState(dataToState);
responseToState({
success: true,
data: { id: 1, name: 'Vasya' },
pageParams: { length: 100 },
});
// Output:
{
isSuccess: true,
data: {
byId: { '1': { id: '1', name: 'Vasya' } },
ids: ['1'],
},
listTotal: 100
}
createStateToRequestParams(filtersToParams, sortTypeToParams)
import { createStateToRequestParams, createFormatter } from '@homeapp/utils';
const filtersToRequestParams = createFormatter([{ from: 'roles', to: ['roles', 'role'].join('.') }]);
const sortTypeToRequestParams = (type) => {
switch (type) {
case 'userId':
return 'user.id';
default:
return;
}
};
const stateToRequestParams = createStateToRequestParams(filtersToRequestParams, sortTypeToRequestParams);
stateToRequestParams({
page: 1,
pageSize: 10,
sortType: 'userId',
sortDirection: 'ascend',
filters: { roles: ['expert'] },
});
// Output:
{
limits: { page: 1, count: 10 },
sorting: { type: 'user.id', direction: 'asc' },
filter: { 'roles.role': 'expert' }
}