1.0.2 • Published 5 years ago

object-search-key v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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.