forked from denoland/deno_std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
includes_value.ts
36 lines (34 loc) · 947 Bytes
/
includes_value.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* If the given value is part of the given object it returns true, otherwise it
* returns false. Doesn't work with non-primitive values: includesValue({x: {}},
* {}) returns false.
*
* @example
* ```ts
* import { includesValue } from "https://deno.land/std@$STD_VERSION/collections/includes_value.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
*
* const input = {
* first: 33,
* second: 34,
* };
*
* assertEquals(includesValue(input, 34), true);
* ```
*/
export function includesValue<T>(
record: Readonly<Record<string, T>>,
value: T,
): boolean {
for (const i in record) {
if (
Object.hasOwn(record, i) &&
(record[i] === value || Number.isNaN(value) && Number.isNaN(record[i]))
) {
return true;
}
}
return false;
}