-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
toarray.ts
33 lines (28 loc) · 1.03 KB
/
toarray.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module utils/toarray
*/
/**
* Transforms any value to an array. If the provided value is already an array, it is returned unchanged.
*
* @label MUTABLE
* @param data The value to transform to an array.
* @returns An array created from data.
*/
export default function toArray<T>( data: ArrayOrItem<T> ): Array<T>;
/**
* Transforms any value to an array. If the provided value is already an array, it is returned unchanged.
*
* @label IMMUTABLE
* @param data The value to transform to an array.
* @returns An array created from data.
*/
export default function toArray<T>( data: ReadonlyArrayOrItem<T> ): ReadonlyArray<T>;
export default function toArray<T>( data: ArrayOrItem<T> ): Array<T> {
return Array.isArray( data ) ? data : [ data ];
}
export type ArrayOrItem<T> = T | Array<T>;
export type ReadonlyArrayOrItem<T> = T | ReadonlyArray<T>;