Skip to content

Commit

Permalink
Add isKeyOf function for checking if a value is a key of an object
Browse files Browse the repository at this point in the history
  • Loading branch information
shtse8 committed Mar 14, 2024
1 parent 81adcc9 commit 0cefc6d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ const isNullish = (value) => {
const isNonNullish = (value) => {
return value !== null && value !== void 0;
};
const isKeyOf = (value, obj) => {
return value in obj;
};

const group = (array, getGroupId) => {
return array.reduce((acc, item) => {
Expand Down Expand Up @@ -943,4 +946,4 @@ const trim = (str, charsToTrim = " ") => {
return str.replace(regex, "");
};

export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNonNullish, isNullish, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isKeyOf, isNonNullish, isNullish, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
4 changes: 4 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ var radash = (function (exports) {
const isNonNullish = (value) => {
return value !== null && value !== void 0;
};
const isKeyOf = (value, obj) => {
return value in obj;
};

const group = (array, getGroupId) => {
return array.reduce((acc, item) => {
Expand Down Expand Up @@ -981,6 +984,7 @@ var radash = (function (exports) {
exports.isFloat = isFloat;
exports.isFunction = isFunction;
exports.isInt = isInt;
exports.isKeyOf = isKeyOf;
exports.isNonNullish = isNonNullish;
exports.isNullish = isNullish;
exports.isNumber = isNumber;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions docs/typed/is-key-of.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: isKeyOf
description: 'Checks if the given value is a key of the given object. It is useful for narrowing down the type of a value.'
group: Typed
---

## Basic usage

Pass the object and the value to check. It will return a boolean indicating if the value is a key of the object.

```ts
import { isKeyOf } from 'radash'

const obj = {
a: 1,
b: 2,
c: 3,
}
isKeyOf(obj, 'a') // true
isKeyOf(obj, 'd') // false
```
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export {
isFloat,
isFunction,
isInt,
isKeyOf,
isNonNullish,
isNullish,
isNumber,
Expand Down
13 changes: 13 additions & 0 deletions src/tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,17 @@ describe('typed module', () => {
assert.isTrue(_.isNonNullish(new Date()))
})
})

describe('isKeyOf function', () => {
test('returns true for keys of the object', () => {
const obj = { name: 'ray', age: 22 }
assert.isTrue(_.isKeyOf('name', obj))
assert.isTrue(_.isKeyOf('age', obj))
})
test('returns false for keys not in the object', () => {
const obj = { name: 'ray', age: 22 }
assert.isFalse(_.isKeyOf('height', obj))
assert.isFalse(_.isKeyOf('weight', obj))
})
})
})
13 changes: 13 additions & 0 deletions src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,16 @@ export const isNonNullish = <TType>(
): value is Exclude<TType, null | undefined> => {
return value !== null && value !== undefined
}

/**
* Checks if the given value is a key of the given object. It is useful for narrowing down the type of a value.
* @param value key to check
* @param obj object to check
* @returns true if the value is a key of the object
*/
export const isKeyOf = <TType extends Record<string | number | symbol, any>>(
value: string | number | symbol,
obj: TType
): value is keyof TType => {
return value in obj
}

0 comments on commit 0cefc6d

Please sign in to comment.