https://github.com/d8corp/condition-handler
The handler of conditions
https://github.com/d8corp/condition-handler
Last synced: about 1 year ago
JSON representation
The handler of conditions
- Host: GitHub
- URL: https://github.com/d8corp/condition-handler
- Owner: d8corp
- License: mit
- Created: 2020-08-10T08:13:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-10T22:27:45.000Z (almost 6 years ago)
- Last Synced: 2025-03-30T21:01:49.408Z (over 1 year ago)
- Language: TypeScript
- Size: 56.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# condition-handler
[](https://www.npmjs.com/package/condition-handler)
[](https://github.com/d8corp/condition-handler/blob/master/lib/conditionHandler.min.js)
[](https://www.npmjs.com/package/condition-handler)
[](https://github.com/d8corp/condition-handler/blob/master/LICENSE)
The handler of conditions.
## Installation
```bash
npm i condition-handler
# or
yarn add condition-handler
```
Or you can use [minified file](https://github.com/d8corp/condition-handler/blob/master/lib/conditionHandler.min.js).
```html
console.log(conditionHandler('!', true))
```
## Using
You can use any operators from the list
`!` `&&` `||` `===` `==` `!==` `!=` `>` `>=` `<` `<=` `(` `)`
It works the same as `if` operator from JavaScript.
```typescript
conditionHandler([1]) // 1 === 1
conditionHandler([1, '&&', 2]) // 1 && 2 === 2
conditionHandler([1, '&&', 0]) // 1 && 0 === 0
conditionHandler([1, '||', 0]) // 1 || 0 === 1
conditionHandler(['!', 1, '||', 0]) // !1 || 0 === 0
conditionHandler([1, '>', 2]) // 1 > 2 === false
conditionHandler([2, '>=', 1]) // 2 >= 1 === true
```
You can use a plugin to handle non-operator argument.
```typescript
const obj = {
test1: true,
test2: false,
test: {
field1: 1,
field2: 0
}
}
function plugin (condition) {
if (typeof condition === 'string') {
const fields = condition.split('.')
condition = obj
for (let i = 0; i < fields.length; i++) {
condition = condition[fields[i]]
}
}
return condition
}
conditionHandler(['test1', '&&', 'test.field2'], plugin)
// obj.test1 && obj.test.field2 === 0
```
## Issues
If you find a bug, please file an issue on [GitHub](https://github.com/d8corp/condition-handler/issues)
[](https://github.com/d8corp/condition-handler/issues)
[](https://github.com/d8corp/condition-handler)
[](https://github.com/d8corp/condition-handler)