File tree Expand file tree Collapse file tree 3 files changed +24
-0
lines changed Expand file tree Collapse file tree 3 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -36,4 +36,5 @@ export const MockWorkspace: Workspace = {
36
36
created_at : "" ,
37
37
updated_at : "" ,
38
38
project_id : "project-id" ,
39
+ owner_id : "test-user-id" ,
39
40
}
Original file line number Diff line number Diff line change
1
+ import { firstOrItem } from "./array"
2
+
3
+ describe ( "array" , ( ) => {
4
+ describe ( "firstOrItem" , ( ) => {
5
+ it ( "returns null if empty array" , ( ) => {
6
+ expect ( firstOrItem ( [ ] ) ) . toBeNull ( )
7
+ } )
8
+
9
+ it ( "returns first item if array with more one item" , ( ) => {
10
+ expect ( firstOrItem ( [ "a" , "b" ] ) ) . toEqual ( "a" )
11
+ } )
12
+
13
+ it ( "returns item if single item" , ( ) => {
14
+ expect ( firstOrItem ( "c" ) ) . toEqual ( "c" )
15
+ } )
16
+ } )
17
+ } )
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Helper function that, given an array or a single item:
3
+ * - If an array with no elements, returns null
4
+ * - If an array with 1 or more elements, returns the first element
5
+ * - If a single item, returns that item
6
+ */
1
7
export const firstOrItem = < T > ( itemOrItems : T | T [ ] ) : T | null => {
2
8
if ( Array . isArray ( itemOrItems ) ) {
3
9
return itemOrItems . length > 0 ? itemOrItems [ 0 ] : null
You can’t perform that action at this time.
0 commit comments