{"id":23081253,"url":"https://github.com/lagden/condition","last_synced_at":"2025-04-03T14:15:21.917Z","repository":{"id":139377843,"uuid":"369675073","full_name":"lagden/condition","owner":"lagden","description":"Create conditional to validate data","archived":false,"fork":false,"pushed_at":"2025-04-02T04:58:04.000Z","size":521,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T05:01:37.574Z","etag":null,"topics":["conditional","conditions","javascript","library","object","validation"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@tadashi/condition","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lagden.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-21T23:28:28.000Z","updated_at":"2025-04-02T04:57:47.000Z","dependencies_parsed_at":"2024-04-29T23:07:49.759Z","dependency_job_id":null,"html_url":"https://github.com/lagden/condition","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lagden%2Fcondition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lagden%2Fcondition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lagden%2Fcondition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lagden%2Fcondition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lagden","download_url":"https://codeload.github.com/lagden/condition/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247014521,"owners_count":20869376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditional","conditions","javascript","library","object","validation"],"created_at":"2024-12-16T13:50:32.996Z","updated_at":"2025-04-03T14:15:21.907Z","avatar_url":"https://github.com/lagden.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Condition\n\n[![NPM version][npm-img]][npm] [![Node.js CI][ci-img]][ci] [![Coverage Status][coveralls-img]][coveralls]\n\n[npm-img]: https://img.shields.io/npm/v/@tadashi/condition.svg\n[npm]: https://www.npmjs.com/package/@tadashi/condition\n[ci-img]: https://github.com/lagden/condition/actions/workflows/nodejs.yml/badge.svg\n[ci]: https://github.com/lagden/condition/actions/workflows/nodejs.yml\n[coveralls-img]: https://coveralls.io/repos/github/lagden/condition/badge.svg?branch=main\n[coveralls]: https://coveralls.io/github/lagden/condition?branch=main\n\nCreate conditional to validate data.\n\n## Install\n\n```\n$ npm i @tadashi/condition\n```\n\n## Operators\n\n| Operator     | Alias            | Symbol | Description                                                  |\n| ------------ | ---------------- | :----: | ------------------------------------------------------------ |\n| eq           | -                |  ===   | Two values are equal                                         |\n| ne           | -                |  !==   | Two values are not equal                                     |\n| gt           | -                |   \u003e    | The first value is greater than the second value             |\n| ge           | -                |   \u003e=   | The first value is greater than or equal to the second value |\n| lt           | -                |   \u003c    | The first value is less than the second value                |\n| le           | -                |   \u003c=   | The first value is less than or equal to the second value    |\n| intersection | -                |   ∩    | There is an intersection between two arrays                  |\n| difference   | -                |   ∆    | There is a difference between two arrays                     |\n| arrayEquals  | -                |   =    | Two arrays are equal                                         |\n| belongs      | `has` `includes` |   ∈    | The value belongs belongs to array                           |\n| regex        | -                |   -    | The value matches a regular expression pattern               |\n| length       | -                |   -    | The length of a value satisfies a comparison                 |\n| assigned     | -                |   -    | The value was assigned                                       |\n\n## Schema\n\n```yaml\nCondition:\n    type: object\n    properties:\n        join_operator:\n            type: string\n            enum: [and, or]\n            default: and\n        args:\n            type: array\n            items:\n                oneOf:\n                    - $ref: '#/definitions/ConditionArgument'\n                    - $ref: '#/definitions/Condition'\n    required:\n        - join_operator\n        - args\n\nConditionArgument:\n    type: object\n    properties:\n        field:\n            type: string\n        operator:\n            type: string\n        value:\n            type: any\n            anyOf:\n                - type: boolean\n                - type: string\n                - type: number\n                - type: array\n        flag:\n            type: string\n        compare:\n            type: string\n            enum: [eq, ne, gt, ge, greater, lt, le, less]\n        not:\n            type: boolean\n            default: false\n    required:\n        - field\n        - operator\n        - value\n\ndefinitions:\n    Condition:\n        $ref: '#/Condition'\n    ConditionArgument:\n        $ref: '#/ConditionArgument'\n```\n\n## Usage\n\nSee example below.\n\n```js\nimport condition, { registerOperator } from '@tadashi/condition'\n\nregisterOperator('legalAge', (params) =\u003e {\n\tconst {\n\t\tfieldValue,\n\t\tvalue: conditionValue,\n\t\t// not,\n\t\t// flag,\n\t\t// compare,\n\t} = params\n\n\ttry {\n\t\tconst today = new Date()\n\t\tconst externalDate = new Date(fieldValue)\n\t\tlet age = today.getFullYear() - externalDate.getFullYear()\n\t\tconst monthDiff = today.getMonth() - externalDate.getMonth()\n\n\t\tif (monthDiff \u003c 0 || (monthDiff === 0 \u0026\u0026 today.getDate() \u003c externalDate.getDate())) {\n\t\t\tage--\n\t\t}\n\n\t\treturn age \u003e= conditionValue\n\t} catch {\n\t\treturn false\n\t}\n})\n\nconst data = {\n\tuser: {\n\t\tname: 'Yumi',\n\t\tbirthday: '1990-12-31',\n\t\tgender: 'F',\n\t\tissues: 65,\n\t},\n\tcity: 'São Paulo',\n\tcountry: 'Brazil',\n\tphone: '(11) 988889999',\n\thasCar: true,\n\tcolors: ['red', 'blue'],\n}\n\nconst conditions = [\n\t{\n\t\tjoin_operator: 'and',\n\t\targs: [\n\t\t\t{\n\t\t\t\tfield: 'user.gender',\n\t\t\t\toperator: 'eq',\n\t\t\t\tvalue: 'F',\n\t\t\t},\n\t\t\t{\n\t\t\t\tfield: 'user.birthday',\n\t\t\t\toperator: 'legalAge',\n\t\t\t\tvalue: 21,\n\t\t\t},\n\t\t\t{\n\t\t\t\tfield: 'user.issues',\n\t\t\t\toperator: 'gt',\n\t\t\t\tvalue: 51,\n\t\t\t},\n\t\t\t{\n\t\t\t\tfield: 'phone',\n\t\t\t\toperator: 'regex',\n\t\t\t\tvalue: /\\(\\d{2}\\)\\s(\\d{8,9})/i,\n\t\t\t},\n\t\t\t{\n\t\t\t\tjoin_operator: 'or',\n\t\t\t\targs: [\n\t\t\t\t\t{\n\t\t\t\t\t\tfield: 'city',\n\t\t\t\t\t\toperator: 'assigned',\n\t\t\t\t\t\tvalue: false,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tfield: 'country',\n\t\t\t\t\t\toperator: 'intersection',\n\t\t\t\t\t\tvalue: ['Japan', 'Brazil'],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\t{\n\t\t\t\tfield: 'hasCar',\n\t\t\t\toperator: 'eq',\n\t\t\t\tvalue: true,\n\t\t\t},\n\t\t\t{\n\t\t\t\tfield: 'colors',\n\t\t\t\toperator: 'intersection',\n\t\t\t\tvalue: ['blue', 'green', 123],\n\t\t\t},\n\t\t\t{\n\t\t\t\tfield: 'colors',\n\t\t\t\toperator: 'arrayEquals',\n\t\t\t\tvalue: ['red', 'blue'],\n\t\t\t},\n\t\t],\n\t},\n]\n\nconst isValid = condition(conditions)\nisValid(data) // =\u003e true\n```\n\n## Buy Me a Coffee\n\nBTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4\n\n## License\n\nMIT © [Thiago Lagden](https://github.com/lagden)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flagden%2Fcondition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flagden%2Fcondition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flagden%2Fcondition/lists"}