Skip to content

Commit

Permalink
feat(values, valuesIn): Implement values & valuesIn
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 committed Feb 1, 2025
1 parent fd317fe commit b8da694
Show file tree
Hide file tree
Showing 14 changed files with 514 additions and 1 deletion.
34 changes: 34 additions & 0 deletions docs/ja/reference/compat/object/values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# values

::: info
この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。

`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。
:::

`object` の列挙可能なプロパティ値を返します。

オブジェクトではない値はオブジェクトに変換されます。

## インターフェース

```typescript
function values<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function values<T>(arr: ArrayLike<T>): T[];
function values<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### パラメータ

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): 問い合わせるオブジェクト。

### 戻り値

(`T[]`): プロパティ値の配列。

##

```typescript
const obj = { a: 1, b: 2, c: 3 };
values(obj); // => [1, 2, 3]
```
38 changes: 38 additions & 0 deletions docs/ja/reference/compat/object/valuesIn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# valuesIn

::: info
この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。

`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。
:::

オブジェクトから値を取得します。プロトタイプから継承されたものも含みます。

- 値がオブジェクトでない場合、オブジェクトに変換されます。
- [配列のようなオブジェクト](../predicate/isArrayLike.md)は配列のように扱われます。
- 一部のインデックスが欠けている疎な配列は密な配列のように扱われます。
- 値が `null` または `undefined` の場合、空の配列を返します。
- プロトタイプオブジェクトを処理する際には、`constructor` プロパティは結果から除外されます。

## インターフェース

```typescript
function valuesIn<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function valuesIn<T>(arr: ArrayLike<T>): T[];
function valuesIn<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### パラメータ

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): 値を調べるためのオブジェクト。

### 戻り値

(`T[]`): プロパティ値の配列。

##

```typescript
const obj = { a: 1, b: 2, c: 3 };
valuesIn(obj); // => [1, 2, 3]
```
34 changes: 34 additions & 0 deletions docs/ko/reference/compat/object/values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# values

::: info
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.

`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요.
:::

`object` 객체의 열거 가능한 프로퍼티 값들을 반환해요.

객체가 아닌 값은 객체로 변환해요.

## 인터페이스

```typescript
function values<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function values<T>(arr: ArrayLike<T>): T[];
function values<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### 파라미터

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): 프로퍼티 값들을 구할 객체.

### 반환 값

(`T[]`): 프로퍼티 값의 배열.

## 예시

```typescript
const obj = { a: 1, b: 2, c: 3 };
values(obj); // => [1, 2, 3]
```
38 changes: 38 additions & 0 deletions docs/ko/reference/compat/object/valuesIn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# valuesIn

::: info
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.

`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요.
:::

객체에서 접근할 수 있는 모든 프로퍼티 값을 반환해요. 프로토타입에서 상속된 속성도 포함돼요.

- 값이 객체가 아닌 경우, 객체로 변환돼요.
- [배열 같은 객체](../predicate/isArrayLike.md)는 배열처럼 다뤄요.
- 일부 인덱스가 빠져 있는 희소 배열은 밀집 배열처럼 다뤄요.
- 값이 `null` 또는 `undefined`이면, 빈 배열을 반환해요.
- 프로토타입 객체를 처리할 때는 `constructor` 프로퍼티를 결과에서 제외해요.

## 인터페이스

```typescript
function valuesIn<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function valuesIn<T>(arr: ArrayLike<T>): T[];
function valuesIn<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### 파라미터

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): 프로퍼티 값을 찾을 객체.

### 반환 값

(`T[]`): 프로퍼티 값의 배열.

## 예시

```typescript
const obj = { a: 1, b: 2, c: 3 };
valuesIn(obj); // => [1, 2, 3]
```
2 changes: 1 addition & 1 deletion docs/reference/compat/object/keysIn.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This function is only available in `es-toolkit/compat` for compatibility reasons
When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
:::

This function retrieves the names of string-keyed properties from an object, including those inherited from its prototype.
Retrieves the names of string-keyed properties from an object, including those inherited from its prototype.

- If the value is not an object, it is converted to an object.
- Array-like objects are treated like arrays.
Expand Down
32 changes: 32 additions & 0 deletions docs/reference/compat/object/values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# values

::: info
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.

When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
:::

Creates an array of the own enumerable property values of `object`.

## Signature

```typescript
function values<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function values<T>(arr: ArrayLike<T>): T[];
function values<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### Parameters

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): The object to query.

### Returns

(`T[]`): Returns an array of property values.

## Examples

```typescript
const obj = { a: 1, b: 2, c: 3 };
values(obj); // => [1, 2, 3]
```
38 changes: 38 additions & 0 deletions docs/reference/compat/object/valuesIn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# valuesIn

::: info
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.

When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
:::

Retrieves the values from an object, including those inherited from its prototype.

- If the value is not an object, it is converted to an object.
- Array-like objects are treated like arrays.
- Sparse arrays with some missing indices are treated like dense arrays.
- If the value is `null` or `undefined`, an empty array is returned.
- When handling prototype objects, the `constructor` property is excluded from the results.

## Signature

```typescript
function valuesIn<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function valuesIn<T>(arr: ArrayLike<T>): T[];
function valuesIn<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### Parameters

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): The object to query.

### Returns

(`T[]`): Returns an array of property values.

## Examples

```typescript
const obj = { a: 1, b: 2, c: 3 };
valuesIn(obj); // => [1, 2, 3]
```
34 changes: 34 additions & 0 deletions docs/zh_hans/reference/compat/object/values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# values

::: info
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。

`es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)
:::

创建一个包含`object`自身可枚举属性值的数组。

非对象值将被强制转换为对象。

## 签名

```typescript
function values<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function values<T>(arr: ArrayLike<T>): T[];
function values<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### 参数

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): 要查询的对象。

### 返回值

(`T[]`): 属性值数组。

## 示例

```typescript
const obj = { a: 1, b: 2, c: 3 };
values(obj); // => [1, 2, 3]
```
38 changes: 38 additions & 0 deletions docs/zh_hans/reference/compat/object/valuesIn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# valuesIn

::: info
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。

`es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)
:::

此函数检索对象中字符串键属性的值,包括从其原型继承的属性。

- 如果值不是对象,则会将其转换为对象。
- 类数组对象被视为数组。
- 稀疏数组中缺少某些索引的情况被视为密集数组。
- 如果值为 `null``undefined`,则返回一个空数组。
- 在处理原型对象时,`constructor` 属性会从结果中排除。

## 签名

```typescript
function valuesIn<T>(object: Record<PropertyKey, T> | null | undefined): T[];
function valuesIn<T>(arr: ArrayLike<T>): T[];
function valuesIn<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
```

### 参数

- `object` (`Record<PropertyKey, T> | ArrayLike<T>`): 要查询的对象。

### 返回值

(`T[]`): 一个属性值数组。

## 示例

```typescript
const obj = { a: 1, b: 2, c: 3 };
valuesIn(obj); // => [1, 2, 3]
```
2 changes: 2 additions & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export { propertyOf } from './object/propertyOf.ts';
export { set } from './object/set.ts';
export { toDefaulted } from './object/toDefaulted.ts';
export { unset } from './object/unset.ts';
export { values } from './object/values.ts';
export { valuesIn } from './object/valuesIn.ts';

export { conforms } from './predicate/conforms.ts';
export { conformsTo } from './predicate/conformsTo.ts';
Expand Down
44 changes: 44 additions & 0 deletions src/compat/object/values.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import * as lodashStable from 'es-toolkit/compat';
import { values } from './values';
import { args } from '../_internal/args';
import { strictArgs } from '../_internal/strictArgs';

describe('values', () => {
it(`should get string keyed values of \`object\``, () => {
const object = { a: 1, b: 2 };
const actual = values(object).sort();

expect(actual).toEqual([1, 2]);
});

it(`should work with an object that has a \`length\` property`, () => {
const object = { 0: 'a', 1: 'b', length: 2 };
const actual = values(object).sort();

expect(actual).toEqual([2, 'a', 'b']);
});

it(`should not include inherited string keyed property values`, () => {
function Foo(this: any) {
this.a = 1;
}
Foo.prototype.b = 2;

const expected = [1];
// eslint-disable-next-line
// @ts-ignore
const actual = values(new Foo()).sort();

expect(actual).toEqual(expected);
});

it(`should work with \`arguments\` objects`, () => {
const vals = [args, strictArgs];
const expected = lodashStable.map(vals, lodashStable.constant([1, 2, 3]));

const actual = lodashStable.map(vals, value => values(value).sort());

expect(actual).toEqual(expected);
});
});
Loading

0 comments on commit b8da694

Please sign in to comment.