2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-18 18:29:29 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2021-09-01 08:12:44 +00:00
|
|
|
/**
|
2024-05-06 07:51:20 +00:00
|
|
|
* Returns true if the given value is part of the given object, otherwise it
|
|
|
|
* returns false.
|
2021-09-01 08:12:44 +00:00
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* Note: this doesn't work with non-primitive values. For example,
|
|
|
|
* `includesValue({x: {}}, {})` returns false.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the values in the input record.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param record The record to check for the given value.
|
|
|
|
* @param value The value to check for in the record.
|
|
|
|
*
|
|
|
|
* @returns `true` if the value is part of the record, otherwise `false`.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2021-09-01 08:12:44 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { includesValue } from "@std/collections/includes-value";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-09-01 08:12:44 +00:00
|
|
|
*
|
|
|
|
* const input = {
|
|
|
|
* first: 33,
|
|
|
|
* second: 34,
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* assertEquals(includesValue(input, 34), true);
|
2022-08-01 03:59:07 +00:00
|
|
|
* ```
|
2021-09-01 08:12:44 +00:00
|
|
|
*/
|
|
|
|
export function includesValue<T>(
|
|
|
|
record: Readonly<Record<string, T>>,
|
|
|
|
value: T,
|
|
|
|
): boolean {
|
|
|
|
for (const i in record) {
|
2021-09-13 04:59:55 +00:00
|
|
|
if (
|
|
|
|
Object.hasOwn(record, i) &&
|
|
|
|
(record[i] === value || Number.isNaN(value) && Number.isNaN(record[i]))
|
|
|
|
) {
|
2021-09-01 08:12:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|