Skip to content

Commit fd026ec

Browse files
authored
feat(functionsIn): Add functionsIn to compat/object (#1077)
1 parent 17c236b commit fd026ec

File tree

8 files changed

+412
-0
lines changed

8 files changed

+412
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { bench, describe } from 'vitest';
2+
import { functionsIn as functionsInToolkitCompat_ } from 'es-toolkit/compat';
3+
import { functionsIn as functionsInLodash_ } from 'lodash';
4+
5+
const functionsInToolkitCompat = functionsInToolkitCompat_;
6+
const functionsInLodash = functionsInLodash_;
7+
8+
describe('functionsIn', () => {
9+
function Foo() {
10+
this.a = function () {
11+
return 'a';
12+
};
13+
this.b = function () {
14+
return 'b';
15+
};
16+
}
17+
18+
Foo.prototype.c = function () {
19+
return 'c';
20+
};
21+
22+
const foo = new Foo();
23+
const plainObject = {
24+
a: function () {
25+
return 'a';
26+
},
27+
b: function () {
28+
return 'b';
29+
},
30+
};
31+
32+
bench('es-toolkit/compat/functionsIn', () => {
33+
functionsInToolkitCompat(foo);
34+
functionsInToolkitCompat(plainObject);
35+
functionsInToolkitCompat(null);
36+
functionsInToolkitCompat(undefined);
37+
});
38+
39+
bench('lodash/functionsIn', () => {
40+
functionsInLodash(foo);
41+
functionsInLodash(plainObject);
42+
functionsInLodash(null);
43+
functionsInLodash(undefined);
44+
});
45+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# functionsIn
2+
3+
::: info
4+
この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。
5+
6+
`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](mdc:../../../compatibility.md)します。
7+
:::
8+
9+
オブジェクトの独自プロパティと継承された列挙可能なプロパティから、関数プロパティ名の配列を生成します。
10+
11+
## インターフェース
12+
13+
```typescript
14+
function functionsIn(object: any): string[];
15+
```
16+
17+
### パラメータ
18+
19+
- `object`: 検査するオブジェクト。
20+
21+
### 戻り値
22+
23+
(`string[]`): オブジェクトの独自プロパティと継承された列挙可能なプロパティから、関数プロパティ名の配列を返します。
24+
25+
##
26+
27+
```typescript
28+
import { functionsIn } from 'es-toolkit/compat';
29+
30+
function Foo() {
31+
this.a = function () {
32+
return 'a';
33+
};
34+
this.b = function () {
35+
return 'b';
36+
};
37+
}
38+
39+
Foo.prototype.c = function () {
40+
return 'c';
41+
};
42+
43+
// 継承された関数を含む関数プロパティ名を取得
44+
functionsIn(new Foo());
45+
// => ['a', 'b', 'c']
46+
47+
// プレーンオブジェクトでも動作
48+
const object = {
49+
a: function () {
50+
return 'a';
51+
},
52+
b: function () {
53+
return 'b';
54+
},
55+
};
56+
57+
functionsIn(object);
58+
// => ['a', 'b']
59+
60+
// オブジェクト以外の場合は空配列を返す
61+
functionsIn(null);
62+
// => []
63+
functionsIn(undefined);
64+
// => []
65+
functionsIn(1);
66+
// => []
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# functionsIn
2+
3+
::: info
4+
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.
5+
6+
`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](mdc:../../../compatibility.md)해요.
7+
:::
8+
9+
객체의 자체 속성과 상속된 열거 가능한 속성에서 함수 속성 이름들의 배열을 생성해요.
10+
11+
## 시그니처
12+
13+
```typescript
14+
function functionsIn(object: any): string[];
15+
```
16+
17+
### 파라미터
18+
19+
- `object`: 검사할 객체예요.
20+
21+
### 반환 값
22+
23+
(`string[]`): 객체의 자체 속성과 상속된 열거 가능한 속성에서 함수 속성 이름들의 배열을 반환해요.
24+
25+
## 예시
26+
27+
```typescript
28+
import { functionsIn } from 'es-toolkit/compat';
29+
30+
function Foo() {
31+
this.a = function () {
32+
return 'a';
33+
};
34+
this.b = function () {
35+
return 'b';
36+
};
37+
}
38+
39+
Foo.prototype.c = function () {
40+
return 'c';
41+
};
42+
43+
// 상속된 함수를 포함한 함수 속성 이름들을 가져와요
44+
functionsIn(new Foo());
45+
// => ['a', 'b', 'c']
46+
47+
// 일반 객체에서도 동작해요
48+
const object = {
49+
a: function () {
50+
return 'a';
51+
},
52+
b: function () {
53+
return 'b';
54+
},
55+
};
56+
57+
functionsIn(object);
58+
// => ['a', 'b']
59+
60+
// 객체가 아닌 경우 빈 배열을 반환해요
61+
functionsIn(null);
62+
// => []
63+
functionsIn(undefined);
64+
// => []
65+
functionsIn(1);
66+
// => []
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# functionsIn
2+
3+
::: info
4+
This function is only available in `es-toolkit/compat` for compatibility reasons. It either has alternative native JavaScript APIs or isn't fully optimized yet.
5+
6+
When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed @here.
7+
:::
8+
9+
Creates an array of function property names from own and inherited enumerable properties of an object.
10+
11+
## Signature
12+
13+
```typescript
14+
// When object is any type
15+
function functionsIn(object: any): string[];
16+
```
17+
18+
### Parameters
19+
20+
- `object`: The object to inspect.
21+
22+
### Returns
23+
24+
(`string[]`): Returns the array of function property names from both own and inherited enumerable properties of the object.
25+
26+
## Examples
27+
28+
```typescript
29+
import { functionsIn } from 'es-toolkit/compat';
30+
31+
function Foo() {
32+
this.a = function () {
33+
return 'a';
34+
};
35+
this.b = function () {
36+
return 'b';
37+
};
38+
}
39+
40+
Foo.prototype.c = function () {
41+
return 'c';
42+
};
43+
44+
// Get function property names including inherited ones
45+
functionsIn(new Foo());
46+
// => ['a', 'b', 'c']
47+
48+
// Works with plain objects
49+
const object = {
50+
a: function () {
51+
return 'a';
52+
},
53+
b: function () {
54+
return 'b';
55+
},
56+
};
57+
58+
functionsIn(object);
59+
// => ['a', 'b']
60+
61+
// Returns empty array for non-objects
62+
functionsIn(null);
63+
// => []
64+
functionsIn(undefined);
65+
// => []
66+
functionsIn(1);
67+
// => []
68+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# functionsIn
2+
3+
::: info
4+
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。
5+
6+
`es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](mdc:../../../compatibility.md)
7+
:::
8+
9+
从对象自身和继承的可枚举属性中创建函数属性名称数组。
10+
11+
## 签名
12+
13+
```typescript
14+
function functionsIn(object: any): string[];
15+
```
16+
17+
### 参数
18+
19+
- `object`: 要检查的对象。
20+
21+
### 返回值
22+
23+
(`string[]`): 返回对象自身和继承的可枚举属性中的函数属性名称数组。
24+
25+
## 示例
26+
27+
```typescript
28+
import { functionsIn } from 'es-toolkit/compat';
29+
30+
function Foo() {
31+
this.a = function () {
32+
return 'a';
33+
};
34+
this.b = function () {
35+
return 'b';
36+
};
37+
}
38+
39+
Foo.prototype.c = function () {
40+
return 'c';
41+
};
42+
43+
// 获取包括继承的函数在内的函数属性名
44+
functionsIn(new Foo());
45+
// => ['a', 'b', 'c']
46+
47+
// 适用于普通对象
48+
const object = {
49+
a: function () {
50+
return 'a';
51+
},
52+
b: function () {
53+
return 'b';
54+
},
55+
};
56+
57+
functionsIn(object);
58+
// => ['a', 'b']
59+
60+
// 对于非对象返回空数组
61+
functionsIn(null);
62+
// => []
63+
functionsIn(undefined);
64+
// => []
65+
functionsIn(1);
66+
// => []
67+
```

src/compat/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export { cloneDeepWith } from './object/cloneDeepWith.ts';
138138
export { defaults } from './object/defaults.ts';
139139
export { findKey } from './object/findKey.ts';
140140
export { fromPairs } from './object/fromPairs.ts';
141+
export { functionsIn } from './object/functionsIn.ts';
141142
export { get } from './object/get.ts';
142143
export { has } from './object/has.ts';
143144
export { invertBy } from './object/invertBy.ts';

src/compat/object/functionsIn.spec.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { functionsIn } from './functionsIn';
3+
4+
describe('functionsIn', () => {
5+
function Foo(this: any) {
6+
this.a = function () {
7+
return 'a';
8+
};
9+
this.b = function () {
10+
return 'b';
11+
};
12+
}
13+
14+
Foo.prototype.c = function () {
15+
return 'c';
16+
};
17+
18+
it('should return the function property names of an object', () => {
19+
// eslint-disable-next-line
20+
// @ts-ignore
21+
expect(functionsIn(new Foo())).toEqual(['a', 'b', 'c']);
22+
});
23+
24+
it('should return an empty array for non objects', () => {
25+
const values = [null, undefined, 1, 'abc', true, Symbol('test')];
26+
const expected = values.map(() => []);
27+
28+
const actual = values.map(value => functionsIn(value));
29+
30+
expect(actual).toEqual(expected);
31+
});
32+
33+
it('should include inherited functions', () => {
34+
function Bar(this: any) {
35+
this.a = function () {
36+
return 'a';
37+
};
38+
}
39+
40+
Bar.prototype.b = function () {
41+
return 'b';
42+
};
43+
44+
// eslint-disable-next-line
45+
// @ts-ignore
46+
const bar = new Bar();
47+
48+
expect(functionsIn(bar)).toEqual(['a', 'b']);
49+
});
50+
51+
it('should work with plain objects', () => {
52+
const object = {
53+
a: function () {
54+
return 'a';
55+
},
56+
b: function () {
57+
return 'b';
58+
},
59+
};
60+
61+
expect(functionsIn(object)).toEqual(['a', 'b']);
62+
});
63+
});

0 commit comments

Comments
 (0)