{"id":22083763,"url":"https://github.com/unlyed/conditions-matcher","last_synced_at":"2025-07-24T16:31:04.977Z","repository":{"id":57169029,"uuid":"195385052","full_name":"UnlyEd/conditions-matcher","owner":"UnlyEd","description":"Compares a given context with a filter (a set of conditions) and resolves whether the context validates the filter. Strongly inspired by GraphQL filters.","archived":false,"fork":false,"pushed_at":"2020-03-09T23:43:00.000Z","size":373,"stargazers_count":4,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-13T09:47:31.299Z","etag":null,"topics":["algorithm","conditions","filter","graphql-filter","js","semver-convention","ts"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/UnlyEd.png","metadata":{"files":{"readme":"README-CONDITIONAL-OPERATORS.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-05T09:51:33.000Z","updated_at":"2024-03-20T18:35:08.000Z","dependencies_parsed_at":"2022-09-13T08:30:59.358Z","dependency_job_id":null,"html_url":"https://github.com/UnlyEd/conditions-matcher","commit_stats":null,"previous_names":["unlyed/prisma-algorithm-matcher"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UnlyEd%2Fconditions-matcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UnlyEd%2Fconditions-matcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UnlyEd%2Fconditions-matcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UnlyEd%2Fconditions-matcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UnlyEd","download_url":"https://codeload.github.com/UnlyEd/conditions-matcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227457126,"owners_count":17777944,"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":["algorithm","conditions","filter","graphql-filter","js","semver-convention","ts"],"created_at":"2024-12-01T00:17:37.330Z","updated_at":"2024-12-01T00:17:37.832Z","avatar_url":"https://github.com/UnlyEd.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e This document describes the list of all available conditional operators in [src/conditionalOperators/index.ts](./src/conditionalOperators/index.ts).\n \n# Flags\nFlags are a way to change the behavior of operators, per property in the context.\n\n- `i`: **Case insensitive**. By default, all conditional operators are case sensitive. Using this flag will perform a case insensitive comparison.\n\n## Example\n```js\nconst context = {\n    'name':'Unly',\n    'name__flags':['i']\n}\n```\n\n---\n# Conditional Operators\n\n\u003e Examples given here use the [`check`](./src/utils/check.ts) function, which is what's called internally by `contextMatcher` to check if the context matches the filter.\n\u003e `const check = (context: object, rule: string, value: any, options: IMap = defaultOptions)`\n\n## Equals\n\n### Definition\n\u003e This operator compares two elements and return true if they are strictly equal (for primitives/scalar types).\n\u003e For complex types (objects, arrays) the comparison is made based on the context (strictly equal as well). \n\u003e **Also, this operator is the default operator if none are explicitly defined.**\n\n### Aliases\n* equals\n* eq\n\n### Example\n```js\nconst context = {\n    'school':{\n        'name':'Unly',\n        'city':'lyon',\n        'city__flags':['i'], // The \"city\" attribute will be matched using \"case insensitive\" flag\n        'postal_code':69000,\n    }\n};\ncheck(context, 'school_name__eq', 'Unly'); // true\ncheck(context, 'school_name__equals', 'unly'); // false\ncheck(context, 'school_city__equals', 'LYON'); // true\ncheck(context, 'school_city__equals', 'lyon'); // true\ncheck(context, 'school_city__equals', 'lYOn'); // true\ncheck(context, 'school', {'name':'Unly','postal_code':69000,}); // false \ncheck(context, 'school', {'name':'Unly', 'city':'lyon', 'postal_code':69000,}); // true \n```\n\n## Not Equals\n\n### Definition\n\u003e Check if two objects are strictly different.\n\n### Aliases\n* notEquals\n* ne\n\n### Example\n```js\nconst context = {\n    'school':{\n        'name':'Unly',\n        'city':'lyon',\n        'city__flags':['i'],\n        'postal_code':69000,\n    }\n};\ncheck(context, 'school_name__ne', 'Unly'); // false\ncheck(context, 'school_name__notEquals', 'unly'); // true\ncheck(context, 'school_city__notEquals', 'LYON'); // false\ncheck(context, 'school_city__notEquals', 'lyon'); // false\ncheck(context, 'school_city__notEquals', 'lYOn'); // false\ncheck(context, 'school__ne', {'name':'Unly','postal_code':69000,}); // true\ncheck(context, 'school', {'name':'Unly', 'city':'lyon', 'postal_code':69000,}); // false \n```\n\n## Starts With\n\n### Definition\n\u003e Check if a string start with another.\n\n### Aliases\n* startsWith\n* sw\n\n### Example\n```js\nconst context = {\n    'school':{\n        'name':'Unly',\n        'city':'lyon',\n        'city__flags':['i'],\n        'postal_code':69000,\n    }\n};\ncheck(context, 'school_name__sw', 'Un'); // true\ncheck(context, 'school_name__sw', 'un'); // false\ncheck(context, 'school_city__sw', 'LY'); // true\ncheck(context, 'school_city__sw', 'ly'); // true\n```\n\n## Ends With\n\n### Definition\n\u003e Check if a string finish by another.\n\n### Aliases\n* endsWith\n* ew\n\n### Example\n\n```js\nconst context = {\n    'school':{\n        'name':'Unly',\n        'city':'lyon',\n        'city__flags':['i'],\n        'postal_code':69000,\n    }\n};\n\ncheck(context, 'school_name__sw', 'ly'); // true\ncheck(context, 'school_name__sw', 'LY'); // false\ncheck(context, 'school_city__sw', 'ON'); // true\ncheck(context, 'school_city__sw', 'on'); // true\n```\n\n## Contains\n\n### Definition\n\u003e String: Checks if the string contains a given string.\n\u003e Array: Checks if the array contains a specific element.\n\u003e Object: Checks if the object contains a specific element.\n\n ### Aliases\n* contains\n* inside\n* in\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    \"location\":{\n        \"city\":\"lyon\",\n        \"post_code\":69000\n    },\n    \"campus\":[42, \"Unly\"],\n    \"campus__flags\":['i']\n};\ncheck(context, 'school_name__in', 'aul'); // true\ncheck(context, 'school_location__in', {'city':\"lyon\"}); // true\ncheck(context, 'school_campus__in', 42); // true\ncheck(context, 'school_campus__in', 'Unly'); // true\ncheck(context, 'school_campus__in', 'unly'); // true\n```\n\n## Not Contains\n\n### Definition\n\u003e The opposite of \"contains\".\n\u003e String: Checks if the string does not contain a given string.\n\u003e Array: Checks if the array does not contain a specific element.\n\u003e Object: Checks if the object does not contain a specific element.\n\n### Aliases\n* notContains\n* notIncludes\n* nin\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    \"location\":{\n        \"city\":\"lyon\",\n        \"post_code\":69000\n    },\n    \"campus\":[42, \"Unly\"],\n    \"campus__flags\":['i']\n};\ncheck(context, 'school_name__nin', 'aul'); // false\ncheck(context, 'school_location__nin', {'city':\"lyon\"}); // false\ncheck(context, 'school_campus__nin', 42); // false\ncheck(context, 'school_campus__nin', 'Unly'); // false\ncheck(context, 'school_campus__nin', 'unly'); // false \n```\n\n## Greater Than\n\n### Definition\n\u003e Check if a value is greater than another.\n\n### Aliases\n* greaterThan\n* gt\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    'GPA':3,\n};\ncheck(context, 'GPA__gt', 2); // true\ncheck(context, 'GPA__gt', '2'); // true TODO check not sure behaviour\n```\n\n## Greater Than or Equals\n\n## definition \n\u003e Check if a value is greater or equal than another.\n\n\n### Aliases\n* greaterThanEquals\n* gte\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    'GPA':3,\n};\ncheck(context, 'GPA__gte', 3); // true\ncheck(context, 'GPA__gte', '3'); // true TODO check not sure behaviour\n```\n\n## Less Than\n\n### Definition\n\u003e Check if a value is less than another.\n\n\n### Aliases\n* lessThan\n* lt\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    'GPA':3,\n};\ncheck(context, 'GPA__lt', 4); // true\ncheck(context, 'GPA__lt', '4'); // true TODO check not sure behaviour\n```\n## Less Than or Equals\n\n### Definition \n\u003e Check if a value is less or equal than another.\n\n### Aliases\n* lessThanEquals\n* lte\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    'GPA':3,\n};\ncheck(context, 'GPA__lte', 3); // true\ncheck(context, 'GPA__lte', '4'); // true\n```\n## Is Inside\n\n### Definition\n\u003e String: Checks if the string is inside a given string.\n\u003e Array: Checks if a specific element is inside an array.\n\u003e Object: Checks if a specific element is inside an  object.\n\n ### Aliases\n* isInside\n* isIn\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    \"location\":{\n        \"city\":\"lyon\",\n        \"post_code\":69000\n    },\n    \"campus\":[42, \"Unly\"],\n    \"campus__flags\":['i']\n};\ncheck(context, 'school_name__isIn', [\"Paul\", \"jake\"]); // true\n```\n\n## Is Not Inside\n\n### Definition\n\u003e The opposite of \"inside\".\n\u003e String: Checks if the string is inside a given string.\n\u003e Array: Checks if a specific element is inside an array.\n\u003e Object: Checks if a specific element is inside an  object.\n\n### Aliases\n* isNotInside\n* isnIn\n\n### Example\n\n```js\nconst context = {\n    \"name\":\"Paul\",\n    \"location\":{\n        \"city\":\"lyon\",\n        \"post_code\":69000\n    },\n    \"campus\":[42, \"Unly\"],\n    \"campus__flags\":['i']\n};\ncheck(context, 'school_name__isnIn', [\"Paul\",\"Jake\"]); // false\n```\n\n---\n# Conditional operators for arrays (batch)\n\n\u003e It is possible to mix both a conditional operator (as seen previously), with a \"batch\" operator to check a condition for a set of data.\n\n## Every\n\n### Definition\n\u003e Batch operator that checks if the condition is matched by all items in the data set.\n\u003e If any item fails to validate the condition, then it's a mismatch.\n\n### Aliases\n* every\n\n### Example\n\n```js\nconst context = {\n  'students':[\n    {'name':'Roger', 'GPA':3, \"promotion\":2021},\n    {'name':'Paul', 'GPA':1, \"promotion\":2021},\n    {'name':'Robert', 'GPA':2, \"promotion\":2021}\n  ]\n}\ncheck(context, 'students_promotion__every_eq', 2021); // true\ncheck(context, 'students_promotion__every_gte', 1); // true\ncheck(context, 'students_promotion__every_lte', 2); // false\n```\n\n\n## Some\n\n#### Definition\n\u003e Batch operator that checks if the condition is matched by any of the items in the data set.\n\u003e If any item succeed to validate the condition, then it's a match.\n\n### Aliases\n* some\n\n### Example\n\n```js\nconst context = {\n  'students':[\n    {'name':'Roger', 'GPA':3, \"promotion\":2021},\n    {'name':'Paul', 'GPA':1, \"promotion\":2021},\n    {'name':'Robert', 'name__flags': ['i'], 'GPA':2, \"promotion\":2021}\n  ]\n}\ncheck(context, 'students_name__some_eq', 'robert'); // true\ncheck(context, 'students_name__some_eq', 'roger'); // false\ncheck(context, 'students_promotion__some_eq', 2); // true\ncheck(context, 'students_promotion__some_lte', 1); // true\n```\n\n## None\n\n#### Definition\n\u003e Batch operator that checks if the condition is matched by none of the items in the data set.\n\u003e If any item succeed to validate the condition, then it's a mismatch.\n\n### Aliases\n* none\n\n### Example\n\n```js\nconst context = {\n  'students':[\n    {'name':'Roger', 'GPA':3, \"promotion\":2021},\n    {'name':'Paul', 'GPA':1, \"promotion\":2021},\n    {'name':'Robert', 'name__flags': ['i'], 'GPA':2, \"promotion\":2021}\n  ]\n}\ncheck(context, 'students_GPA__none_gte', 4); // true\ncheck(context, 'students_GPA__none_gte', 3); // false\ncheck(context, 'students_name__none_eq', 'Louis'); // true\ncheck(context, 'students_name__none_sw', 'Rog'); // false\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funlyed%2Fconditions-matcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funlyed%2Fconditions-matcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funlyed%2Fconditions-matcher/lists"}