Skip to content

Commit

Permalink
feat-value-has (#160)
Browse files Browse the repository at this point in the history
* feat: add `valueHas` check if a value has a value

* chore: remove unused import

* doc(model): regenerate
  • Loading branch information
snickbit authored Mar 11, 2024
1 parent 40860cf commit f883de9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/model/docs/classes/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Create a simple object model
- [toJSON](Model.md#tojson)
- [toString](Model.md#tostring)
- [validate](Model.md#validate)
- [valueHas](Model.md#valuehas)

## Constructors

Expand Down Expand Up @@ -647,3 +648,22 @@ If the model is invalid and strict mode is enabled
#### Returns

`Promise`<``true`` \| [`ModelErrors`](../README.md#modelerrors)\>

___

### valueHas

**valueHas**(`key`, `value`): `boolean`

Tests if value exists in paths value (i.e. in an array or object)

#### Parameters

| Name | Type |
| :------ | :------ |
| `key` | [`ModelKey`](../README.md#modelkey) |
| `value` | `any` |

#### Returns

`boolean`
15 changes: 14 additions & 1 deletion packages/model/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
uuid,
VariableType
} from '@snickbit/utilities'
import objectPath, {ensureExists, ObjectPathBound} from 'object-path'
import objectPath, {ObjectPathBound} from 'object-path'

export type ModelId = number | string | undefined

Expand Down Expand Up @@ -397,6 +397,19 @@ export class Model<T extends object = any, D = Partial<T>> {
return this.data.has(this.checkKey(key))
}

/**
* Tests if value exists in paths value (i.e. in an array or object)
*/
valueHas(key: ModelKey, value: ModelValue): boolean {
const data = this.get(key)
if (isObject(data)) {
return Object.values(data).includes(value)
} else if (isArray(data)) {
return data.includes(value)
}
return false
}

/**
* Get the keys of the data
*/
Expand Down

0 comments on commit f883de9

Please sign in to comment.