object-search-key
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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.

Package Sidebar

Install

npm i object-search-key

Weekly Downloads

6

Version

1.0.2

License

MIT

Unpacked Size

11.7 kB

Total Files

8

Last publish

Collaborators

  • imhele