File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 11import type { Arrayable } from 'type-fest' ;
22import type { Choice , Maybe , Optional , ReadonlyArrayStrict } from './types.js' ;
33
4- export function firstOrSelf < T > ( value ?: Maybe < Arrayable < T > > ) : Optional < T > {
5- if ( ! value ) return undefined ;
4+ /**
5+ * Returns the first element of an array or the value itself if it's not an array.
6+ *
7+ * ```ts
8+ * import { firstOrSelf } from '@jagaad/utils';
9+ *
10+ * const singleValue = firstOrSelf('Hello'); // 'Hello'
11+ * const arrayValue = firstOrSelf(['Hello', 'World']); // 'Hello'
12+ * const emptyArray = firstOrSelf([]); // undefined
13+ * const nullValue = firstOrSelf(null); // undefined
14+ * const undefinedValue = firstOrSelf(undefined); // undefined
15+ * const numberValue = firstOrSelf(42); // 42
16+ * const emptyString = firstOrSelf(''); // ''
17+ * const booleanValue = firstOrSelf(false); // false
18+ * ```
19+ */
20+ export function firstOrSelf < T > ( value : Maybe < Arrayable < T > > ) : Optional < T > {
21+ if ( value == null ) return undefined ;
622
723 if ( Array . isArray ( value ) ) {
824 return value . at ( 0 ) ;
You can’t perform that action at this time.
0 commit comments