Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(values): add values in src/object #687

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function keyBy<T, K extends PropertyKey>(arr: readonly T[], getKeyFromIte
return result;
}
```

</details>

### 1.3 Documentation
Expand Down
6 changes: 2 additions & 4 deletions jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"./compat": "./src/compat/index.ts"
},
"publish": {
"include": [
"./src/**/*.ts"
]
"include": ["./src/**/*.ts"]
}
}
}
2 changes: 1 addition & 1 deletion src/compat/predicate/isString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('isString', () => {
it('returns false if the value is not string', () => {
const expected = falsey.map(value => value === '');

const actual = falsey.map((value) => isString(value));
const actual = falsey.map(value => isString(value));

expect(actual).toEqual(expected);

Expand Down
34 changes: 34 additions & 0 deletions src/object/values.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest';
import { values } from './values';

describe('values', () => {
it('should return an array of the values of the object', () => {
const obj = { a: 1, b: 'hello', c: true };
const result = values(obj);
expect(result).toEqual([1, 'hello', true]);
});

it('should return an array of characters in case a string is passed', () => {
// @ts-expect-error (a string is not the ideal param for this function but for keeping parity with lodash
// this case should be tested.
const result = values('hello');
expect(result).toEqual(['h', 'e', 'l', 'l', 'o']);
});

it('should return an empty array for an empty object', () => {
const result = values({});
expect(result).toEqual([]);
});

it('should work with an array-like object', () => {
const obj = { 0: 'a', 1: 'b', 2: 'c' };
const result = values(obj);
expect(result).toEqual(['a', 'b', 'c']);
});

it('should return values for nested objects', () => {
const obj = { a: { nested: 'hello' }, b: 2 };
const result = values(obj);
expect(result).toEqual([{ nested: 'hello' }, 2]);
});
});
22 changes: 22 additions & 0 deletions src/object/values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Returns an array of the values of an object.
*
* This function takes an object and returns a new array containing all the values
* of the object's properties.
*
* @template T - The type of the object.
* @template K - The type of keys in the object.
* @param {T} obj - The object to extract values from.
* @returns {Array<T[K]>} An array of the object's values.
*
* @example
* const obj = { a: 1, b: 'hello', c: true };
* const result = values(obj);
* // result will be [1, 'hello', true]
* const result = values('hello')
* // result will be ["h", "e", "l", "l", "l", "o"]
*/

export function values<T extends Record<string, any>, K extends keyof T>(obj: T): Array<T[K]> {
return obj == null ? [] : Object.values(obj);
}
Loading