diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 5deb5c1f7..19b77ec7c 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -45,6 +45,7 @@ export function keyBy(arr: readonly T[], getKeyFromIte return result; } ``` + ### 1.3 Documentation diff --git a/jsr.json b/jsr.json index 72bffad0f..b20c2b03e 100644 --- a/jsr.json +++ b/jsr.json @@ -6,8 +6,6 @@ "./compat": "./src/compat/index.ts" }, "publish": { - "include": [ - "./src/**/*.ts" - ] + "include": ["./src/**/*.ts"] } -} \ No newline at end of file +} diff --git a/src/compat/predicate/isString.spec.ts b/src/compat/predicate/isString.spec.ts index c380da7cd..ef21ae195 100644 --- a/src/compat/predicate/isString.spec.ts +++ b/src/compat/predicate/isString.spec.ts @@ -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); diff --git a/src/object/values.spec.ts b/src/object/values.spec.ts new file mode 100644 index 000000000..db3955e9a --- /dev/null +++ b/src/object/values.spec.ts @@ -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]); + }); +}); diff --git a/src/object/values.ts b/src/object/values.ts new file mode 100644 index 000000000..98f1043ea --- /dev/null +++ b/src/object/values.ts @@ -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} 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, K extends keyof T>(obj: T): Array { + return obj == null ? [] : Object.values(obj); +}