Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConditionKey is set as "string | number | boolean" yet data turns into string when boolean is set in the context. #14

Closed
yaser-ali-s opened this issue Mar 19, 2020 · 1 comment
Labels
bug Something isn't working enhancement New feature or request

Comments

@yaser-ali-s
Copy link

yaser-ali-s commented Mar 19, 2020

Running the following code:

const context: Context = {
    entity: {
        User   : {
            active: false,
        }
    },
};

const conditionResolver = {
    boolean: (data, expected) => {
        console.log('typeof data', typeof data);
        console.log('typeof expected', typeof expected);
        
        return data === expected;
    },
};


// this.condition in matchConditions is the following:
// {
//     boolean: {
//         'entity.User.active'  : false,
//     }
// }
console.log('matchConditions', matchConditions({context, conditionResolver}));

Yields the following:

typeof data string
typeof expected boolean
matchConditions false

This would be fine except in types.ts the following is set:

export type ConditionKey = string | number | boolean
type Resolver = (data: ConditionKey, expected: ConditionKey) => boolean;

Expected behavior is data in conditionResolver function would be the same type passed in the context.

I'm assuming this is from the fact that the matchConditions function uses getValueFromPath which is set to return string, as it returns `${value}`, as opposed to value, as seen here:

// getValueFromPath
export function getValueFromPath(data: any, path: string): string {
  let value=data;
  path.split('.').forEach((step) => {
    if(value)
      value=value[step];
  });
  
  if (Array.isArray(value)) return `{${value}}`;
  if (value instanceof Object) return 'undefined';

  return `${value}`;
}

// matchConditions
  matchConditions({
    context,
    conditionResolver
  }:MatchConditionInterface): boolean {
    return (
      (conditionResolver && this.condition && context
        ? Object.keys(this.condition).every(condition =>
            Object.keys(
              this.condition ? this.condition[condition] : {}
            ).every(path =>{
              if(this.condition){
                const conditionValues=this.condition[condition][path]
                if(conditionValues instanceof Array){
                  return conditionValues.some(value=>conditionResolver[condition](
                    getValueFromPath(context, path),
                    value
                  ))
                }
                return conditionResolver[condition](
                  getValueFromPath(context, path),
                  conditionValues
                )
              }
              return conditionResolver[condition](
                getValueFromPath(context, path),
                ''
              )
            }
              
            )
          )
        : true)
    );
  }

Is there a reason as to why it is always returned as string? What else does it affect?

I would like to suggest using lodash's property function, as you can't go wrong with it, and it's already quite efficient.

You can even change the applyContext function to utilize the template function provided by them.

roggervalf pushed a commit that referenced this issue Mar 20, 2020
## [3.0.2](v3.0.1...v3.0.2) (2020-03-20)

### Bug Fixes

* **getvaluefrompath:** return ConditionKey type values ([febbcb7](febbcb7)), closes [#14](#14)
@roggervalf
Copy link
Owner

Hi @yaser-ali-s, already made a change for that. I would like this package life on its own, that's why it doesn't have dependencies. Thank you for notice this issue.

@roggervalf roggervalf added bug Something isn't working enhancement New feature or request labels Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants