Skip to content

Commit 0b1503d

Browse files
chore: bring backward-compatible isObject
1 parent 8460756 commit 0b1503d

File tree

5 files changed

+153
-32
lines changed

5 files changed

+153
-32
lines changed

README.md

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,28 +742,79 @@ isNumberType(NUMBER_NEW_INSTANCE); // false
742742

743743
### isObject
744744

745-
![update][update]
746-
747-
* Removed `key` parameter. Use [`isObjectKeyIn`](#isobjectkin) instead.
748-
749-
Use `isObject()` or `is.object()` to check if **any** `value` is an `object` of a generic `Obj` type and [`Object`][object] instance.
745+
Use `isObject()` or `is.object()` to check if **any** `value` is an `object` of a generic `Obj` type and [`Object`][object] instance with the possibility of containing the `key`.
750746

751747
```typescript
752-
const isObject: IsObject = <Obj = object>(value: any, callback: ResultCallback = resultCallback): value is Obj =>
753-
callback(typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true, value);
748+
const isObject: IsObject = <Obj = object>(value: any, key?: Key): value is Obj =>
749+
(typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true)
750+
? isKey(key)
751+
? key in value
752+
: true
753+
: false;
754754
```
755755

756756
| Parameter | Type | Description |
757757
| :-------- | :-----------: | :---------- |
758758
| value | `any` | Any `value` to check |
759-
| callback | [`ResultCallback`][resultcallback]=[`resultCallback`][callback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
759+
| key? | [`Key`][key] | Property name to find in the `value` |
760760

761761
The **return value** is a `boolean` indicating whether or not the `value` is an `object`.
762762

763763
[Example usage on playground][is-object] | [How to detect an `object` type][detect-object]
764764

765765
```typescript
766766
// Example usage
767+
const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
768+
const SYMBOL_STRING: unique symbol = Symbol(STRING);
769+
770+
/**
771+
* typeof === 'number'
772+
* instanceof Function === false
773+
* instanceof Number === false
774+
* instanceof Object === false
775+
*/
776+
const NUMBER: any = 10304050;
777+
778+
/**
779+
* typeof === 'number'
780+
* instanceof Function === false
781+
* instanceof Number === false
782+
* instanceof Object === false
783+
*/
784+
const NUMBER_INSTANCE: any = Number(NUMBER);
785+
786+
/**
787+
* typeof === 'number'
788+
* instanceof Function === false
789+
* instanceof Number === true
790+
* instanceof Object === true
791+
*/
792+
const NUMBER_NEW_INSTANCE: any = new Number(NUMBER);
793+
794+
/**
795+
* typeof === 'string'
796+
* instanceof Function === false
797+
* instanceof Object === false
798+
* instanceof String === false
799+
*/
800+
const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz';
801+
802+
/**
803+
* typeof === 'string'
804+
* instanceof Function === false
805+
* instanceof Object === false
806+
* instanceof String === false
807+
*/
808+
const STRING_INSTANCE: any = String(STRING);
809+
810+
/**
811+
* typeof === 'string'
812+
* instanceof Function === false
813+
* instanceof Object === true
814+
* instanceof String === true
815+
*/
816+
const STRING_NEW_INSTANCE: any = new String(STRING);
817+
767818
const OBJECT_ONE: ObjectOne = {
768819
'key as string': true,
769820
1030405027: 'key is number',
@@ -776,6 +827,14 @@ const OBJECT_ONE: ObjectOne = {
776827
};
777828

778829
isObject(OBJECT_ONE); // true
830+
isObject(OBJECT_ONE, 'key as string'); // true
831+
isObject(OBJECT_ONE, STRING); // true
832+
isObject(OBJECT_ONE, STRING_NEW_INSTANCE); // true
833+
isObject(OBJECT_ONE, 1030405027); // true
834+
isObject(OBJECT_ONE, NUMBER); // true
835+
isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE); // true
836+
isObject(OBJECT_ONE, SYMBOL_NUMBER); // true
837+
isObject(OBJECT_ONE, SYMBOL_STRING); // true
779838

780839
```
781840

packages/type/README.md

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,28 +742,79 @@ isNumberType(NUMBER_NEW_INSTANCE); // false
742742

743743
### isObject
744744

745-
![update][update]
746-
747-
* Removed `key` parameter. Use [`isObjectKeyIn`](#isobjectkin) instead.
748-
749-
Use `isObject()` or `is.object()` to check if **any** `value` is an `object` of a generic `Obj` type and [`Object`][object] instance.
745+
Use `isObject()` or `is.object()` to check if **any** `value` is an `object` of a generic `Obj` type and [`Object`][object] instance with the possibility of containing the `key`.
750746

751747
```typescript
752-
const isObject: IsObject = <Obj = object>(value: any, callback: ResultCallback = resultCallback): value is Obj =>
753-
callback(typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true, value);
748+
const isObject: IsObject = <Obj = object>(value: any, key?: Key): value is Obj =>
749+
(typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true)
750+
? isKey(key)
751+
? key in value
752+
: true
753+
: false;
754754
```
755755

756756
| Parameter | Type | Description |
757757
| :-------- | :-----------: | :---------- |
758758
| value | `any` | Any `value` to check |
759-
| callback | [`ResultCallback`][resultcallback]=[`resultCallback`][callback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
759+
| key? | [`Key`][key] | Property name to find in the `value` |
760760

761761
The **return value** is a `boolean` indicating whether or not the `value` is an `object`.
762762

763763
[Example usage on playground][is-object] | [How to detect an `object` type][detect-object]
764764

765765
```typescript
766766
// Example usage
767+
const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
768+
const SYMBOL_STRING: unique symbol = Symbol(STRING);
769+
770+
/**
771+
* typeof === 'number'
772+
* instanceof Function === false
773+
* instanceof Number === false
774+
* instanceof Object === false
775+
*/
776+
const NUMBER: any = 10304050;
777+
778+
/**
779+
* typeof === 'number'
780+
* instanceof Function === false
781+
* instanceof Number === false
782+
* instanceof Object === false
783+
*/
784+
const NUMBER_INSTANCE: any = Number(NUMBER);
785+
786+
/**
787+
* typeof === 'number'
788+
* instanceof Function === false
789+
* instanceof Number === true
790+
* instanceof Object === true
791+
*/
792+
const NUMBER_NEW_INSTANCE: any = new Number(NUMBER);
793+
794+
/**
795+
* typeof === 'string'
796+
* instanceof Function === false
797+
* instanceof Object === false
798+
* instanceof String === false
799+
*/
800+
const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz';
801+
802+
/**
803+
* typeof === 'string'
804+
* instanceof Function === false
805+
* instanceof Object === false
806+
* instanceof String === false
807+
*/
808+
const STRING_INSTANCE: any = String(STRING);
809+
810+
/**
811+
* typeof === 'string'
812+
* instanceof Function === false
813+
* instanceof Object === true
814+
* instanceof String === true
815+
*/
816+
const STRING_NEW_INSTANCE: any = new String(STRING);
817+
767818
const OBJECT_ONE: ObjectOne = {
768819
'key as string': true,
769820
1030405027: 'key is number',
@@ -776,6 +827,14 @@ const OBJECT_ONE: ObjectOne = {
776827
};
777828

778829
isObject(OBJECT_ONE); // true
830+
isObject(OBJECT_ONE, 'key as string'); // true
831+
isObject(OBJECT_ONE, STRING); // true
832+
isObject(OBJECT_ONE, STRING_NEW_INSTANCE); // true
833+
isObject(OBJECT_ONE, 1030405027); // true
834+
isObject(OBJECT_ONE, NUMBER); // true
835+
isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE); // true
836+
isObject(OBJECT_ONE, SYMBOL_NUMBER); // true
837+
isObject(OBJECT_ONE, SYMBOL_STRING); // true
779838

780839
```
781840

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// Function.
2-
import { resultCallback } from '../../lib/result-callback.func';
2+
import { isKey } from './is-key.func';
33
import { typeOf } from '../../lib/type-of.func';
44
// Type.
55
import { IsObject } from '../type/is-object.type';
6-
import { ResultCallback } from '../../type/result-callback.type';
6+
import { Key } from '../../type/key.type';
77
/**
8-
* Checks if any `value` is an `object` of a generic `Obj` type and `Object` instance.
8+
* Checks if any `value` is an `object` of a generic `Obj` type and `Object` instance with the possibility of containing the `key`.
99
* @param value Any `value` to check.
10-
* @param callback `ResultCallback` function to handle result before returns.
11-
* @callback `resultCallback`.
10+
* @param key Property name to find in the `value`.
1211
* @returns A `boolean` indicating whether or not the `value` is an `object`.
1312
*/
14-
export const isObject: IsObject = <Obj = object>(value: any, callback: ResultCallback = resultCallback): value is Obj =>
15-
callback(typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true, value);
13+
export const isObject: IsObject = <Obj = object>(value: any, key?: Key): value is Obj =>
14+
(typeOf(value) === 'object' && typeof value === 'object' && value instanceof Object === true)
15+
? isKey(key)
16+
? key in value
17+
: true
18+
: false;

packages/type/src/is/test/is-object.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ describe(isObject.name, () => {
2222

2323
// Checks ...
2424
describe(`checks`, () => {
25-
it('callback', () => {
26-
isObject(OBJECT_ONE, (result: boolean, value: ObjectOne) => {
27-
expect(result).toBe(TRUE);
28-
expect(value).toEqual(OBJECT_ONE);
29-
return result;
30-
});
31-
});
25+
// it('callback', () => {
26+
// isObject(OBJECT_ONE, (result: boolean, value: ObjectOne) => {
27+
// expect(result).toBe(TRUE);
28+
// expect(value).toEqual(OBJECT_ONE);
29+
// return result;
30+
// });
31+
// });
3232

3333
// ... arrays.
3434
describe(`array`, () => {});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { ResultCallback } from '../../type/result-callback.type';
2-
export type IsObject = <Obj = object>(value: any, callback?: ResultCallback) => value is Obj;
1+
import { Key } from '../../type/key.type';
2+
export type IsObject = <Obj = object>(value: any, key?: Key) => value is Obj;

0 commit comments

Comments
 (0)