Open6

microCMSのqueries.filterをいい感じに組めるものを作ろうとしてるスクラップ

hanetsukihanetsuki

ケース1

オプションで渡す系を作ってみた。
が、入れ子構造が複雑化すると難読化する。ボツ

hanetsukihanetsuki

ケース2

メソッドチェーン型、優先度付、入れ子などは未対応だけど...
呼び出しの見通しとしては結構良さげ

あとコード綺麗にしたい。

const createFilterQuery = (function() {
  let query = ''

  return {
    equals: function(fieldId: string, value: string) {
      query += `${fieldId}[equals]${value}`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    notEquals: function(fieldId: string, value: string) {
      query += `${fieldId}[not_equals]${value}`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    lessThan: function(fieldId: string, value: string) {
      query += `${fieldId}[less_than]${value}`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    greaterThan: function(fieldId: string, value: string) {
      query += `${fieldId}[greater_than]${value}`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    contains: function(fieldId: string, value: string) {
      query += `${fieldId}[contains]${value}`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    beginsWith: function(fieldId: string, value: string) {
      query += `${fieldId}[begins_with]${value}`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    notExists: function(fieldId: string) {
      query += `${fieldId}[not_exists]`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    },
    exists: function(fieldId: string) {
      query += `${fieldId}[exists]`
      return {
        and: () => {
          query += `[and]`
          return this as typeof createFilterQuery
        },
        or: () => {
          query += `[or]`
          return this as typeof createFilterQuery
        },
        $execute: () => {
          return query
        }
      }
    }
  }
})()


const query = createFilterQuery.equals('type', 'blog')
  .and().exists('new')
  .$execute()

console.log(query)
// type[equals]blog[and]new[exists]