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

Added toArray function #102

Merged
merged 1 commit into from
Feb 3, 2025
Merged
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ A collection of useful utility functions with associated TypeScript types.
- [Dictionary](#dictionary)
- [Constants](#constants)
- [isJsdom](#isjsdom)
- [Arrays](#arrays)
- [toArray](#toarray)
- [Attribution](#attribution)

## Install
Expand Down Expand Up @@ -2435,6 +2437,32 @@ if (isJsdom) {

---

### Arrays

---

#### toArray

Checks the provided value is an array and if not returns the value within an array.

| Parameter | Type | Optional | Description |
| --------- | ---- | -------- | -------------------- |
| value | any | false | The value to convert |

Return type: `Array<any>`

**Example:**

```typescript
import { toArray } from '@qntm-code/utils';

const array: Array<any> = toArray('hello');

console.log(array); // ['hello']
```

---

## Attribution

- [isArguments](#isarguments), [isBuffer](#isbuffer), [isError](#iserror), [isGeneratorObject](#isgeneratorobject), [isRegExp](#isregexp), [typeOf](#typeof) initially based off Jon Schlinkert's [kind-of](https://github.com/jonschlinkert/kind-of)
Expand Down
1 change: 1 addition & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './to-array.js';
15 changes: 15 additions & 0 deletions src/array/to-array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { toArray } from './to-array';

describe('toArray', () => {
it('should return the provided value if it is already an array', () => {
const value = ['a', 'b'];

expect(toArray(value)).toBe(value);
});

it('should return the provided value in a new array if it is not already an array', () => {
const value = 'a';

expect(toArray(value)).toEqual([value]);
});
});
6 changes: 6 additions & 0 deletions src/array/to-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Checks the provided value is an array and if not returns the value within an array.
*/
export function toArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './array/index.js';
export * from './async/index.js';
export * from './clone/clone.js';
export * from './constants/index.js';
Expand Down
Loading