fix: cleaned up the tests and added in a lowdash 'hasOwn' alternative

Signed-off-by: Mark David Avery <mark@webark.cc>
This commit is contained in:
Mark David Avery
2022-05-06 09:04:25 -07:00
parent acfd217142
commit fe2d99e471
2 changed files with 41 additions and 11 deletions
@@ -132,19 +132,49 @@ describe('parseEntityTransformParams', () => {
it('does not return a sub key if an incorrect longer key is requested', () => {
entity.spec = {
...entity.spec,
strValue: 'st1',
boolValue: true,
numValue: 4,
arrValue: [4, 5],
nullValue: null,
undefValue: undefined,
'field-with.dot': 'fd1',
plain: true,
numLike: 4,
arrayLike: [4, 5],
};
expect(
parseEntityTransformParams({
fields:
'kind,spec.field-withdot.other.item,kind.other.other,spec.plain.other,spec.numLike.other,spec.arrayLike.other',
})!(entity),
).toEqual({
kind: 'k',
});
parseEntityTransformParams({ fields: 'kind,spec.strValue.other' })!(
entity,
),
).toEqual({ kind: 'k' });
expect(
parseEntityTransformParams({ fields: 'kind,spec.boolValue.other' })!(
entity,
),
).toEqual({ kind: 'k' });
expect(
parseEntityTransformParams({ fields: 'kind,spec.numValue.other' })!(
entity,
),
).toEqual({ kind: 'k' });
expect(
parseEntityTransformParams({ fields: 'kind,spec.arrValue.other' })!(
entity,
),
).toEqual({ kind: 'k' });
expect(
parseEntityTransformParams({ fields: 'kind,spec.nullValue.other' })!(
entity,
),
).toEqual({ kind: 'k' });
expect(
parseEntityTransformParams({ fields: 'kind,spec.undefValue.other' })!(
entity,
),
).toEqual({ kind: 'k' });
expect(
parseEntityTransformParams({ fields: 'kind,spec.field-with.dot.other' })!(
entity,
),
).toEqual({ kind: 'k' });
});
});
@@ -23,7 +23,7 @@ import { parseStringsParam } from './common';
function getPathArrayAndValue(input: Entity, field: string) {
return field.split('.').reduce(
([pathArray, inputSubset], pathPart, index, fieldParts) => {
if (inputSubset.hasOwnProperty(pathPart)) {
if (lodash.hasIn(inputSubset, pathPart)) {
return [pathArray.concat(pathPart), inputSubset[pathPart]];
} else if (fieldParts[index + 1] !== undefined) {
fieldParts[index + 1] = `${pathPart}.${fieldParts[index + 1]}`;