0

I wrote this function that construct an object based if some values are null or not.

protected temporalFilter({ range }: Filter): W {
    const temporalKey = ['created', 'bootedSince', 'firstComunication'];
    if (temporalKey.includes(range.key)) {
      if (range.from && !range.to) {
        return {
          [range.from as keyof T]: { gte: range.from }
        } as unknown as W;
      }
      else if (!range.from && range.to) {
        return {
          [range.to as keyof T]: { lte: range.to }
        } as unknown as W;
      }
      else if (range.from && range.to) {
        return {
          AND: [
            { [range.key as keyof T]: { lte: range.to } },
            { [range.key as keyof T]: { gte: range.from } }
          ]
        } as unknown as W;
      }
      else return {} as unknown as W;
    }
    else return null;
  }

But, there is a compact way to do that or some refactoring that can I do?

1
  • pls share reproducable example. Where did you get Filter. I assume it is from elastic search? Commented Aug 31, 2021 at 10:39

2 Answers 2

2

This is the most compact yet readable form I can think of:

temporalFilter({range}: Filter): W {
  if (['created', 'bootedSince', 'firstComunication'].includes(range.key)) {
    const f = range.from ? { [range.from]: {gte: range.from} } : undefined;
    const t = range.to ? { [range.to]: {lte: range.to} } : undefined;
    return ((f && t) ? { AND: [f, t] } : (f || t)) as unknown as W;
  } else {
    return null;
  }
}

I suggest you fix your typing; the usage of so many unknown in a single function suggests poor typing. Furthermore, this { [range.from]: {gte: range.from} } it is not wrong, but it is strange to have the same value as filtering condition and as key, perhaps you meant { [range.key]: {gte: range.from} }

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your suggestions. I put as unknow as W because I have this error the type is not assignable to W. How can I resolve?
The error you are getting is due to problems within Filter and Wif you can provide the complete context (eg Filter definition and what W is), i can address you better.
Btw, I edited my answer to address your current typing issues in the same way you did
W is a type generated from Prisma and Filter as defined as follow: export type Filter = { key?: any, field?: any, range?: { key?: any, from?: any, to?: any, }, };
0

Something like this should work right? I did not test this, just a thought on how to use conditional spreading:

 return {
     ...(!!range.from && {[range.from]: {gte: range.from}}),
     ...(!!range.to && {[range.to]: {lte: range.to}})
    }

2 Comments

Unfortunately this doesn't cover for the null case, write a complete answer. You have some typos, you closed the parentheses with brackets, other than that, i like alot your solution so far.
I am not sure what you meant by null case, the includes condition can be tested separately and return null when necessary. Is there some other case I am missing? @darklightcode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.