Skip to content

Commit

Permalink
fix(getvaluefrompath): return ConditionKey type values
Browse files Browse the repository at this point in the history
Can also return undefined in case doesn't exist the path

fix #14
  • Loading branch information
roggervalf committed Mar 20, 2020
1 parent 61d7544 commit febbcb7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import {
EffectBlock,
ConditionBlock,
StatementInterface,
ConditionKey,
Context,
MatchConditionInterface,
} from './types';

const reDelimiters = /\${([^}])*}/g;
const trim = / +(?= )|^\s+|\s+$/g;

export function getValueFromPath(data: any, path: string): string {
export function getValueFromPath(data: any, path: string): ConditionKey {
let value = data;
path.split('.').forEach(step => {
if (value) value = value[step];
Expand All @@ -18,7 +19,7 @@ export function getValueFromPath(data: any, path: string): string {
if (Array.isArray(value)) return `{${value}}`;
if (value instanceof Object) return 'undefined';

return `${value}`;
return value;
}

const specialTrim = (str: string): string => str.replace(trim, '');
Expand Down
20 changes: 13 additions & 7 deletions tests/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export default (): void => {
user: { id: 456, bestfriends: [123, 532] },
};
expect(getValueFromPath(context, 'user.bestfriends')).toBe('{123,532}');
expect(getValueFromPath(context, 'user.id')).toBe('456');
expect(getValueFromPath(context, 'user.id')).toBe(456);
});

it('get undefined from non existed path', () => {
const context = {
user: { id: 456, bestfriends: [123] },
};
expect(getValueFromPath(context, 'user.id.pets')).toBe('undefined');
expect(getValueFromPath(context, 'company')).toBe('undefined');
expect(getValueFromPath(context, 'user.id.pets')).toBe(undefined);
expect(getValueFromPath(context, 'company')).toBe(undefined);
expect(getValueFromPath(context, 'user')).toBe('undefined');
});
});
Expand Down Expand Up @@ -66,13 +66,16 @@ export default (): void => {
};
const secondStatementConfig = {
condition: {
greatherThan: { 'user.age': [30, 40] },
lessThan: { 'user.age': [30, 40] },
},
};
const conditionResolver = {
greatherThan: (data: number, expected: number): boolean => {
return data > expected;
},
lessThan: (data: number, expected: number): boolean => {
return data < expected;
},
};
expect(
new Statement(firstStatementConfig).matchConditions({
Expand All @@ -82,7 +85,7 @@ export default (): void => {
).toBe(true);
expect(
new Statement(secondStatementConfig).matchConditions({
context: { user: { age: 42 } },
context: { user: { age: 35 } },
conditionResolver,
})
).toBe(true);
Expand All @@ -96,13 +99,16 @@ export default (): void => {
};
const secondStatementConfig = {
condition: {
greatherThan: { 'user.age': [50, 45] },
lessThan: { 'user.age': [50, 45] },
},
};
const conditionResolver = {
greatherThan: (data: number, expected: number): boolean => {
return data > expected;
},
lessThan: (data: number, expected: number): boolean => {
return data < expected;
},
};
expect(
new Statement(firstStatementConfig).matchConditions({
Expand All @@ -112,7 +118,7 @@ export default (): void => {
).toBe(false);
expect(
new Statement(secondStatementConfig).matchConditions({
context: { user: { age: 42 } },
context: { user: { age: 60 } },
conditionResolver,
})
).toBe(false);
Expand Down

0 comments on commit febbcb7

Please sign in to comment.