Skip to content
This repository was archived by the owner on Feb 16, 2020. It is now read-only.

alitajs/object-search-key

Repository files navigation

object-search-key

Alita NPM version NPM downloads Build Status Coverage Status License

Match the appropriate search keywords according to the key value definition of the object. Use yaml-joi as validator.

Install

$ npm install object-search-key
or
$ yarn add object-search-key

Example

import { getObjectSearchKeys } from 'object-search-key';

const AccountModelDefine = `
type: object
isSchema: true
limitation:
  - keys:
      id:
        type: number
        isSchema: true
        limitation:
          - min: 0
          - max: 999999999
          - integer: []
      name:
        type: string
        isSchema: true
        limitation:
          - max: 32
      age:
        type: number
        isSchema: true
        limitation:
          - min: 0
          - integer: []
      locked:
        type: string
        isSchema: true
        limitation:
          - only: [Y, N]
`;

const searchInput = '-123 hele xiaohuoni 24 1.2 N'.split(/(?:\s*)/);
const search = getObjectSearchKeys(AccountModelDefine, searchInput);
/**
 * Result:
 * {
 *   id: [24],
 *   name: ['-123', 'hele', 'xiaohuoni', '24', '1.2', 'N'],
 *   age: [24],
 *   locked: ['N'],
 * }
 */
const or = Object.entries(search).reduce((prev, [key, value]) => {
  return prev.concat(value.map(searchKey => ({ [key]: searchKey })));
}, []);

sequelize.Account.findAndCountAll({ where: { [Op.or]: or } });

/**
 * [
 *   { id: '123' },
 *   { id: '24' },
 *   { name: '123' },
 *   { name: 'hele' },
 *   { name: 'xiaohuoni' },
 *   { name: '24' },
 *   { name: '1.2' },
 *   { name: 'N' },
 *   { age: '123' },
 *   { age: '24' },
 *   { locked: 'N' },
 * ]
 */

Get more at cases.yml.

About

Match the appropriate search keywords according to the key value definition of the object.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published